Guides · Node.js · built-in fetch

Generate a fillable invoice PDF from a web page in Node.js with built-in fetch

The usual invoice-PDF pipeline is a template engine, a PDF library, and an afternoon of coordinate arithmetic. If your app already renders the invoice as an HTML page with input fields (PO number, notes, approver name — whatever the recipient fills in), you can skip all of it: render that page with pdf_forms and the returned invoice keeps those inputs as real, typeable PDF fields.

Below is a complete, runnable Node.js program using built-in fetch, no third-party dependency needed. It reads your API key from the SNAPDOK_KEY environment variable — free keys take about thirty seconds and need no card.

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

const res = await fetch("https://snapdok.io/v1/render", {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${process.env.SNAPDOK_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    url: "https://your-app.com/invoices/1042/edit",
    format: "pdf",
    pdf_forms: true,
    pdf_form_only: true,
  }),
});

if (!res.ok) throw new Error(`${res.status} ${await res.text()}`);
await writeFile("invoice-1042.pdf", Buffer.from(await res.arrayBuffer()));
console.log(res.headers.get("x-form-fields"), "// fields the recipient can type into");

Two practical tips from rendering real invoice pages. First, pre-filled values carry over: render the edit view of the invoice with amounts and line items already populated, and the PDF arrives pre-filled, with only the recipient's fields left blank. Second, add "pdf_form_only": true when the invoice page lives inside your app's UI — it strips the sidebar and account chrome so the customer sees an invoice, not a screenshot of your dashboard. If extraction is unsure it falls back to the full page (check X-Form-Extracted), so the worst case is cosmetic, never a missing field.

Renders are cached for 24 hours (X-Cache: HIT responses are free), which suits invoices well: re-sending the same invoice email does not burn quota. When you regenerate after an amount changes, the body differs — new URL or changed page content means a fresh render, so you never serve a stale total from cache unless the URL and page are byte-identical.

Notes for built-in fetch

No dependency needed: fetch is global from Node 18 onward (snapdok's own test suite uses it). The binary-safety detail is res.arrayBuffer()Buffer.from(...) — reaching for res.text() is the classic way to corrupt a PDF in Node. fetch does not reject on HTTP error statuses, so the res.ok check is load-bearing: without it, an expired key writes a 40-byte JSON error to disk with a .pdf extension.

Response headers worth reading

HeaderMeaning
X-Form-FieldsTypeable fields in the returned invoice.
X-Form-ExtractedWhether page chrome was stripped.
X-CacheHIT = served from the 24h cache, not charged.

Full parameter reference: the docs. Hard numbers on caps and timeouts: limits.

Related guides

Same task, other stacks

More with Node.js + built-in fetch

In the wild