Generate PDFs with Node.js

Complete Node.js code examples for the PixDoc API

Install

Terminal
# No dependencies — uses native fetch (Node 18+)

Generate a PDF

Send HTML to the /api/v1/pdf endpoint and save the binary response.

Node.js
const fs = require("fs");

const response = await fetch("https://pixdoc.dev/api/v1/pdf", {
  method: "POST",
  headers: {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ html: "<h1>Hello World</h1>" }),
});

const pdf = await response.arrayBuffer();
fs.writeFileSync("output.pdf", Buffer.from(pdf));

Take a Screenshot

The /api/v1/screenshot endpoint returns a PNG image.

Node.js
const fs = require("fs");

const response = await fetch("https://pixdoc.dev/api/v1/screenshot", {
  method: "POST",
  headers: {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ html: "<h1>Hello World</h1>" }),
});

const png = await response.arrayBuffer();
fs.writeFileSync("screenshot.png", Buffer.from(png));

Use a Template

Pass a template_id and variables instead of raw HTML.

Node.js
const fs = require("fs");

const response = await fetch("https://pixdoc.dev/api/v1/pdf", {
  method: "POST",
  headers: {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    template_id: "invoice",
    variables: {
      company: "Acme Corp",
      total: "$1,250.00",
    },
  }),
});

const pdf = await response.arrayBuffer();
fs.writeFileSync("invoice.pdf", Buffer.from(pdf));

Next Steps