Introduction
pdfmake is a JavaScript library for generating PDF documents on both client and server side. Instead of manually positioning elements on a page, developers describe documents with a declarative JSON-like definition, and pdfmake handles layout, pagination, and rendering automatically.
What pdfmake Does
- Generates multi-page PDF documents from declarative JavaScript objects
- Runs identically in the browser (no server round-trip) and in Node.js
- Handles automatic page breaks, headers, footers, and page numbering
- Supports tables with column spans, row spans, and auto-width calculations
- Embeds custom fonts, images (including SVG), and vector graphics
Architecture Overview
pdfmake consists of a layout engine and a PDF writer. The layout engine takes a document-definition object, measures text runs using font metrics, calculates column and row dimensions for tables, and determines page breaks. The PDF writer then serializes the laid-out nodes into a valid PDF byte stream. In the browser, it uses an in-memory virtual file system for font files; in Node.js, it reads fonts from disk. The output can be returned as a buffer, base64 string, or piped to a writable stream.
Self-Hosting & Configuration
- Install via npm for Node.js projects or load the CDN build for browser use
- Register custom fonts by providing TTF files through the virtual file system API
- Configure default styles, page size (A4, Letter, custom), and margins in the document definition
- Use the Node.js buffer output for server-side generation and HTTP response streaming
- No external dependencies beyond the bundled font files
Key Features
- Declarative API eliminates manual coordinate math and page management
- Full table support with automatic column width calculation and nested tables
- Watermarks, backgrounds, headers, and footers with page-aware content
- Client-side generation keeps data private and reduces server load
- Consistent rendering across browser and Node.js environments
Comparison with Similar Tools
- jsPDF — imperative API with manual positioning; pdfmake offers declarative layout
- PDFKit — powerful low-level Node.js library; pdfmake adds automatic pagination and table layout
- Puppeteer/Playwright — render HTML to PDF via a headless browser; pdfmake generates PDFs natively without a browser engine
- WeasyPrint — Python HTML-to-PDF; pdfmake is JavaScript-native and runs client-side
- React-PDF — React component for rendering PDFs; pdfmake is framework-agnostic
FAQ
Q: Can pdfmake run in the browser without a server? A: Yes. The entire PDF is generated client-side in JavaScript. No server communication is needed.
Q: How do I use custom fonts? A: Convert TTF files to a virtual file system module using the provided build tool, then register them in the fonts configuration.
Q: Does it support right-to-left text? A: Basic RTL support exists, but complex scripts like Arabic may need careful font selection and testing.
Q: What is the maximum document size? A: There is no hard limit. Performance depends on available memory; documents with thousands of pages work in Node.js but may be slow in browsers.