Free PDFKit (Node.js) Templates & Streaming Code Generator

Pure Node.js streaming vector writer — the same low-level engine powering @react-pdf/renderer and pdfmake underneath. Generates single-page invoices in 2.8 ms (2.6 KB) and 22 stress-test pages in 140 ms (125.5 MB RAM, 162.6 KB PDF).

Install: npm install pdfkit@^0.15.2 @types/pdfkit | Runtime: Node.js (TypeScript / JS) | Version: 0.15.2 | Popularity: 2.4M/mo installs

Works with: Node.js, Express, Fastify, NestJS, AWS Lambda

How PDFKit (Node.js) Templates Work in CodePDF

PDFKit streams vector graphics, standard/embedded fonts, and positioned text directly to a Node.js WritableStream or HTTP response without any DOM or Chromium overhead.

  • Produces tiny 3.4 KB PDFs with ~107 MB RAM — ideal for AWS Lambda and high-throughput Node.js microservices
  • Automates table grid lines, rounded cards, pills, QR codes, and multi-page flow offsets
  • Streams directly to fs.createWriteStream or Express/Fastify/NestJS HTTP responses

Quick Start: Generate PDFs with PDFKit

Install PDFKit (Node.js) (TypeScript / JS), then run the code CodePDF exports for any template:

npm install pdfkit@^0.15.2 @types/pdfkit

  1. Install PDFKit: npm install pdfkit@^0.15.2 @types/pdfkit
  2. Save the generated file as generateDocument.ts in your project.
  3. Call generatePDF('output.pdf') or pass your data object as the second argument.
  4. Run with ts-node generateDocument.ts or compile first with tsc.

Example PDFKit Output (Standard B2B Commercial Invoice)

Compiled directly from the Standard B2B Commercial Invoice template (TypeScript / JS):

import PDFDocument from 'pdfkit';
import fs from 'fs';

// This code is generated using codepdf.dev

interface DocumentData {
  vendorCompany: string;
  invoiceNumber: string;
  vendorAddress: string;
  invoiceDate: string;
  paymentDueDate: string;
  grandTotalNum: number;
  clientCompany: string;
  clientDetails: string;
  lineItems: Array<Record<string, string>>;
  wireBank: string;
  paymentTerms: string;
  subtotalAmount: string;
  taxAmount: string;
  grandTotal: string;
  paymentMethods: string[];
}

export const SAMPLE_DATA: DocumentData = {
  vendorCompany: 'MERIDIAN CLOUD INFRASTRUCTURE LLC',
  invoiceNumber: 'INV-2026-8842',
  vendorAddress: '100 Montgomery St, Suite 2400, San Francisco, CA 94104 · EIN: 84-2938192',
  invoiceDate: '12 Sep 2026',
  paymentDueDate: '12 Oct 2026 (Net 30)',
  grandTotalNum: 26674.38,
// … (561 more lines in full generated file)

Custom Fonts in PDFKit

Register TTF, OTF, or WOFF aliases via doc.registerFont(name, path) or pass paths directly to doc.font().

  1. Place .ttf or .otf files inside ./fonts/ on disk.
  2. Call doc.registerFont('{{FONT_FAMILY}}', '{{FONT_LOCAL_PATH}}') after constructing PDFDocument.
  3. Call doc.font('{{FONT_FAMILY}}') prior to drawing text.

Official documentation: PDFKit docs

PDFKit Benchmark Results

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

PDFKit (Node.js) stress-test results and rank out of 22 PDF libraries
MetricPDFKitRank (of 22)
Wall-clock time140 ms8th fastest
CPU time0.52 s9th lowest
Peak RAM125.5 MB13th lowest
PDF file size162.6 KB13th smallest
Engine type
Coordinate Canvas
Under the hood
Pure Node.js streaming PDF vector writer (also powers pdfmake & react-pdf underneath)
Runs in
Node.js Server (Express, Fastify, NestJS, Lambda)
Verdict
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).

Under the Hood & Benchmark Highlights

  • The foundational Node.js PDF writer — both @react-pdf/renderer and pdfmake rely on PDFKit underneath to serialize actual PDF streams.
  • Fastest Node.js library in both our single-page baseline (492 ms, 3.4 KB) and 11-page 26-component stress test (812 ms, 125.3 KB, 141 MB RAM — 3.5× faster than Puppeteer).
  • Pipes bytes directly into Node WritableStreams or HTTP responses without buffering an entire browser DOM.

Good to Know (PDFKit Caveats)

  • Node.js server-only: Relies on Node streams and filesystem APIs — not supported in Cloudflare Workers/Edge runtimes, and browser builds require heavy bundler polyfills.
  • Coordinate-based streaming API: No built-in table or Flexbox layout engine; CodePDF generates vector table grids and manual page-break tracking.
  • Unicode & custom fonts: Built-in Helvetica/Times/Courier are WinAnsi-only; register local .ttf/.otf/.woff files via doc.registerFont() for non-Latin scripts.

Popular PDFKit PDF Templates

All 100 CodePDF templates export to PDFKit (Node.js). 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.

PDFKit Template FAQs

Why choose PDFKit over Puppeteer in Node.js?

PDFKit uses ~5x less memory, starts in milliseconds without a 400MB Chromium binary, and produces 3–10KB vector PDFs compared to 200KB+ headless browser prints.

How does CodePDF handle tables in PDFKit?

CodePDF compiles visual tables into clean row-iteration loops using PDFKit's vector rect/moveTo/lineTo primitives and text cell bounds, with automatic page breaks.

Can I stream generated PDFKit templates directly from an Express or NestJS endpoint?

Yes. Simply pipe the PDFDocument instance directly to res (doc.pipe(res)) instead of a file stream.

PDFKit 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.
  • 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).
  • 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