Generate a PDF in Node.js

The simplest way to generate a PDF in Node.js is to call the Docweave API with the built-in fetch — no headless browser to install, launch, or keep patched yourself. POST your HTML, a URL, or a template + JSON to one endpoint and get back a rendered PDF.

Generate a PDF from HTML in Node.js

Using only fetch and node:fs/promises (both built into modern Node.js — no extra HTTP client to install), POST your HTML to /api/v1/pdf and write the response to disk:

import { writeFile } from "node:fs/promises";

const response = await fetch("https://docweave.dev/api/v1/pdf", {
  method: "POST",
  headers: {
    Authorization: "Bearer dw_live_your_key",
    "Content-Type": "application/json",
    Accept: "application/pdf",
  },
  body: JSON.stringify({
    source: {
      type: "html",
      html: "<h1>Hello from Node.js</h1><p>Generated with Docweave.</p>",
    },
    options: { format: "A4" },
    idempotencyKey: "node-quickstart-001",
  }),
});

if (!response.ok) {
  throw new Error(`Docweave API error: ${response.status} ${await response.text()}`);
}

const pdfBuffer = Buffer.from(await response.arrayBuffer());
await writeFile("output.pdf", pdfBuffer);

console.log("Saved output.pdf");

Generate a PDF from a URL instead

Swap the source to render an existing page instead of inline HTML:

const response = await fetch("https://docweave.dev/api/v1/pdf", {
  method: "POST",
  headers: {
    Authorization: "Bearer dw_live_your_key",
    "Content-Type": "application/json",
    Accept: "application/pdf",
  },
  body: JSON.stringify({
    source: { type: "url", url: "https://example.com/report" },
    options: { format: "A4" },
  }),
});

if (!response.ok) {
  throw new Error(`Docweave API error: ${response.status} ${await response.text()}`);
}

const pdfBuffer = Buffer.from(await response.arrayBuffer());
await writeFile("output.pdf", pdfBuffer);

…or from an AI agent

The same Node.js workflow works from an AI agent with no fetch code at all. Run npx @docweave/mcp and your agent gets a generate_pdf tool that wraps the same render orchestration as the REST endpoint:

// From an AI agent over MCP — no fetch/HTTP boilerplate:
// npx @docweave/mcp
generate_pdf({
  source: {
    type: "html",
    html: "<h1>Hello from Node.js</h1><p>Generated with Docweave.</p>",
  },
  outputPath: "/tmp/output.pdf"
})

FAQ

How do I generate a PDF in Node.js without Puppeteer or Playwright?

Call the Docweave API instead of running a headless browser yourself: POST your HTML (or a URL, or a template + JSON) to /api/v1/pdf with fetch, and write the returned PDF bytes to disk with node:fs. Chromium runs on Docweave's servers, not in your Node process.

What's the simplest way to convert HTML to PDF in Node.js?

Use the built-in fetch to POST { "source": { "type": "html", "html": "<h1>...</h1>" } } to https://docweave.dev/api/v1/pdf with an Authorization: Bearer header, then save response.arrayBuffer() to a file. No native dependencies, no browser binaries to download.

Do I need to install Chromium or Playwright in my Node.js project?

No. Docweave renders with Chromium on its own infrastructure, so your Node.js app only needs a standard HTTP client (fetch) — nothing to install, no serverless-function size limits from bundling a browser.

How do I avoid double-generating a PDF on a retried Node.js request?

Pass an idempotencyKey in the request body (for example a job ID or order number). If the same key is sent again — say, after a network retry — Docweave returns the original result instead of rendering and billing twice.

Ready to generate PDFs from your Node.js app?

Get an API key