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

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): A Complete Guide to Libraries and Tools



Generating PDF documents is one of the most in-demand tasks in web development. From creating invoices and reports to generating complex multi-page documents, Node.js offers powerful tools for working with PDFs. In this article, we will review the main libraries and approaches for creating PDF files on the server-side of JavaScript.



Why Generate PDF on the Server?

Server-side PDF generation allows you to:

  • Create documents dynamically based on data from a database
  • Ensure formatting consistency
  • Work with large volumes of data without client-side load
  • Integrate with existing reporting systems


Main Libraries for PDF Generation in Node.js



1. pdfkit — Classic Solution

pdfkit is one of the most popular libraries for creating PDFs from scratch. It provides full control over the document content: text, images, tables, vector graphics.



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

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

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

// Draw a linedoc.moveTo(100, 150).lineTo(400, 150).stroke();

// Insert an imagedoc.image('logo.png', 100, 200, { width: 100 });

doc.end();


Pros: lightweight, fast, supports Unicode and Cyrillic, many fonts.

Cons: requires manual layout setup, no built-in HTML support.



2. Puppeteer — Generation from HTML

Puppeteer is a library for controlling a headless Chrome browser. It allows you to convert HTML pages to PDF while preserving all styles and scripts.



const puppeteer = require('puppeteer');

async function generatePDF() { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.setContent('<h1 style="color: red;">PDF Title</h1><p>Document text</p>'); await page.pdf({ path: 'output.pdf', format: 'A4', margin: { top: '20px', bottom: '20px' } }); await browser.close();}

generatePDF();


Pros: ideal for complex HTML layouts, supports CSS, JavaScript, fonts.

Cons: heavy (requires Chromium), slower than other libraries.



3. pdf-lib — Modifying Existing PDFs

pdf-lib specializes in editing existing PDF documents. It allows you to add text, images, and fill forms.



const { PDFDocument } = require('pdf-lib');const fs = require('fs');

async function editPDF() { const existingPdfBytes = fs.readFileSync('template.pdf'); const pdfDoc = await PDFDocument.load(existingPdfBytes); const pages = pdfDoc.getPages(); const firstPage = pages[0]; firstPage.drawText('New text', { x: 50, y: 50, size: 20, color: rgb(0, 0, 0) }); const pdfBytes = await pdfDoc.save(); fs.writeFileSync('modified.pdf', pdfBytes);}

editPDF();


Pros: lightweight, no external dependencies, works great with forms.

Cons: limited capabilities for creating complex layouts from scratch.



4. jsPDF — Client-Server Solution

jsPDF was originally created for the browser but also works in Node.js. Suitable for simple documents.



const { jsPDF } = require("jspdf");

const doc = new jsPDF();doc.text("Hello world!", 10, 10);doc.save("output.pdf");


Pros: simple API, works on client and server.

Cons:

Recommendations