What is PDFKit and why is it needed?
\n\nPDFKit 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\nMain 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
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\nInstalling PDFKit
\n\nPDFKit is distributed via the npm package manager. To install it, run the following command in the terminal in the root of your project:
\n\nnpm install pdfkit\n\nAfter installation, the library will be available for import in your code. No additional dependencies are required.
\n\nQuick Start: Creating Your First PDF Document
\n\nLet's create a simple PDF file with the text "Hello, World!".
\n\nconst 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\nAfter running this code, a file named hello_world.pdf with the text "Hello, World!" will appear in the same folder.
Main Methods and Functions of PDFKit
\n\nPDFKit provides an extensive API. Below are 10 key methods with detailed descriptions and examples.
\n\n1. text(text, x, y, options)
\nDescription: Adds text to the document. This is the main method for working with text. It supports line breaks, alignment, indentation, and styling.
\nParameters:
\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 <
Also in library
Go Application Configuration: The Viper Library
Mongoose for JavaScript (Node.js): Complete Guide with Examples
QR Code Generator in Rust: Creation and Integration into Web Applications