Generate PDFs in Node.js
Generate PDFs from HTML, Markdown, URLs or templates in Node.js — using the official renderpdfs npm package or a plain fetch call.
1. Install
RenderPDFs uses a plain REST API — no SDK required. For Node.js, install the dependency below and grab your API key from renderpdfs.com/signup (free, 100 PDFs/month, no credit card).
npm install renderpdfs
# or
pnpm add renderpdfs
# or
yarn add renderpdfs2. Convert HTML to PDF
The simplest case: send an HTML string, get back a PDF binary. Anything Chromium renders works — Flexbox, Grid, web fonts, SVG, JavaScript.
import { RenderPDFs } from "renderpdfs";
import { writeFile } from "node:fs/promises";
const client = new RenderPDFs({ apiKey: process.env.RENDERPDFS_KEY });
const pdf = await client.generate({
html: "<h1>Hello PDF</h1><p>Generated from Node.js</p>",
});
await writeFile("hello.pdf", pdf);3. Convert a URL to PDF
Pass url instead of html and RenderPDFs fetches the page, waits for JS, and snapshots it. Useful for archiving dashboards, public pages, or invoices served from your app.
const pdf = await client.generate({
url: "https://example.com",
options: { format: "A4", printBackground: true },
});
await writeFile("page.pdf", pdf);4. Use a built-in template
Skip the design work. RenderPDFs ships six battle-tested templates — invoice, receipt, report, contract, certificate, offer_letter. Send JSON, get a styled PDF.
const pdf = await client.generate({
template: "invoice",
data: {
company: "Acme Inc.",
invoice_number: "INV-001",
items: [
{ description: "Consulting", quantity: 10, unit_price: 120 },
],
},
});5. Custom paper, margins, headers
Control page format, orientation, margins, and running headers/footers via the options object. All standard Chromium PDF settings are supported.
const pdf = await client.generate({
html: "<h1>Report</h1>",
options: {
format: "Letter", // A4, A5, Letter, Legal, Tabloid
landscape: false,
margin: { top: "20mm", bottom: "20mm", left: "15mm", right: "15mm" },
printBackground: true,
displayHeaderFooter: true,
headerTemplate: '<div style="font-size:9px; width:100%; text-align:center;">Acme Report</div>',
footerTemplate: '<div style="font-size:9px; width:100%; text-align:center;"><span class="pageNumber"></span> / <span class="totalPages"></span></div>',
},
});6. Store the PDF and share a link
For emailable links or webhook payloads, set store: true in the body. The response becomes { url, expires_in } — the PDF is hosted on our CDN for 24 hours by default.
const { url, expires_in } = await client.generate({
html: "<h1>Shared report</h1>",
store: true,
});
console.log(`Download at ${url} (expires in ${expires_in}s)`);7. Notes & gotchas
Every request needs an X-API-Key header. Grab a free key at renderpdfs.com/signup — 100 PDFs/month, no credit card. Treat the key like a password: keep it server-side, never expose it in browser code.
By default the endpoint streams back the raw PDF binary (Content-Type: application/pdf). Set `store: true` in the body and the response becomes { url, expires_in } — useful for emailing links or attaching to webhooks.
Non-2xx responses return JSON: { error: string }. The most common cases are 401 (bad API key), 402 (over plan quota), and 422 (invalid HTML or URL). Always parse the error body before retrying.
Other languages
Generate your first PDF in 60 seconds
100 free PDFs per month. No credit card. HTML, Markdown, URLs, templates, MCP — all included.