API Reference

PDFKit

Node.js and browser PDF generation library with streaming output, vector graphics and font embedding.

Official Documentation ↗
CompatibilityThis documentation covers version pdfkit v0.15.x / Node 18 LTS. Check the official docs for the latest API surface.

Installation

npmnpm install pdfkit
pnpmpnpm add pdfkit
yarnyarn add pdfkit

Methods

Overview

PDFKit generates PDFs by writing content to a document that is piped as a stream. It handles text layout, vector shapes, images and font embedding, making it well suited to server-side report and invoice generation.

Core Methods

Create a document, pipe it to a destination, then call drawing methods such as text, image and vector path operations. The cursor advances as content is added, and addPage starts a new page.

Usage Notes

Stream directly into an HTTP response for large documents to keep memory flat. Register fonts up front, and measure text width with widthOfString to lay out columns precisely.

Common Pitfalls

Coordinate origin is bottom-left, which surprises developers used to top-left canvases. Missing font registration causes blank glyphs for non-Latin text. Forgetting doc.end leaves streams open and responses hanging.

FAQ

How do I generate a PDF in an HTTP response?
Pipe the document to the response and set Content-Type: application/pdf with a Content-Disposition filename. Call doc.end when writing is complete so the stream closes.
How do I add multiple pages?
Call doc.addPage when the current page fills up, or let text flow and add pages at known points. Each page keeps its own coordinate system with the origin at the bottom left.
Can I embed custom fonts?
Yes. Register a TTF or OTF file with doc.registerFont and select it by name. Embedding is required for glyphs outside the standard fonts, such as CJK characters.