## Forked by [Cantoo](https://cantoo.fr)
[`@cantoo/pdf-lib`](https://www.npmjs.com/package/@cantoo/pdf-lib) is an actively maintained fork of [`Hopding/pdf-lib`](https://github.com/Hopding/pdf-lib). We keep the original API and docs, and ship the features below that upstream does not (or no longer) provide. Install with:
```bash
npm install @cantoo/pdf-lib
# or
yarn add @cantoo/pdf-lib
```
We maintain this project for our own product needs and cannot guarantee support for every issue outside that roadmap — but contributions that fit the library are welcome.
### Benefits vs original `pdf-lib`
**Drawing & content**
- **Full SVG drawing** (`page.drawSvg` / `embedSvg`) — not just `drawSvgPath`: shapes, text, transforms, viewBox cropping, blend modes, rounded rects, CSS colors via [`culori`](https://culorijs.org/)
- **`PDFPage.extractContents()`** — pull typed assets (text, images, approximate vector graphics as SVG) out of page content streams
- **`characterSpacing`** on `drawText`
**Documents & save**
- **Incremental updates** (`forIncrementalUpdate`, `save({ useObjectStreams… })`, `commit()`) — append changes without rewriting the whole file (signing / multi-increment workflows)
- **PDF header preserved** on save (writers no longer always force `%PDF-1.7`)
- **Recover truncated PDFs** instead of failing hard on a broken trailer object
- More robust parsing (hex-string whitespace, object streams on older PDFs, HTML closing tags spanning lines, case-insensitive PDF names, …)
**Forms & scripts**
- **XFA helpers** — `preserveXFA`, read/modify XFA JavaScript, signature-field discovery (static / government-style XFA)
- Extract / work with **document-level JavaScript**
- **`PDFForm.flatten()`** also flattens orphaned widget annotations not listed in `AcroForm.Fields`
- **Attachments** — add, extract (including unsaved), and detach embedded files; name trees kept in PDF lexical order
**Standards & e-invoicing**
- **`convertToPDFA()`** — PDF/A-1/2/3 structural pieces (OutputIntent + sRGB ICC, `/ID`, XMP synced with Info)
- **`embedFacturX()`** — Factur-X / ZUGFeRD PDF/A-3 hybrid invoices
**Security & crypto**
- **`PDFDocument.encrypt()`** — AES-256 revision 6 by default; AES-128 / RC4 via `algorithm` (RC4 needs `allowWeakCryptography`)
- Decrypt PDFs whose crypt filters are **indirect objects**
- No `crypto-js`: in-tree AES/RC4/MD5/SHA-256 + Web Crypto `getRandomValues`
**Fonts & layers**
- First-class **[`@cantoo/fontkit`](https://www.npmjs.com/package/@cantoo/fontkit)** support (types, default export, UMD); still compatible with upstream `fontkit` / `@pdf-lib/fontkit`
- **`postscriptName`** to pick a face from `.ttc` / `.dfont` collections
- **Optional content (PDF layers)** — `getOptionalContentGroups()` / `setOptionalContentGroupVisibility()`
**Packaging & deps**
- Vendored **[`@cantoo/upng`](./src/packages/upng)** and **[`@cantoo/standard-fonts`](./src/packages/standard-fonts)** (also as `@cantoo/pdf-lib/upng` and `@cantoo/pdf-lib/standard-fonts`)
- Compression via **[`fflate`](https://www.npmjs.com/package/fflate)** instead of `pako` (modular TS UPNG with Photopea `quantize` / `framize` / lossy `cnum`)
- Lighter color parsing with **`culori`**; `html-entities` as a peer (pulled in by the HTML/SVG parser)
See [CHANGELOG.md](CHANGELOG.md) for version-by-version detail.
Create and modify PDF documents in any JavaScript environment.
Designed to work in any modern JavaScript runtime. Tested in Node, Browser, Deno, and React Native environments.
> **Learn more at [pdf-lib.js.org](https://pdf-lib.js.org)**
## Table of Contents
- [Benefits vs original `pdf-lib`](#benefits-vs-original-pdf-lib)
- [Features](#features)
- [Motivation](#motivation)
- [Usage Examples](#usage-examples)
- [Create Document](#create-document)
- [Modify Document](#modify-document)
- [Incremental Document Modification](#incremental-document-modification)
- [Consecutive Incremental Updates](#consecutive-incremental-updates)
- [Create Form](#create-form)
- [Fill Form](#fill-form)
- [Flatten Form](#flatten-form)
- [Work with XFA Forms](#work-with-xfa-forms)
- [Extract XFA JavaScript](#extract-xfa-javascript)
- [Modify XFA JavaScript](#modify-xfa-javascript)
- [Extract Document JavaScript](#extract-document-javascript)
- [Copy Pages](#copy-pages)
- [Embed PNG and JPEG Images](#embed-png-and-jpeg-images)
- [Embed PDF Pages](#embed-pdf-pages)
- [Embed Font and Measure Text](#embed-font-and-measure-text)
- [Add Attachments](#add-attachments)
- [Extract Attachments](#extract-attachments)
- [Create PDF/A Documents](#create-pdfa-documents)
- [Embed Factur-X / ZUGFeRD Invoices](#embed-factur-x--zugferd-invoices)
- [Set Document Metadata](#set-document-metadata)
- [Read Document Metadata](#read-document-metadata)
- [Set Viewer Preferences](#set-viewer-preferences)
- [Read Viewer Preferences](#read-viewer-preferences)
- [Draw SVG Paths](#draw-svg-paths)
- [Draw SVG](#draw-svg)
- [Deno Usage](#deno-usage)
- [Complete Examples](#complete-examples)
- [Installation](#installation)
- [NPM Module](#npm-module)
- [Vendored PNG & standard fonts](#vendored-png--standard-fonts)
- [UMD Module](#umd-module)
- [Fontkit Installation](#fontkit-installation)
- [Documentation](#documentation)
- [Fonts and Unicode](#fonts-and-unicode)
- [Creating and Filling Forms](#creating-and-filling-forms)
- [Limitations](#limitations)
- [Help and Discussion](#help-and-discussion)
- [Encryption Handling](#encryption-handling)
- [Migrating to v1.0.0](docs/MIGRATION.md)
- [Contributing](#contributing)
- [Maintainership](#maintainership)
- [Tutorials and Cool Stuff](#tutorials-and-cool-stuff)
- [Prior Art](#prior-art)
- [Git History Rewrite](#git-history-rewrite)
- [Changelog](CHANGELOG.md)
- [License](#license)
## Features
- Create new PDFs
- Modify existing PDFs
- Incremental document updates (append-only saves / multi-commit)
- Encrypt / decrypt PDFs (AES-256 by default)
- Create forms
- Fill forms
- Flatten forms
- Preserve XFA forms
- Extract XFA JavaScript
- Modify XFA JavaScript
- Extract document-level JavaScript
- Add Pages
- Insert Pages
- Remove Pages
- Copy pages between PDFs
- Draw Text (including character spacing)
- Draw Images
- Draw PDF Pages
- Draw Vector Graphics
- Draw SVG Paths
- Draw full SVG documents (`drawSvg` / `embedSvg`)
- Extract page contents (text, images, vector graphics)
- Optional content / PDF layer visibility
- Measure width and height of text
- Embed Fonts (supports UTF-8 and UTF-16 character sets; TTC/DFont face selection)
- Set document metadata
- Read document metadata
- Set viewer preferences
- Read viewer preferences
- Add attachments
- Extract / detach attachments
- Create PDF/A documents (parts 1–3)
- Embed Factur-X / ZUGFeRD e-invoices (PDF/A-3)
## Motivation
`pdf-lib` was created to address the JavaScript ecosystem's lack of robust support for PDF manipulation (especially for PDF _modification_).
Two of `pdf-lib`'s distinguishing features are:
1. Supporting modification (editing) of existing documents.
2. Working in all JavaScript environments - not just in Node or the Browser.
There are [other](#prior-art) good open source JavaScript PDF libraries available. However, most of them can only _create_ documents, they cannot _modify_ existing ones. And many of them only work in particular environments.
## Usage Examples
### Create Document
_This example produces [this PDF](assets/pdfs/examples/create_document.pdf)._
[Try the JSFiddle demo](https://jsfiddle.net/Hopding/rxwsc8f5/13/)
```js
import { PDFDocument, StandardFonts, rgb } from 'pdf-lib'
// Create a new PDFDocument
const pdfDoc = await PDFDocument.create()
// Embed the Times Roman font
const timesRomanFont = await pdfDoc.embedFont(StandardFonts.TimesRoman)
// Add a blank page to the document
const page = pdfDoc.addPage()
// Get the width and height of the page
const { width, height } = page.getSize()
// Draw a string of text toward the top of the page
const fontSize = 30
page.drawText('Creating PDFs in JavaScript is awesome!', {
x: 50,
y: height - 4 * fontSize,
size: fontSize,
font: timesRomanFont,
color: rgb(0, 0.53, 0.71),
})
// Serialize the PDFDocument to bytes (a Uint8Array)
const pdfBytes = await pdfDoc.save()
// For example, `pdfBytes` can be:
// • Written to a file in Node
// • Downloaded from the browser
// • Rendered in an