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
- 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 } from 'pdf-lib/core/pdf-document';
import { drawText } from 'pdf-lib/helpers/pdf-operators/composite';
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 } from 'pdf-lib/core/pdf-document';
import { drawText } from 'pdf-lib/helpers/pdf-operators/composite';
// 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);
Installation
npm install --save pdf-lib
or
yarn add pdf-lib
API Documentation
API documentation is available here.
Prior Art
pdfkit
is 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.jspdf
is a PDF generation library for the browser.pdfmake
is a PDF generation library for the browser.hummus
is a PDF generation and modification library for Node environments.hummus
is a Node wrapper around a C++ library.react-native-pdf-lib
is a PDF generation and modification library for React Native environments.react-native-pdf-lib
is a wrapper around C++ and Java libraries.