Free Puppeteer HTML-to-PDF Templates & page.pdf() Generator

Drives Headless Chrome to print HTML5 + @page CSS via page.pdf(). Delivers full browser CSS fidelity at the cost of a ~300–400 MB browser binary, higher RAM (263.2 MB), and larger PDFs (841.2 KB & 1.35s warm render across 19 pages, plus cold browser startup if unpooled).

Install: npm install puppeteer@^24.8.0 | Runtime: Node.js (TypeScript + HTML/CSS) | Version: 24.8.0 | Popularity: 18.2M/mo installs

Works with: Node.js, Express, NestJS, Docker, Cloud Run

How Puppeteer Templates Work in CodePDF

Puppeteer launches headless Chrome, injects dynamic data into a self-contained HTML + CSS document with @page print rules, waits for fonts/assets, and prints via page.pdf().

  • Includes battle-tested @page rules, printBackground: true, and -webkit-print-color-adjust: exact so background colors never vanish
  • Pairs self-contained HTML + @page CSS with a ready-to-run TypeScript Puppeteer runner and Mustache ({{var}}) template rendering for dynamic payloads
  • Switch seamlessly to native PDFKit, pdf-lib, or @react-pdf/renderer anytime if you outgrow headless Chrome memory

Quick Start: Generate PDFs with Puppeteer

Install Puppeteer (TypeScript + HTML/CSS), then run the code CodePDF exports for any template:

npm install puppeteer@^24.8.0

  1. Run npm install puppeteer@^24.8.0 in your project root.
  2. Save the generated file as renderDocument.ts.
  3. Run node renderDocument.ts to launch headless Chrome and produce document.pdf.
  4. First run downloads the Chromium binary; set PUPPETEER_SKIP_DOWNLOAD to skip it.
  5. Dynamic {{var}} placeholders in the HTML are rendered with Mustache — install it with npm install mustache and pass your data to renderDocument() if your document uses bindings.

Example Puppeteer Output (SaaS Subscription & Usage Tax Invoice)

Compiled directly from the SaaS Subscription & Usage Tax Invoice template (TypeScript + HTML/CSS):

import puppeteer from 'puppeteer';
import { pathToFileURL } from 'node:url';

// This code is generated using codepdf.dev

// Renders the CodePDF HTML + CSS document with Puppeteer.
// Dynamic {{var}} placeholders are expanded by the small render helper below.

