What is PDFKit?
PDFKit is a powerful library for generating PDF documents in JavaScript (Node.js). It allows you to create complex multi-page PDF files with text, images, tables, vector graphics, and annotations. PDFKit fully implements the PDF 1.7 specification and requires no external dependencies, making it an ideal choice for server-side Node.js applications.
Why is PDF generation needed in JavaScript?
Server-side PDF generation is in demand in various scenarios: creating invoices and receipts, generating reports, producing certificates, and exporting data to a portable format. PDFKit allows you to automate these processes, working entirely in the Node.js environment without a browser. You can dynamically create documents, insert data from a database, and add watermarks and signatures.
Installing PDFKit
Installing the library is simple and is done via npm:
npm install pdfkitIt is also recommended to install pdfkit-table for convenient work with tables or svg-to-pdfkit for inserting SVG graphics.
Main Features of PDFKit
- Text and Fonts: Unicode support, font embedding, alignment, line wrapping, kerning.
- Vector Graphics: lines, rectangles, circles, Bezier curves, gradients.
- Images: inserting JPEG, PNG, SVG (via plugin).
- Tables: creating tables with auto-width columns, styles, and headers.
- Annotations: links, bookmarks, metadata (author, subject), watermarks.
- Pages: adding, configuring size, orientation, margins, numbering.
Code Example in JavaScript (Node.js)
const PDFDocument = require('pdfkit');const fs = require('fs');
// Create a documentconst doc = new PDFDocument();
// Save to a fileconst stream = fs.createWriteStream('output.pdf');doc.pipe(stream);
// Add a titledoc.fontSize(25).text('Hello, World!', 100, 100);
// Add text with line wrappingdoc.fontSize(12).text('This is an example PDF created with PDFKit.', 100, 150);
// Draw a linedoc.moveTo(100, 200).lineTo(400, 200).stroke();
// Insert an image (if available)// doc.image('path/to/image.png', 100, 250, {width: 200});
// New pagedoc.addPage();doc.fontSize(20).text('Page 2');
// Add a table (via pdfkit-table)// doc.table({ headers: ['Name', 'Age'], rows: [['Anna', 25], ['Ivan', 30]] });
// Finalize the documentdoc.end();
stream.on('finish', () => { console.log('PDF created successfully');});When to use PDFKit?
PDFKit is suitable for tasks where you need full control over the PDF content: from simple text documents to complex reports with charts, tables, and watermarks. It is faster and lighter than Puppeteer if HTML layout is not needed. Use PDFKit if you are creating documents on a Node.js server and want to avoid launching a browser. For simple tasks where HTML-to-PDF is needed, Puppeteer is a better fit.
Also in library
SwiftyJSON for Swift: Complete Guide with Examples
Working with Excel in Java: Apache POI and Alternatives
TBB (Intel Threading Building Blocks) for C++: Complete Guide with Examples
GraphQL in PHP: webonyx/graphql-php library for creating APIs
Node.js Monitoring: Tools and Libraries for Performance Tracking