PDF Generation in TypeScript: Libraries and Tools for Node.js

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 TypeScript: Overview of Libraries and Tools



Creating PDF documents is one of the common tasks in web development. TypeScript, thanks to strict typing and modern capabilities, allows writing reliable code for PDF generation both on the client and server. In this article, we will review popular libraries and tools for PDF generation in TypeScript, their features, and usage examples.



1. PDFKit — Powerful Server-Side Library



PDFKit is one of the most popular libraries for creating PDFs on Node.js. It provides full control over the document structure, supports fonts, images, annotations, and vector graphics. PDFKit works on the server and is ideal for generating complex reports, invoices, or certificates.



Installation and example:



npm install pdfkit @types/pdfkit


import PDFDocument from 'pdfkit';import * as fs from 'fs';

const doc = new PDFDocument();

doc.pipe(fs.createWriteStream('output.pdf'));

doc.fontSize(25).text('Hello, world!', 100, 100);doc.fontSize(12).text('This is a PDF generated using PDFKit and TypeScript.', 100, 150);

doc.end();


PDFKit is great for:



  • Creating PDFs on the server (Express, NestJS)
  • Working with custom fonts and encodings (including Cyrillic)
  • Adding images, tables, and graphic primitives


2. jsPDF — Client-Side PDF Generation



jsPDF is a client-side library for creating PDFs directly in the browser. It is easy to use, supports text, images, annotations, and basic shapes. Types for TypeScript are available via DefinitelyTyped.



Installation:



npm install jspdf @types/jspdf


Example of generating a PDF in the browser:



import { jsPDF } from 'jspdf';

const doc = new jsPDF();

doc.text('Hello, TypeScript!', 10, 10);doc.text('This is a PDF created in the browser.', 10, 20);

doc.save('document.pdf');


jsPDF is suitable for:



  • Downloading PDFs on the client side (reports, receipts)
  • Simple documents without complex layout
  • Rapid prototyping


3. Puppeteer — Generation from HTML



Puppeteer is a library for controlling the Chrome/Chromium browser. It allows converting HTML pages to PDF with full preservation of styles, fonts, and layout. Great for creating complex documents, reports, and business cards.



Installation:



npm install puppeteer


Example of converting HTML to PDF:



import puppeteer from 'puppeteer';

async function generatePDF(html: string, outputPath: string): Promise<void> { const browser = await puppeteer.launch(); const page = await browser.newPage();

await page.setContent(html, { waitUntil: 'networkidle0' }); await page.pdf({ path: outputPath, format: 'A4' });

await browser.close();}

// Usageconst htmlContent = `<h1 style="color: blue;">Heading</h1><p>Text with HTML styles</p>`;generatePDF(htmlContent, 'output.pdf');


Puppeteer is ideal for:



  • Converting complex HTML templates to PDF
  • Creating PDFs with CSS styles, Flexbox, Grid
  • Server-side PDF generation with full control over appearance


4. pdf-lib — Lightweight and Fast Library



pdf-lib is a modern library for creating and modifying PDF documents without external dependencies. It works both in Node.js and in the browser. Supports fonts, images, forms, and annotations.



Installation:



npm install pdf-lib
\

Recommendations