const TEMPLATE = `\
<!DOCTYPE html>
<!-- This code is generated using codepdf.dev -->
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>SaaS Subscription &amp; Usage Tax Invoice</title>
  <style>
    * { box-sizing: border-box; }
    body, p, h1, h2, h3, h4, h5, h6, ul, ol, li { margin: 0; padding: 0; }
    body { font-family: Helvetica, Arial, sans-serif; background: #f3f4f6; }
    @page { size: A4 portrait; margin: 0; }
    .pdf-page { position: relative; display: flex; flex-direction: column; width: 210mm; min-height: 297mm; margin: 0 auto 16px; background: #fff; overflow: hidden; }
    .pdf-page + .pdf-page { page-break-before: always; }
    .pdf-list li { break-inside: avoid; page-break-inside: avoid; }
    .pdf-text-flow { white-space: pre-wrap; overflow-wrap: break-word; }
    @media print { .pdf-page { display: block; overflow: visible; margin: 0; box-shadow: none; page-break-inside: auto; } .pdf-list { display: block; }
      .pdf-page > *:not(.pdf-page-bg):not(.pdf-hf-band):not(.pdf-hf-spacer):not(.pdf-page-number) + *:not(.pdf-page-bg):not(.pdf-hf-band):not(.pdf-hf-spacer):not(.pdf-page-number) { margin-top: 8pt; } }
    .pdf-page > * { box-sizing: border-box; }
    .pdf-view { display: -webkit-box; display: flex; -webkit-box-orient: vertical; flex-direction: column; }
    .pdf-table { display: table; width: 100%; border-collapse: collapse; }
// … (298 more lines in full generated file)

Custom Fonts in Puppeteer

Declare @font-face rules in the HTML template and await document.fonts.ready before calling page.pdf().

  1. Serve custom fonts over HTTPS or embed base64 @font-face rules in HTML.
  2. Call await page.evaluate(() => document.fonts.ready) before page.pdf() completes.
  3. Set waitUntil: 'networkidle0' in page.setContent() for remote font URLs.

Official documentation: Puppeteer docs

Puppeteer Benchmark Results

Measured on CodePDF’s 26-component, 19-page All-Components Dynamic Multi-Page Flow Stress Test (same document across all 22 libraries).

Puppeteer stress-test results and rank out of 22 PDF libraries
MetricPuppeteerRank (of 22)
Wall-clock time1.35 s20th fastest
CPU time4.40 s21st lowest
Peak RAM263.2 MB22nd lowest
PDF file size841.2 KB21st smallest
Engine type
Browser (Chromium)
Under the hood
Launches full Headless Chrome browser and prints HTML/CSS via DevTools page.pdf()
Runs in
Node.js Server / Docker / Cloud Run
Verdict
Full Chrome CSS fidelity, but 841.2 KB PDF (#2 largest), 263.2 MB RAM (highest), 1.35s warm render (+ cold Chrome launch & ~350 MB binary).

Under the Hood & Benchmark Highlights

  • Uses a real Headless Chrome browser behind the scenes — every render spins up Chromium’s Blink layout engine and prints via Chrome DevTools Protocol (page.pdf()).
  • That gives pixel-accurate modern CSS (Flexbox, Grid, SVG, web fonts), at a steep resource cost: 454.0 KB PDF (#2 largest), 2.89s render, and 252.1 MB RAM on our 10-page stress test.
  • Slower and heavier than Playwright on the exact same HTML document (454 KB vs. 296 KB; 2.89s vs. 1.95s).

Good to Know (Puppeteer Caveats)

  • Server-only with heavy Chromium footprint: Requires a ~300–400 MB headless Chrome binary, 263 MB RAM, and 1.35s warm PDF render on 19 pages (plus ~0.8–1.2s if launching Chrome cold per request); reuse a warm browser instance in production.
  • Serverless / Docker setup: AWS Lambda and minimal Linux containers need system Chrome libraries or @sparticuz/chromium instead of the default Puppeteer binary.
  • Dynamic bindings use Mustache: Install npm install mustache when exporting templates with dynamic {{variable}} fields.

Popular Puppeteer PDF Templates

All 100 CodePDF templates export to Puppeteer. Browse them by category: Career (5) · Business (6) · Education (7) · Marketing (6) · Human Resources (8) · Finance (7) · Legal (8) · Healthcare (10) · Logistics (6) · Real Estate (6) · Hospitality (4) · Operations (5) · Technology (5) · Banking (9) · Non-Profit (4) · Personal (3) · Compliance & Enterprise (1), or see the full template gallery.

Puppeteer Template FAQs

Why do background colors disappear in Puppeteer page.pdf(), and how do these templates fix it?

Chromium strips backgrounds in print mode unless both printBackground: true in page.pdf() and -webkit-print-color-adjust: exact / print-color-adjust: exact in CSS are set. CodePDF configures both automatically.

How is dynamic data injected into exported Puppeteer templates?

Dynamic {{variable}} placeholders and repeating sections are emitted as Mustache templates — install mustache (npm install mustache) and pass your JSON data object into renderDocument().

Can I migrate a Puppeteer template to a lightweight native library later?

Yes! Because every template is stored in CodePDF's engine-agnostic schema, you can re-export the exact same template to PDFKit, pdf-lib, @react-pdf/renderer, or QuestPDF in one click.

Puppeteer vs Other Node.js PDF Libraries

  • @react-pdf/renderer — 1.06 s, 237.3 MB RAM, 126.8 KB PDF. #2 smallest multi-page PDF overall (126.8 KB across 18p; 12 ms single-page, 1.06s stress) with Yoga Flexbox; run server-side in Next.js.
  • pdf-lib — 334 ms, 209.7 MB RAM, 203.8 KB PDF. Runs in browsers & Edge Workers and edits/merges existing PDFs (5.3 ms single-page; 334 ms / 203.8 KB across 20p).
  • jsPDF — 120 ms, 98.3 MB RAM, 157.4 KB PDF. Fastest Node.js/browser JS engine in stress test (120 ms for 22p, 3.5 ms single-page, 98.3 MB RAM); custom TTFs require base64 VFS.
  • pdfmake — 189 ms, 183.7 MB RAM, 165.5 KB PDF. Declarative JSON tables & automatic pagination in 189 ms (27p, 165.5 KB; 4.4 ms single-page); VFS fonts add weight to browser bundles.
  • PDFKit (Node.js) — 140 ms, 125.5 MB RAM, 162.6 KB PDF. Fastest server-only Node.js stream writer on single-page invoices (2.8 ms / 2.6 KB; 140 ms / 125.5 MB RAM across 22p).
  • Playwright — 883 ms, 203.8 MB RAM, 452.2 KB PDF. Beat Puppeteer on pure PDF render speed (883 ms vs 1.35s), RAM (204 vs 263 MB), and PDF size (452 vs 841 KB); Chromium-only.
  • HTML5 & CSS Paged Media — 818 ms, 196.3 MB RAM, 453.6 KB PDF. Renderer-agnostic HTML5 + @page CSS; warm Chromium print finishes 19 pages in 818 ms (453.6 KB; 581 ms single-page).

See the full 22-library benchmark rankings · Compare all supported PDF libraries