A low-level, minimal-dependency, type-safe PDF library that works in the browser and Node.js.
Note: This library is actively developed and may not support all PDF features yet. However, it is designed to be extensible and can be improved over time.
PRs and issues are welcome!
MIT License - see LICENSE for details.
Provides access to low-level PDF constructs, allowing for advanced manipulation and customization of PDF documents.
Supports encrypting and decrypting PDF files using standard algorithms, ensuring document security. Implements a password recovery feature for encrypted PDFs, allowing users to regain access to their documents.
Encryption algorithms supported:
Encryption methods:
Handles various compression algorithms, including Flate, LZW, and RunLength for efficient file size management. Note: image compression such as JPEG is not supported.
Compression algorithms supported:
PDF Signatures are powered by the pki-lite library.
Provides support for digitally signing PDF documents, ensuring authenticity and integrity. All signing functionality is integrated into the main pdf-lite package.
Signing algorithms supported:
Signature types supported:
LTV support/Revocation Info/Document Security Store (DSS):
Long-Term Validation (LTV) support ensures that digital signatures remain valid over time, even after the signing certificate has expired. To enable this, the Document Security Store (DSS) is used to store revocation information and other metadata related to the signature.
Other features:
This project uses pnpm for package management.
pnpm install
pnpm compile
# Run all tests
pnpm test
# Run unit tests only
pnpm test:unit
# Run acceptance tests
pnpm test:acceptance
The main package (packages/pdf-lite) contains:
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
npm install pdf-lite
or
yarn add pdf-lite
or
pnpm add pdf-lite
The library provides both low-level and high-level APIs for working with PDF documents.
import { PdfReader } from 'pdf-lite/pdf/pdf-reader'
import { readFile } from 'fs/promises'
const pdfBytes = await readFile('document.pdf')
const doc = await PdfReader.fromBytes([pdfBytes])
import { PdfDocument } from 'pdf-lite/pdf/pdf-document'
import { PdfDictionary } from 'pdf-lite/core/objects/pdf-dictionary'
import { PdfIndirectObject } from 'pdf-lite/core/objects/pdf-indirect-object'
import { PdfStream } from 'pdf-lite/core/objects/pdf-stream'
import { PdfName } from 'pdf-lite/core/objects/pdf-name'
import { PdfArray } from 'pdf-lite/core/objects/pdf-array'
import { PdfNumber } from 'pdf-lite/core/objects/pdf-number'
// Create the document
const document = new PdfDocument()
// Create content stream
const contentStream = new PdfIndirectObject({
content: new PdfStream({
header: new PdfDictionary(),
original: 'BT /F1 24 Tf 100 700 Td (Hello, PDF-Lite!) Tj ET',
}),
})
// Create and commit objects
document.commit(contentStream)
// ... create pages, catalog, etc.
// Output the PDF
console.log(document.toString())
import { PdfDocument } from 'pdf-lite/pdf/pdf-document'
import { PdfV2SecurityHandler } from 'pdf-lite/security/handlers/v2'
const document = new PdfDocument()
// ... build your PDF structure
// Set up encryption
document.securityHandler = new PdfV2SecurityHandler({
password: 'user-password',
documentId: 'unique-doc-id',
encryptMetadata: true,
})
// Encrypt the document
await document.encrypt()
console.log(document.toString())
import {
PdfAdbePkcs7DetachedSignatureObject,
PdfEtsiCadesDetachedSignatureObject,
} from 'pdf-lite'
// See examples directory for complete signing implementations
For more detailed examples, see the EXAMPLES.md file and the examples/ directory.
This project is organized as a monorepo:
The library uses TypeScript subpath exports for modular imports:
// PDF Document
import { PdfDocument } from 'pdf-lite/pdf/pdf-document'
import { PdfReader } from 'pdf-lite/pdf/pdf-reader'
// Core PDF objects
import { PdfArray } from 'pdf-lite/core/objects/pdf-array'
import { PdfDictionary } from 'pdf-lite/core/objects/pdf-dictionary'
import { PdfStream } from 'pdf-lite/core/objects/pdf-stream'
import { PdfString } from 'pdf-lite/core/objects/pdf-string'
import { PdfName } from 'pdf-lite/core/objects/pdf-name'
import { PdfNumber } from 'pdf-lite/core/objects/pdf-number'
import { PdfIndirectObject } from 'pdf-lite/core/objects/pdf-indirect-object'
import { PdfObjectReference } from 'pdf-lite/core/objects/pdf-object-reference'
// Security
import { PdfV2SecurityHandler } from 'pdf-lite/security/handlers/v2'
// Signing
import {
PdfAdbePkcs7DetachedSignatureObject,
PdfAdbePkcs7Sha1SignatureObject,
PdfAdbePkcsX509RsaSha1SignatureObject,
PdfEtsiCadesDetachedSignatureObject,
PdfEtsiRfc3161SignatureObject,
} from 'pdf-lite'