Guides / Invoice PDF in Node.js

Generate PDF invoices in Node.js

JSON in, PDF out, with the fetch built into Node 18+. No Chromium download, no PDFKit coordinates.

What you need

  • Node.js 18 or newer (for built-in fetch)
  • A free SlipKiln API key in SLIPKILN_KEY. Keep it on the server, never in browser code.

1. Render an invoice

// Node 18+ (built-in fetch). Save as invoice.mjs and run: node invoice.mjs
import { writeFile } from "node:fs/promises";

const invoice = {
  number: "INV-1042",
  issue_date: "2026-11-20",
  due_date: "2026-12-20",
  currency: "USD",
  customer: { name: "Acme Co.", email: "ap@acme.example", address: ["100 Main St", "Denver, CO 80202"] },
  items: [
    { description: "Consulting", qty: 10, unit_price: 120 },
    { description: "Hosting (November)", qty: 1, unit_price: 49 },
  ],
  tax_rate: 8.25,
  notes: "Thank you for your business. Net 30.",
};

const res = await fetch("https://api.slipkiln.com/v1/render/template", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.SLIPKILN_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ template: "invoice", data: invoice }),
});
if (!res.ok) throw new Error((await res.json()).message);
await writeFile("INV-1042.pdf", Buffer.from(await res.arrayBuffer()));
console.log("documents this month:", res.headers.get("x-usage-month"));

The invoice template computes subtotal, tax and total from items and tax_rate. Add a brand object with name, color and a base64 logo data URI to match your look.

2. Serve it from Express

// Express route that streams the PDF to the browser
app.get("/invoices/:number.pdf", async (req, res, next) => {
  try {
    const data = await loadInvoice(req.params.number); // your own lookup
    const r = await fetch("https://api.slipkiln.com/v1/render/template", {
      method: "POST",
      headers: { Authorization: `Bearer ${process.env.SLIPKILN_KEY}`, "Content-Type": "application/json" },
      body: JSON.stringify({ template: "invoice", data }),
    });
    if (!r.ok) return res.status(502).json(await r.json());
    res.type("application/pdf");
    res.set("Content-Disposition", `attachment; filename="${req.params.number}.pdf"`);
    res.send(Buffer.from(await r.arrayBuffer()));
  } catch (e) { next(e); }
});

3. Receipts from Stripe webhooks

A common setup: when Stripe reports invoice.paid, render a branded receipt and email or store it.

// After a Stripe invoice is paid, render a receipt PDF from the webhook payload
// (verify the webhook signature first, as Stripe's docs describe)
const inv = event.data.object; // invoice.paid
const receipt = {
  number: inv.number,
  date: new Date(inv.status_transitions.paid_at * 1000).toISOString().slice(0, 10),
  currency: inv.currency.toUpperCase(),
  customer: { name: inv.customer_name },
  items: inv.lines.data.map((l) => ({
    description: l.description,
    qty: l.quantity ?? 1,
    unit_price: l.amount / 100 / (l.quantity ?? 1),
  })),
};
// POST { template: "receipt", data: receipt } exactly like the invoice example

Your own HTML

For reports, packing slips or anything custom, post HTML to /v1/render/html. Inline CSS, fonts and images as data: URIs; external URLs are blocked by design.

const html = `<!doctype html><style>
  @page { margin: 0 }  body { font: 14px system-ui; margin: 20mm }
  tr { break-inside: avoid }
</style><h1>Report</h1>${rowsToTable(rows)}`;

await fetch("https://api.slipkiln.com/v1/render/html", {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.SLIPKILN_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({ html, options: { format: "Letter" } }),
});

Errors and limits

  • 401: missing or wrong key. 402: the free plan's 50 documents are used up this month.
  • 429 / 503: back off and retry (honor retry-after).
  • Request up to 2 MB, PDF up to 20 MB, 15 seconds per render.

Puppeteer vs PDFKit vs an API

PDFKit and pdfmake are fast and dependency-light, but you lay out every line in code. Puppeteer and Playwright give you full CSS, but each render runs a Chromium you have to size, patch and sandbox (details). An API keeps HTML/CSS layouts without running a browser yourself.

Related

Updated November 2026.