PricingDocumentationCase StudiesBlogLoginGet started →

Guides

HTML to PDF in Node.js

A complete guide to generating PDF documents from HTML templates in Node.js using the PDF API REST API.

Why use a PDF API in Node.js?

Generating PDFs in Node.js usually means installing Puppeteer or Playwright, managing Chromium binaries, and dealing with memory leaks in production. A dedicated HTML to PDF API offloads this complexity to a managed service, letting you focus on your application logic instead of browser automation.

With PDF API, you send your HTML template and JSON data over HTTPS, and receive a secure PDF URL in under 300ms. No headless browsers to maintain, no Docker images to optimize, no surprise memory spikes.

Prerequisites

  • A PDF API account (free tier includes 500 PDFs/month)
  • Node.js 18 or higher
  • Your API key from the dashboard

Rendering a PDF with fetch

render.js
const response = await fetch("https://api.pdfapi.dev/v1/render", {
  method: "POST",
  headers: {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    html: "<h1>Hello {{name}}</h1>",
    data: { name: "World" },
  }),
});

const { url } = await response.json();
console.log("PDF URL:", url);

Using templates

Instead of passing raw HTML every time, you can save reusable templates in your dashboard and reference them by ID. This keeps your API requests smaller and your document design centralized.

Templates support Handlebars-style variables ({{variable}}), loops, and conditionals. Combine them with your JSON data to generate dynamic invoices, reports, or contracts.

Error handling

Always check the response status. PDF API returns structured error codes likeQUOTA_LIMIT_REACHED, INVALID_DATA, or TEMPLATE_NOT_FOUND. Handle these gracefully in your application to provide clear feedback to your users.

Next steps