Package Exports
- pdf-lib
- pdf-lib/lib/core/pdf-structures/factories/PDFXRefTableFactory
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pdf-lib) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
pdf-lib
Table of Contents
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 design requirements since its inception are to:
- Support modification (editing) of existing documents.
- Work in all JavaScript environments - not just in Node or the browser.
There are other good open source JavaScript PDF libraries available. However, most of them can only create documents, they cannot modify existing ones (e.g. pdfkit, jspdf, pdfmake). hummus is a NodeJS library capable of both creating and modifying PDF documents. However, hummus is a Node wrapper around a C++ library. This means that it cannot run in all JavaScript environments. For example, you cannot use hummus in the Browser or in React Native.
Features
- Create new PDFs
- Modify existing PDFs
- New: Copy pages between PDFs
- Add Pages
- Insert Pages
- Remove Pages
- Draw Text
- Draw Images
- Draw Vector Graphics
- Embed Fonts
- Embed Images
Usage Examples
More detailed examples are available here.
Document Creation
import { PDFDocumentFactory, PDFDocumentWriter, drawText } from 'pdf-lib';
const pdfDoc = PDFDocumentFactory.create();
const [timesRomanFont] = pdfDoc.embedStandardFont('Times-Roman');
const page = pdfDoc
.createPage([350, 500])
.addFontDictionary('Times-Roman', timesRomanFont);
const contentStream = pdfDoc.createContentStream(
drawText('Creating PDFs in JavaScript is awesome!', {
x: 50,
y: 450,
size: 15,
font: 'Times-Roman',
colorRgb: [0, 0.53, 0.71],
}),
);
page.addContentStreams(pdfDoc.register(contentStream));
pdfDoc.addPage(page);
const pdfBytes = PDFDocumentWriter.saveToBytes(pdfDoc);Document Modification
import { PDFDocumentFactory, PDFDocumentWriter, drawText } from 'pdf-lib';
// This should be a Uint8Array.
// This data can be obtained in a number of different ways.
// If your running in a Node environment, you could use fs.readFile().
// In the browser, you could make a fetch() call and use res.arrayBuffer().
const existingPdfDocBytes = ...
const pdfDoc = PDFDocumentFactory.load(existingPdfDocBytes);
const [helveticaFont] = pdfDoc.embedStandardFont('Helvetica');
const pages = pdfDoc.getPages();
const page = pages[0];
page.addFontDictionary('Helvetica', helveticaFont);
const contentStream = pdfDoc.createContentStream(
drawText('This text was added to the PDF with JavaScript!', {
x: 25,
y: 25,
size: 24,
font: 'Helvetica',
colorRgb: [0.95, 0.26, 0.21],
}),
);
page.addContentStreams(pdfDoc.register(contentStream));
const pdfBytes = PDFDocumentWriter.saveToBytes(pdfDoc);Copying Pages
import { PDFDocumentFactory, PDFDocumentWriter } from 'pdf-lib';
// These should be a Uint8Arrays. (see Document Modification example for more details)
const firstDonorPdfBytes = ...
const secondDonorPdfBytes = ...
const firstDonorPdfDoc = PDFDocumentFactory.load(firstDonorPdfBytes);
const secondDonorPdfDoc = PDFDocumentFactory.load(secondDonorPdfBytes);
// We'll copy the fourth page from the first donor document, and the
// first page from the second donor document.
const firstDonorPage = firstDonorPdfDoc.getPages()[3];
const secondDonorPage = secondDonorPdfDoc.getPages()[0];
const pdfDoc = PDFDocumentFactory.create();
// Copy over the pages from the donor documents into our new document
pdfDoc.addPage(firstDonorPage);
pdfDoc.insertPage(0, secondDonorPage);
const pdfBytes = PDFDocumentWriter.saveToBytes(pdfDoc);Installation
NPM Module
To install the latest stable version:
# With npm
npm install --save pdf-lib
# With yarn
yarn add pdf-libThis assumes you're using npm or yarn as your package manager.
UMD Module
You can also download pdf-lib as a UMD module from unpkg. The UMD builds have been compiled to ES5, so they should work in any modern browser. UMD builds are useful if you aren't using a package manager or module bundler. For example, you can use them directly in the <script> tag of an HTML page.
The following builds are available:
When using a UMD build, you will have access to a global window.PDFLib variable. This variable contains all of the classes and functions exported by pdf-lib. For example:
// NPM module
import { PDFDocumentFactory, drawText } from 'pdf-lib';
// UMD module
var PDFDocumentFactory = PDFLib.PDFDocumentFactory;
var drawText = PDFLib.drawText;API Documentation
API documentation is available here.
Contributing
We welcome contributions from the open source community! If you are interested in contributing to pdf-lib, please take a look at the CONTRIBUTING.md file. It contains information to help you get pdf-lib setup and running on your machine.
Prior Art
pdfkitis a PDF generation library for Node and the Browser. This library was immensely helpful as a reference and existence proof when creatingpdf-lib.pdfkit's code for font embedding, PNG embedding, and JPG embedding was especially useful.jspdfis a PDF generation library for the browser.pdfmakeis a PDF generation library for the browser.hummusis a PDF generation and modification library for Node environments.hummusis a Node wrapper around a C++ library.react-native-pdf-libis a PDF generation and modification library for React Native environments.react-native-pdf-libis a wrapper around C++ and Java libraries.pdfassembleris a PDF generation and modification library for Node and the browser. It requires some knowledge about the logical structure of PDF documents to use.