Tools
pdf-libPDFFile Processing
PDF Manipulation with pdf-lib
Merging, splitting, and manipulating PDF files using the pdf-lib library.
PDF Manipulation with pdf-lib
pdf-lib is a powerful library for creating and modifying PDF documents in JavaScript and TypeScript.
Capabilities
Document Creation
- Create PDFs from scratch
- Add pages, text, images, and forms
- Full control over layout and styling
Document Modification
- Merge multiple PDFs
- Split PDFs into separate files
- Add metadata (title, author, subject)
- Extract and modify pages
Use Cases in The Workbench
PDF Merger
Combine multiple PDF files into a single document:
import { PDFDocument } from 'pdf-lib';
const mergedPdf = await PDFDocument.create();
for (const pdfBytes of pdfFiles) {
const pdf = await PDFDocument.load(pdfBytes);
const pages = await mergedPdf.copyPages(pdf, pdf.getPageIndices());
pages.forEach((page) => mergedPdf.addPage(page));
}
PDF Splitter
Extract specific page ranges:
const sourcePdf = await PDFDocument.load(pdfBytes);
const newPdf = await PDFDocument.create();
const pages = await newPdf.copyPages(sourcePdf, [0, 1, 2]); // Pages 1-3
pages.forEach((page) => newPdf.addPage(page));
Metadata Management
Add or modify PDF metadata:
const pdfDoc = await PDFDocument.load(pdfBytes);
pdfDoc.setTitle('My Document');
pdfDoc.setAuthor('The Workbench');
pdfDoc.setSubject('Generated PDF');
pdfDoc.setProducer('The Workbench PDF Service');
Benefits
- Pure JavaScript: No native dependencies
- TypeScript Support: Full type definitions
- Browser Compatible: Works in both Node.js and browsers
- Small Bundle: Lightweight library
Performance Tips
- Load PDFs once and reuse
- Use
copyPagesfor better performance than individual page copying - Stream large files when possible
- Cache frequently used PDFs