Generate receipt PDFs in Node.js

The simplest way to generate a receipt PDF in Node.js is to POST a receipt HTML template plus your order JSON to the Docweave API — no headless browser to install or manage yourself.

Receipt template example

Any HTML works as a template. Use {{ placeholders }} for the order id, items, amount paid, payment method, and date you pass in data:

<article style="font-family: sans-serif; max-width: 420px;">
  <header style="text-align:center;">
    <h1 style="margin-bottom:0;">Receipt</h1>
    <div>Order {{ order_id }}</div>
    <div>{{ date }}</div>
  </header>

  <table style="width:100%; border-collapse:collapse; margin-top:16px;">
    {{{ items }}}
  </table>

  <p style="text-align:right; margin-top:16px;">
    <strong>Amount paid: {{ amount_paid }}</strong>
  </p>
  <p style="text-align:right;">Payment method: {{ payment_method }}</p>
</article>

Node.js code sample

Use the fetch built into modern Node.js to POST the template and data to /api/v1/pdf, then save the raw PDF bytes with node:fs/promises:

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

const template = `<article style="font-family: sans-serif; max-width: 420px;">
  <header style="text-align:center;">
    <h1 style="margin-bottom:0;">Receipt</h1>
    <div>Order {{ order_id }}</div>
    <div>{{ date }}</div>
  </header>
  <table style="width:100%; border-collapse:collapse; margin-top:16px;">
    {{{ items }}}
  </table>
  <p style="text-align:right; margin-top:16px;">
    <strong>Amount paid: {{ amount_paid }}</strong>
  </p>
  <p style="text-align:right;">Payment method: {{ payment_method }}</p>
</article>`;

const data = {
  order_id: "ORD-1042",
  date: "2026-07-16",
  items: "<tr><td>Wireless mouse</td><td style=\"text-align:right;\">$24.00</td></tr>",
  amount_paid: "$24.00",
  payment_method: "Visa •••• 4242",
};

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: "template", template, data },
    idempotencyKey: data.order_id,
  }),
});

if (!response.ok) {
  throw new Error(`Receipt render failed: ${response.status} ${await response.text()}`);
}

const bytes = Buffer.from(await response.arrayBuffer());
await writeFile("receipt.pdf", bytes);
console.log("Saved receipt.pdf");

…or from an AI agent

Add the Docweave MCP server (npx @docweave/mcp) and your agent gets a generate_pdf tool — the same template + JSON render, no HTTP boilerplate:

// From an AI agent over MCP — no HTTP plumbing:
// npx @docweave/mcp
generate_pdf({
  source: {
    type: "template",
    template: "<h1>Receipt {{ order_id }}</h1><p>Paid: {{ amount_paid }}</p>",
    data: { order_id: "ORD-1042", amount_paid: "$24.00" }
  },
  outputPath: "/tmp/receipt-1042.pdf"
})

FAQ

How do I generate a receipt PDF in Node.js?

Use the built-in fetch to POST a source of type "template" — your receipt HTML plus a data object with the order id, items, amount paid, and payment method — to /api/v1/pdf. Save the raw response bytes with node:fs/promises writeFile.

What headers does the receipt PDF request need?

Authorization: Bearer <your API key>, Content-Type: application/json to describe the request body, and Accept: application/pdf so Docweave returns the rendered PDF bytes instead of a JSON wrapper.

How do I avoid emailing a duplicate receipt PDF?

Pass an idempotencyKey, such as the order id. A retry with the same key returns the already-rendered PDF instead of re-rendering or re-billing the request.

Can an AI agent generate the receipt instead of my Node.js server?

Yes. Run the open-source Docweave MCP server with npx @docweave/mcp and any MCP-compatible agent gets a generate_pdf tool that performs the same template + JSON render, with no HTTP code to write.

Ready to generate receipt PDFs in production?

Get an API key

Related