talira

@cantoo/pdf-lib

An actively maintained fork of pdf-lib: create and modify PDF documents in any JavaScript environment

open sourceMITself-hostablefree

Forked from pdf-lib in 2021. Cantoo maintains it for its own product and ships features upstream doesn't provide; pdf-lib's owner stopped answering, so Cantoo asks for issues and pull requests on the fork

@cantoo/pdf-lib is Cantoo's actively maintained fork of pdf-lib, the pure JavaScript library that creates and edits PDFs in browsers, Node, Deno and React Native. It keeps pdf-lib's API and docs (create, fill, read and flatten AcroForm fields, copy and merge pages, embed fonts and images) and adds what upstream lacks: opening and encrypting password-protected PDFs (AES-256 by default), incremental saves, full SVG drawing, extracting page contents, PDF/A and Factur-X/ZUGFeRD structure, attachments, PDF layers, and helpers that preserve and edit static XFA forms. Its flatten() also flattens orphaned widget annotations that aren't listed in the form's fields. Cantoo says it maintains the fork for its own product's needs and relies on contributions for issues outside that roadmap.

Problems it solves

  • pdf-lib hasn't had a release since 2021 and the bug I hit was fixed in a pull request nobody merged
  • open a password-protected PDF in Node, change it and save it again
  • encrypt a generated PDF with a password and permissions from JavaScript
  • append changes to a PDF as an incremental update so earlier signatures stay intact
  • draw a whole SVG file onto a PDF page from JavaScript, not just a single path
  • after flattening a form, some widgets are still editable because they aren't in the AcroForm fields list
  • loading and saving an XFA government form in JavaScript throws away its XFA data
  • read or change the JavaScript inside an XFA form
  • create a PDF/A-3 file in the browser or Node
  • embed a Factur-X or ZUGFeRD XML invoice into a PDF from TypeScript
  • pull the images and text out of a PDF page's content stream in JavaScript
  • show or hide PDF layers (optional content groups) from code
  • a truncated PDF makes my JavaScript PDF library throw instead of opening what's there
  • attach files to a PDF and extract or remove existing attachments in JavaScript
  • embed one face from a .ttc font collection into a PDF

View all 16 problems

Features

  • pdf-lib's API and docs: create and modify PDFs, add, remove, copy and merge pages
  • create, fill, read and flatten AcroForm fields; flatten() also flattens orphaned widget annotations
  • open encrypted PDFs with a password; encrypt() with AES-256 (default), AES-128 or RC4
  • incremental updates: append changes without rewriting the file
  • full SVG drawing (drawSvg, embedSvg) as well as SVG paths
  • PDFPage.extractContents(): text, images and approximate vector graphics from content streams
  • XFA helpers: preserveXFA, read and modify XFA JavaScript, signature-field discovery
  • document-level JavaScript extraction
  • convertToPDFA() for PDF/A-1, 2 and 3 structure; embedFacturX() for Factur-X / ZUGFeRD invoices
  • add, extract and detach attachments
View 5 more
  • optional content (PDF layers) visibility
  • recovers truncated PDFs and parses more malformed files
  • @cantoo/fontkit support and postscriptName to pick a face from .ttc / .dfont collections
  • characterSpacing on drawText
  • pure JavaScript: runs in Node, browsers, Deno and React Native

Limitations

  • Doesn't extract plain text from a page outside form fields: it reads the values of text fields only1
  • Doesn't add HTML or CSS content to a PDF: its README points to Puppeteer for that1
  • Doesn't handle dynamic XFA forms: its XFA helpers target static government and tax XFA layouts1
  • Doesn't make files fully PDF/A compliant: convertToPDFA() adds the structure but doesn't rewrite fonts, transparency or JavaScript1

Alternatives

The same job

These do what @cantoo/pdf-lib does.

  1. PDFKit A JavaScript PDF generation library for Node and the browser.
  2. @pdfme/pdf-lib This version is forked from Hopding/pdf-lib, with several bug fixes and additional features
  3. pdf-lib Create and modify PDF documents in any JavaScript environment.
  4. Apryse SDK (WebViewer) Document SDKs for viewing, editing and extracting data across 100+ formats
  5. Nutrient (formerly PSPDFKit) The document layer for agents
  6. pdftk (PDFtk Server and pdftk-java) A command-line tool for working with PDFs

Compare @cantoo/pdf-lib with the top 3