npm package
jspdf review
Use jsPDF for a PDF template whose positions you can own and check.
A receipt with a fixed heading and a few known fields is a good reason to consider jsPDF. jsPDF generates PDFs in JavaScript through explicit document commands: create a page, place text at coordinates and save the result. Its version-matched README documents browser and Node usage. The useful trade is control over a small layout, with the responsibility to decide where content goes. This guide covers jsPDF 4.2.1 and keeps the examples focused on that trade.
Use jsPDF for a PDF template whose positions you can own and check. Build one representative page with the longest fields and required glyphs before committing the rest of your export workflow.
Answers from our run
jspdf or pdf-lib: which should you use?
pdf-lib: Choose pdf-lib when you need to create and modify PDF documents. Its pinned README makes that scope explicit; this guide hasn't tested a migration or compared output quality. Use jsPDF for a PDF template whose positions you can own and check.
When should you not use jspdf?
Your main job is modifying an existing PDF. Evaluate pdf-lib's create-and-modify workflow before building around this generation guide.
Use it if
Skip it if
Setup reality
Install the discussed release with npm install jspdf@4.2.1. In a browser module, use import { jsPDF } from 'jspdf'. The pinned README also demonstrates const { jsPDF } = require('jspdf') for Node. Its default document is A4 portrait with millimeters as the coordinate unit. Set the format and units explicitly in a reusable template so a later reader doesn't have to remember the defaults.
No package installation, snippet execution, generated-PDF inspection or browser compatibility test was performed for this guide. These are documentation-based, unexecuted illustrations checked against the 4.2.1 README and TypeScript declarations. The declarations establish API shapes, not whether your fonts, bundler and output viewer produce the document you want.
Start with one fixed page. Decide which fields may grow, reserve space for them and check unusually long names before adding more decoration. The snippets use short ASCII strings and deliberately chosen coordinates. They don't implement invoice calculations, text overflow handling or automatic pagination. A real billing template also needs business rules that this PDF library does not supply through these drawing calls.
Fonts are a separate setup task. For UTF-8 text, the README calls for a custom TTF with the required glyphs and shows registration through addFileToVFS, addFont and setFont. Loading a font doesn't establish that it contains your customer's characters. Check the actual names and symbols your document must contain. The font example below accepts bytes you have already obtained rather than pretending that a font asset comes with the snippet.
The html method takes a different dependency path: the README names html2canvas and, for a string HTML document, dompurify, loaded when required. Check your build output if you use that route. Node file reads have their own documented restrictions and permission guidance. The examples here avoid reading external image or font paths in Node. The Node save example writes into the working directory, so use a deliberate output location and disposable data when trying it.
The 4.2.1 release notes report fixes for HTML injection in output methods and PDF object injection through free text annotation color. That's evidence of repairs in this release, not a safety guarantee for arbitrary input. Follow the README's advice to sanitize user input.
The scores use a 1-5 editorial rubric: 1 means weak supporting evidence, 3 means documented capability with material limits and 5 requires unusually complete evidence. API stability considers published contracts, documentation quality considers usable instructions and caveats, maintenance considers recorded release work, and ecosystem considers integration paths. These ratings are source assessments, not measured compatibility, adoption or performance scores.
Patterns
Download a small browser document browser-download
import { jsPDF } from 'jspdf'
const doc = new jsPDF()
doc.text('Receipt R-104', 15, 20)
doc.save('receipt.pdf')Unexecuted browser illustration. The default coordinate unit is millimeters. Keep the download behind an explicit user action in your application and inspect the resulting file.
Save a document from Node node-file
const { jsPDF } = require('jspdf')
const doc = new jsPDF()
doc.text('Receipt R-104', 15, 20)
doc.save('receipt.pdf')The pinned README uses this CommonJS import for Node and says save writes to the current working directory. Use a disposable directory for the first run. This example has not been executed.
Declare a fixed page template explicit-page
import { jsPDF } from 'jspdf'
const doc = new jsPDF({
orientation: 'portrait',
unit: 'mm',
format: 'a4'
})
doc.text('Certificate', 20, 30)
doc.save('certificate.pdf')Explicit constructor options make the template's coordinate system visible. This doesn't add layout rules for growing content. The illustration is based on the pinned constructor documentation and declarations.
Choose a small page in inches custom-label
import { jsPDF } from 'jspdf'
const doc = new jsPDF({
orientation: 'landscape',
unit: 'in',
format: [4, 2]
})
doc.text('Parcel A', 0.25, 0.5)
doc.save('label.pdf')This follows the README's custom-size constructor shape. Coordinates are now inches, so copying millimeter positions into this template is a mistake. Check the actual printed dimensions before using it for labels.
Set a heading font before drawing it text-style
import { jsPDF } from 'jspdf'
const doc = new jsPDF()
doc.setFont('helvetica', 'bold')
doc.setFontSize(18)
doc.text('Receipt', 15, 20)
doc.setFont('helvetica', 'normal')
doc.setFontSize(11)
doc.text('Reference: R-104', 15, 32)
doc.save('heading.pdf')The versioned declarations expose setFont, setFontSize and text. This illustration resets the style explicitly before the next field. It does not demonstrate support for non-ASCII text.
Add a separate page deliberately explicit-second-page
import { jsPDF } from 'jspdf'
const doc = new jsPDF({ unit: 'mm', format: 'a4' })
doc.text('Receipt', 15, 20)
doc.addPage('a4', 'portrait')
doc.text('Terms', 15, 20)
doc.save('receipt-with-terms.pdf')addPage is declared with optional format and orientation arguments. This is explicit page creation, not an automatic overflow solution. The example is unexecuted and uses fixed content on each page.
Return PDF bytes to another layer binary-output
import { jsPDF } from 'jspdf'
const doc = new jsPDF()
doc.text('Receipt R-104', 15, 20)
const bytes = doc.output('arraybuffer')
console.log(bytes.byteLength)The pinned declarations give output('arraybuffer') an ArrayBuffer return type. This illustration stops at serialization. Uploading, retaining and serving those bytes need separate application code, and no output size is claimed here.
Register a TTF already loaded by the caller register-font
import { jsPDF } from 'jspdf'
function makeDocument(fontBinaryString) {
const doc = new jsPDF()
doc.addFileToVFS('ReceiptFont.ttf', fontBinaryString)
doc.addFont('ReceiptFont.ttf', 'ReceiptFont', 'normal')
doc.setFont('ReceiptFont')
doc.text('Receipt', 15, 20)
return doc
}The caller must provide actual TTF content as a binary string, following the pinned README. The filename and family name are application choices. No font asset is bundled here, and registration alone doesn't prove that the font contains every required glyph.
Alternatives
| Package | Registry | Pick it when |
|---|---|---|
| pdf-lib | npm | Choose pdf-lib when you need to create and modify PDF documents. Its pinned README makes that scope explicit; this guide hasn't tested a migration or compared output quality. |
| pdfkit | npm | Choose PDFKit when documented line wrapping and piped document output fit your Node generation task. Its pinned README shows both. The existing [PDFKit guide](https://mrkeyoor.com/libs/pdfkit/) covers that workflow in more detail. |
More infra guides
How this guide is made: grounded in the library's documentation, release notes, changelog, and issue history, on a fixed rubric — not a hands-on install of every release. The 50 most-downloaded entries are additionally install-verified in clean containers. Corrections: contact the desk.