Introduction
jsPDF is a JavaScript library that creates PDF files entirely on the client side. It lets developers generate invoices, reports, and other documents without sending data to a server, keeping sensitive information in the browser and reducing backend load.
What jsPDF Does
- Creates multi-page PDF documents from JavaScript code
- Adds text, images, shapes, and vector graphics to pages
- Supports custom fonts via a font converter plugin
- Integrates with the html2canvas or jsPDF-AutoTable plugins for HTML-to-PDF and table rendering
- Works in modern browsers and Node.js environments
Architecture Overview
jsPDF builds an in-memory representation of the PDF document structure. Each API call (text, image, rect, line) appends objects to an internal content stream following the PDF specification. When save() or output() is called, jsPDF serializes the object tree into a valid PDF byte stream. Plugins extend the core by hooking into lifecycle events to add capabilities like table layout or HTML conversion.
Setup & Configuration
- Install via npm (
npm install jspdf) or load from a CDN script tag - Import as an ES module or use the global
jsPDFconstructor - Set page size and orientation in the constructor:
new jsPDF('p', 'mm', 'a4') - Add plugins like jspdf-autotable for structured table output
- Use
addFont()to register custom TTF fonts for non-Latin scripts
Key Features
- Pure client-side PDF generation with no server dependency
- Plugin ecosystem for tables, HTML conversion, and SVG support
- Supports standard page sizes (A4, Letter) and custom dimensions
- Produces compact PDF output with optional compression
- Mature project with a large community and extensive documentation
Comparison with Similar Tools
- pdf-lib — lower-level API for modifying existing PDFs; jsPDF focuses on creating new documents from scratch
- pdfmake — uses a declarative document-definition approach; jsPDF uses an imperative drawing API
- Puppeteer PDF — generates PDFs by rendering HTML in a headless browser; heavier setup than jsPDF
- React-PDF (renderer) — React-specific; jsPDF is framework-agnostic
FAQ
Q: Can jsPDF handle complex HTML layouts?
A: Use the html2canvas plugin or jsPDF's .html() method, though CSS support is partial. For pixel-perfect HTML-to-PDF, a headless browser approach may be more reliable.
Q: Does jsPDF support Unicode and non-Latin text?
A: Yes, after registering a TTF font via addFont(). The default built-in fonts cover only basic Latin characters.
Q: What is the maximum file size jsPDF can produce? A: There is no hard limit, but very large documents may hit browser memory constraints. Generating pages incrementally helps manage memory.
Q: Is jsPDF suitable for server-side generation? A: It works in Node.js, though libraries like PDFKit or pdf-lib are more commonly chosen for server-side workflows.