API Reference

pdf-lib

Create and modify PDF documents in JavaScript with text, images, forms and page manipulation.

Official Documentation ↗
CompatibilityThis documentation covers version pdf-lib v1.17.x / Node 18 LTS 与浏览器. Check the official docs for the latest API surface.

Installation

npmnpm install pdf-lib
pnpmpnpm add pdf-lib
yarnyarn add pdf-lib

Methods

Overview

pdf-lib builds and edits PDFs entirely in JavaScript. It creates documents from scratch, loads existing ones, draws text and images, and merges or reorders pages, all with a promise-based API.

Core Methods

Create with PDFDocument.create or load with load, add pages, embed fonts and images, draw content, and finish with save to get bytes. Existing pages can be copied between documents.

Usage Notes

Embed fonts once per document and reuse the handle. Prefer vector drawing over rasterized text for crisp output. When merging, copy pages rather than re-encoding images.

Common Pitfalls

Standard fonts have limited Unicode coverage, so non-Latin text needs an embedded font with the needed glyphs. There is no automatic text reflow, and very large documents should be generated in chunks to limit memory.

FAQ

Can pdf-lib edit existing PDFs?
Yes. Load with PDFDocument.load, copy or modify pages, then save. It cannot reflow existing text, so edits are limited to overlaying new content or manipulating pages.
How do I add custom fonts?
Embed a font with embedFont using a standard font or a registered @pdf-lib/fontkit font file. Custom fonts must be embedded before drawing text that uses them.
Are form fields supported?
pdf-lib can read and fill AcroForm fields and create some field types. Complex dynamic forms may need a form-aware tool, so verify field behavior for your document.