# PDFKit — JavaScript PDF Generation for Node and Browser > A JavaScript library for generating complex, multi-page PDF documents in Node.js and the browser. ## Install Save in your project root: # PDFKit — JavaScript PDF Generation for Node and Browser ## Quick Use ```bash npm install pdfkit ``` ```js import PDFDocument from 'pdfkit'; import fs from 'fs'; const doc = new PDFDocument(); doc.pipe(fs.createWriteStream('output.pdf')); doc.fontSize(25).text('Hello, PDFKit!', 100, 100); doc.addPage().text('Page two content'); doc.end(); ``` ## Introduction PDFKit is a JavaScript PDF generation library for Node.js and the browser. It provides a rich API for creating complex, multi-page documents with text, vector graphics, images, and embedded fonts. Unlike template-based PDF tools, PDFKit gives you full programmatic control over every element on the page, making it well suited for invoices, reports, certificates, and dynamic document workflows. ## What PDFKit Does - Creates multi-page PDF documents from JavaScript code - Renders text with custom fonts, sizes, colors, and alignment - Draws vector graphics including lines, curves, rectangles, and paths - Embeds PNG, JPEG, and other image formats at specified positions - Supports font subsetting and Unicode text rendering ## Architecture Overview PDFKit generates PDF files by writing PDF objects directly to an output stream. It maintains an internal document tree of pages, fonts, images, and content streams. Text layout is handled by a built-in line-breaking algorithm that supports word wrap, alignment, and list formatting. The library uses a streaming architecture, so documents can be piped to files, HTTP responses, or blob URLs without buffering the entire PDF in memory. ## Self-Hosting & Configuration - Install via npm: `npm install pdfkit` - For browser usage, bundle with webpack or another bundler and use Blob URLs - Register custom fonts: `doc.registerFont('CustomFont', './path/to/font.ttf')` - Set page size and margins: `new PDFDocument({ size: 'A4', margin: 50 })` - Enable automatic page breaks with the text layout engine or add pages manually ## Key Features - Streaming PDF generation with low memory footprint - Built-in text layout with word wrap, columns, and lists - Vector graphics API mirroring HTML5 Canvas drawing commands - TrueType and OpenType font embedding with automatic subsetting - AcroForm support for interactive PDF form fields ## Comparison with Similar Tools - **jsPDF** — lighter API for simpler PDFs; PDFKit has richer text layout and vector graphics - **pdf-lib** — focused on modifying existing PDFs; PDFKit is designed for creating new documents - **Puppeteer** — renders HTML to PDF via a headless browser; PDFKit generates PDFs programmatically without a browser - **ReportLab (Python)** — the Python equivalent; PDFKit brings similar capabilities to the JavaScript ecosystem ## FAQ **Q: Can PDFKit run in the browser?** A: Yes. Bundle it with a tool like webpack and use `blobStream()` to generate a downloadable PDF in the browser. **Q: How do I add images to a PDF?** A: Use `doc.image('path/to/image.png', x, y, { width: 200 })` to embed PNG or JPEG images. **Q: Does PDFKit support tables?** A: There is no built-in table API. Use the text positioning and vector graphics API to draw table structures, or use community plugins like pdfkit-table. **Q: Can I use Google Fonts or web fonts?** A: Download the TTF/OTF file and register it with `doc.registerFont()`. Direct web font URLs are not supported. ## Sources - https://github.com/foliojs/pdfkit - https://pdfkit.org/ --- Source: https://tokrepo.com/en/workflows/asset-3cdfdc3d Author: AI Open Source