Generate PDFs in Node.js
Render PDFs from a Node.js app with a single fetch call — no headless browser to run or scale.
Generating PDFs in Node.js usually means running Puppeteer or headless Chrome — memory leaks, cold starts and a process to babysit. PDFgen replaces all of that with one HTTP call: POST your HTML (with Handlebars tokens) and data, and get a print-ready PDF back.
Node 18+ ships fetch built in, so the example below needs no dependencies. Loop over your records to render thousands of documents from the same template.
Generate a PDF in Node.js
POST your HTML and data to /api/v1/generate with your API key, then save the PDF that comes back. Swap in your own HTML or a saved template_id and loop over records to render at scale.
import fs from "node:fs";const res = await fetch("https://pdfgen.com/api/v1/generate", {method: "POST",headers: {Authorization: "Bearer pdfg_live_xxx","Content-Type": "application/json",},body: JSON.stringify({html: "<h1>Invoice {{number}}</h1><p>Billed to {{client}} — total {{total}}</p>",engine: "handlebars",format: "A4",data: { number: "INV-001", client: "Acme Corp", total: "$2,400.00" },}),});// Default response is the PDF binary. Pass export_type:"url" for a hosted link.fs.writeFileSync("invoice.pdf", Buffer.from(await res.arrayBuffer()));
Convert existing HTML to PDF in Node.js
Already have finished HTML? Skip templating — read an .html file and POST it and send it with engine: "legacy", which tells the API your markup is final.
import fs from "node:fs";// Your existing, already-styled HTML file.const html = fs.readFileSync("invoice.html", "utf8");const res = await fetch("https://pdfgen.com/api/v1/generate", {method: "POST",headers: {Authorization: "Bearer pdfg_live_xxx","Content-Type": "application/json",},// engine "legacy" = treat the HTML as final, no templating.body: JSON.stringify({ html, engine: "legacy", format: "A4" }),});fs.writeFileSync("invoice.pdf", Buffer.from(await res.arrayBuffer()));
How it works
- 1
Send your HTML + data
POST an HTML template (with optional Handlebars tokens) and a data object to the API.
- 2
We render the PDF
PDFgen renders it server-side — no headless browser for you to run or scale.
- 3
Get the PDF back
Receive the PDF binary (or a hosted link with export_type:"url") and serve it from Node.js.
Good to know
- Node 18+ has fetch built in — no axios or node-fetch required.
- Keep your API key in an environment variable, never in client-side code.
- The default response is the raw PDF; send "export_type": "url" to get a hosted, expiring link instead.
Frequently asked questions
- Do I need Puppeteer to generate PDFs in Node.js?
- No. PDFgen runs the rendering for you, so you make a single fetch call instead of launching and scaling headless Chrome yourself.
- How do I generate many PDFs at once in Node?
- Loop over your records and call /api/v1/generate for each, or fire requests in parallel with Promise.all — each call returns one PDF.
- Can I save a reusable template instead of sending HTML every time?
- Yes — create a template once and send "template_id" with your data instead of inline "html". See the API docs for the templates endpoint.