Free Playwright HTML-to-PDF Templates & TypeScript Generator

Automates Headless Chromium via Playwright for modern HTML5/CSS3 print-to-PDF. Outperformed Puppeteer on pure PDF generation speed (883 ms vs. 1.35s), memory (203.8 vs. 263.2 MB), and file size (452.2 vs. 841.2 KB) across our 19-page stress test.

Install: npm install playwright@^1.53.0 | Runtime: Node.js (TypeScript + HTML/CSS) | Version: 1.53.0 | Popularity: 24.5M/mo installs

Works with: Node.js, TypeScript, CI Pipelines, Docker

How Playwright Templates Work in CodePDF

Playwright renders self-contained HTML5 + @page CSS in headless Chromium and exports A4/Letter PDFs with full web font, SVG, and CSS Flexbox support.

  • Reuses Playwright headless Chromium with faster render times and smaller output than default Puppeteer print scripts
  • Uses Mustache ({{var}}) interpolation inside self-contained HTML + @page CSS paired with a typed Playwright TypeScript runner
  • Exact A4 and US Letter @page margin synchronization so pages never clip unexpectedly

Quick Start: Generate PDFs with Playwright

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

npm install playwright@^1.53.0

  1. Run npm install playwright@^1.53.0 in your project root.
  2. Install the browser: npx playwright install chromium.
  3. Save the generated file as renderDocument.ts.
  4. Run node renderDocument.ts to produce document.pdf.
  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 Playwright Output (Standard B2B Commercial Invoice)

Compiled directly from the Standard B2B Commercial Invoice template (TypeScript + HTML/CSS):

import { chromium } from 'playwright';
import { pathToFileURL } from 'node:url';

// This code is generated using codepdf.dev

// Renders the CodePDF HTML + CSS document with Playwright.
// 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>Standard B2B Commercial 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; }
// … (312 more lines in full generated file)

Custom Fonts in Playwright

Include CSS @font-face rules and wait for document.fonts.ready before invoking Chromium page.pdf().

  1. Add @font-face rules with HTTPS URLs, local server URLs, or base64 font data.
  2. Await page.evaluate(() => document.fonts.ready) prior to page.pdf().
  3. Pass printBackground: true to page.pdf().

Official documentation: Playwright docs

Playwright Benchmark Results

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

Playwright stress-test results and rank out of 22 PDF libraries
MetricPlaywrightRank (of 22)
Wall-clock time883 ms17th fastest
CPU time2.13 s18th lowest
Peak RAM203.8 MB19th lowest
PDF file size452.2 KB19th smallest
Engine type
Browser (Chromium)
Under the hood
Drives Headless Chromium via Playwright automation and calls page.pdf()
Runs in
Node.js Server / Docker / CI Pipelines
Verdict
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.

Under the Hood & Benchmark Highlights

  • Like Puppeteer, Playwright spawns a real Headless Chromium browser to paint HTML/CSS and export PDFs — though page.pdf() only works on Chromium (not Firefox or WebKit).
  • Outperformed Puppeteer across every metric on our 10-page 26-component test: 33% faster (1.95s vs. 2.89s), 24% less RAM (192.2 MB vs. 252.1 MB), and 35% smaller PDF (296.0 KB vs. 454.0 KB).
  • Still carries Chromium binary deployment overhead compared to native engines like PDFKit (812 ms, 125 KB).

Good to Know (Playwright Caveats)

  • Chromium-only PDF API: Playwright's page.pdf() works only in headless Chromium (not Firefox or WebKit) and requires npx playwright install chromium.
  • Server-only: Requires OS browser dependencies in Docker (npx playwright install-deps chromium) and does not run in browsers or Edge Workers.
  • Dynamic bindings use Mustache: Install npm install mustache when exporting templates with dynamic {{variable}} fields.

Popular Playwright PDF Templates

All 100 CodePDF templates export to Playwright. 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.

Playwright Template FAQs

Does Playwright page.pdf() work in headless mode?

Yes. Playwright's page.pdf() runs natively in headless Chromium with printBackground: true and preferCSSPageSize enabled in CodePDF's generated code.

How is dynamic JSON data passed into Playwright PDF templates?

Dynamic {{variable}} placeholders and repeating sections are rendered with Mustache (npm install mustache) before calling page.setContent() and page.pdf().

Can I preview Playwright templates visually before running code?

Yes. Every template opens in CodePDF's browser studio with instant live PDFium preview and sample JSON editing.

Playwright 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).
  • Puppeteer — 1.35 s, 263.2 MB RAM, 841.2 KB PDF. 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).
  • 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