PDF Generation in JavaScript (Node.js): Libraries and Tools

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

PDF Generation in JavaScript (Node.js): Overview of Libraries and Tools



Creating PDF documents is one of the common tasks in web development. Whether it's generating invoices, reports, certificates, or simply exporting data, Node.js offers several powerful solutions. In this article, we will look at the main libraries for working with PDF on the server-side of JavaScript, their capabilities, and usage examples.



Why Generate PDF on the Server?

Generating PDF on the server (in Node.js) offers several advantages:

  • Consistency — the document looks the same on all devices
  • Security — data does not leave the server
  • Automation — PDFs can be created on a schedule or upon certain events
  • Support for complex logic — access to databases, file systems, and other resources


Main Libraries for PDF Generation in Node.js



1. PDFKit

PDFKit is the most popular library for creating PDFs "from scratch". It requires no external dependencies and allows full control over the document's content.

Usage example:

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

const doc = new PDFDocument();doc.pipe(fs.createWriteStream('output.pdf'));

// Adding contentdoc.fontSize(25).text('Hello, world!', 100, 100);doc.fontSize(12).text('This is a PDF created with PDFKit.', 100, 150);

// Drawing a linedoc.moveTo(100, 200).lineTo(500, 200).stroke();

// Finalizing the documentdoc.end();

Capabilities:

  • Adding text with different fonts and sizes
  • Inserting images (JPEG, PNG)
  • Drawing lines, rectangles, and circles
  • Support for links and bookmarks
  • Creating multi-page documents


2. Puppeteer

Puppeteer is a tool for controlling the Chrome/Chromium browser. It allows generating PDFs from HTML pages, which is convenient for complex layouts.

Usage example:

const puppeteer = require('puppeteer');

async function generatePDF() { const browser = await puppeteer.launch(); const page = await browser.newPage();

await page.setContent('<h1>Heading</h1><p>Document text</p>'); await page.pdf({ path: 'page.pdf', format: 'A4' });

await browser.close();}

generatePDF();

Features:

  • Support for CSS, JavaScript, and all modern web technologies
  • Ideal for converting HTML templates to PDF
  • Ability to print with specified parameters (format, margins, scale)
  • Requires installing Chromium (~300 MB)


3. jsPDF (with adaptation for Node.js)

jsPDF is a client-side library, but there are wrappers for use in Node.js (e.g., jspdf-autotable).

Example (using jspdf and html2canvas):

const { jsPDF } = require('jspdf');const fs = require('fs');

const doc = new jsPDF();doc.text('Hello from Node.js!', 10, 10);doc.save('output.pdf');

Limitations: jsPDF does not support complex layouts and requires additional libraries for working with HTML.



4. pdfmake

pdfmake is a library based on a declarative description of the document in JSON format.

Example:

const pdfmake = require('pdfmake');const fs = require('fs');

const fonts = { Roboto: { normal: 'fonts/Roboto-Regular.ttf', bold: 'fonts/Roboto-Medium.ttf' }};

const printer = new pdfmake(fonts);

const docDefinition = { content: [ { text:

Recommendations