PDFKit for JavaScript (Node.js): Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to PDFKit

PDFKit is a powerful and flexible library for generating PDF documents on the server side using JavaScript (Node.js). It gives developers full control over the PDF creation process — from simple text documents to complex multi-page reports with tables, images, graphics, and interactive elements. PDFKit requires no additional dependencies like PhantomJS or Puppeteer and runs purely on JavaScript, making it an ideal choice for high-traffic server applications.

Why do you need this library? In modern web development, you often need to generate PDF documents programmatically: invoices, packing slips, reports, certificates, contracts. PDFKit solves this problem elegantly and efficiently. Unlike HTML-to-PDF converters, which can be slow and unpredictable, PDFKit gives you direct access to the PDF model: you define the coordinates of every element, manage fonts, colors, and page layout yourself. This is especially important when you need precise element positioning or work with custom formats.

The library is actively maintained by the community, has extensive documentation, and is used in production by thousands of companies worldwide. PDFKit supports all major PDF features: font embedding (including Cyrillic), image handling (JPEG, PNG, SVG), annotations, hyperlinks, bookmarks, and document encryption. Thanks to its modular architecture and streaming capabilities, PDFKit is perfect for creating both small documents and multi-gigabyte PDF files.

Installing PDFKit

PDFKit is distributed via the npm package manager. Install it with the standard command:

npm install pdfkit

For TypeScript support, you may need to install additional types:

npm install pdfkit @types/pdfkit

If you use Yarn:

yarn add pdfkit

Once installed, the library is ready to use. No additional dependencies or system requirements are needed — it works out of the box on any platform that supports Node.js (Windows, macOS, Linux).

Quick Start: Minimal Working Example

Create a file called hello-pdf.js and add the following code:

const PDFDocument = require('pdfkit');
const fs = require('fs');

// Create a new PDF document
const doc = new PDFDocument();

// Pipe the output to a file
doc.pipe(fs.createWriteStream('hello.pdf'));

// Add some text
doc.fontSize(25)
   .text('Hello, World!', 100, 100);

// Finalize the document
doc.end();

console.log('PDF created: hello.pdf');

Run the script:

node hello-pdf.js

A file named hello.pdf will appear in your current directory with the text "Hello, World!". This example demonstrates the basic workflow with PDFKit: creating a document, adding content, and saving the result.

Core PDFKit Methods and Classes

PDFKit provides a rich API for working with PDF documents. Let's explore the key methods you'll use in everyday development.

1. new PDFDocument(options) — Document Constructor

Signature: new PDFDocument([options])

Description: Creates a new PDF document instance. The options parameter is an object that lets you set page size, margins, orientation, metadata, and other settings.

const doc = new PDFDocument({
  size: 'A4',              // Page size (A4, Letter, Legal, etc.)
  layout: 'portrait',      // Orientation: 'portrait' or 'landscape'
  margins: {               // Margins                    

Recommendations