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

What is PDFKit and why is it needed?

\n\n

PDFKit is a powerful and flexible library for JavaScript (Node.js) designed for programmatic creation of PDF documents "on the fly". It allows developers to generate complex, multi-page PDF files with rich content: text, images, tables, vector graphics, annotations, and forms. Unlike many other libraries, PDFKit works at a low level, providing full control over every aspect of the document, from element positioning coordinates to font embedding.

\n\n

Main use cases for PDFKit:

\n
    \n
  • Report and invoice generation: Creating dynamic PDF documents based on data from a database or API.
  • \n
  • Creating invoices and receipts: Automatic document generation for online stores and accounting systems.
  • \n
  • Generating certificates and diplomas: Creating personalized documents with unique data for each recipient.
  • \n
  • Data export: Converting web pages, tables, or charts to PDF format for printing or archiving.
  • \n
  • Creating e-books and manuals: Programmatic layout of multi-page documents with a table of contents and bookmarks.
  • \n
\n\n

PDFKit is the choice for developers who need performance, flexibility, and full control without using external dependencies (for example, without needing to install LibreOffice or wkhtmltopdf).

\n\n

Installing PDFKit

\n\n

PDFKit is distributed via the npm package manager. To install it, run the following command in the terminal in the root of your project:

\n\n
npm install pdfkit
\n\n

After installation, the library will be available for import in your code. No additional dependencies are required.

\n\n

Quick Start: Creating Your First PDF Document

\n\n

Let's create a simple PDF file with the text "Hello, World!".

\n\n
const PDFDocument = require('pdfkit');\nconst fs = require('fs');\n\n// Create a new PDF document\nconst doc = new PDFDocument();\n\n// Specify the stream for writing the result\ndoc.pipe(fs.createWriteStream('hello_world.pdf'));\n\n// Add text to the page\ndoc.fontSize(25)\n   .text('Hello, World!', 100, 100);\n\n// Finalize the document and end the stream\ndoc.end();\n\nconsole.log('PDF file "hello_world.pdf" successfully created!');
\n\n

After running this code, a file named hello_world.pdf with the text "Hello, World!" will appear in the same folder.

\n\n

Main Methods and Functions of PDFKit

\n\n

PDFKit provides an extensive API. Below are 10 key methods with detailed descriptions and examples.

\n\n

1. text(text, x, y, options)

\n

Description: Adds text to the document. This is the main method for working with text. It supports line breaks, alignment, indentation, and styling.

\n

Parameters:

\n
    \n
  • text (String) — The text to add.
  • \n
  • x (Number, optional) — X coordinate. If not specified, text continues from the current position.
  • \n
  • y (Number, optional) — Y coordinate. If not specified, text continues from the current position.
  • \n
  • options (Object, optional) — An object with settings:\n
      \n
    • align (String) — Alignment: 'left', 'center', 'right', 'justify'.
    • \n
    • indent (Number) — First line indent.
    • \n
    • paragraphGap (Number) — Space between paragraphs.
    • \n
    • lineGap (Number) — Line spacing.
    • \n
    • width (Number) — Width of the text block (for line wrapping).
    • \n
    • height (Number) — Height of the text block.
    • \n <

Recommendations