Client-Side PDF Engineering: pdf-lib Manipulation, Worker Rasterization & Metadata Sanitization
Master zero-upload PDF engineering in the browser: learn when to modify vector COS objects losslessly with pdf-lib, how PDF.js Web Workers rasterize pages cleanly at 150-300 DPI, and how to strip hidden Author, Creator, and XMP metadata.
1. PDF Internal Architecture: COS Objects vs Pixel Rasterization
pdfinfo -meta confidential-contract.pdf
qpdf --show-all-pages --object-streams=disable confidential-contract.pdf inspect.pdfAudits embedded document metadata, XMP XML packets, and page rotation flags from the terminal.
import { PDFDocument, degrees } from 'pdf-lib';
const doc = await PDFDocument.load(arrayBuffer);
doc.getPages().forEach((page) => page.setRotation(degrees(90)));
const cleanBytes = await doc.save({ useObjectStreams: true });Rotates vector pages purely by updating each Page /Rotate dictionary key.
| Operation | Engine Class | Vector Text Preserved | Typical Latency | Copy |
|---|---|---|---|---|
| Merge / Split / Reorder | Direct PDFDocument page copy | Yes (100% lossless) | < 40ms | |
| Rotate / Watermark / Page # | Page dictionary & overlay stream | Yes (100% lossless) | < 60ms | |
| PDF -> PNG/JPG Export | PDF.js off-thread Web Worker | Rasterized at chosen scale | 80-250ms / page | |
| Image -> PDF Print Book | JPEG/PNG XObject embedding | N/A (embedded images) | < 120ms |
2. Rasterization Scale Factors, DPI & Browser Canvas Limits
const viewport = page.getViewport({ scale: 2.0 });
canvas.width = Math.floor(viewport.width);
canvas.height = Math.floor(viewport.height);
await page.render({ canvasContext: ctx, viewport }).promise;Uses an isolated Web Worker to decode CFF fonts and FlateDecode streams without freezing UI responsiveness.
| Target Use Case | Scale Factor | A4 Output Resolution | Canvas Pixel Footprint | Copy |
|---|---|---|---|---|
| Web Thumbnail Preview | 1.0x (72 DPI) | 595 x 842 px | ~2.0 MB RGBA raw buffer | |
| Crisp Screen & Document Share | 2.0x (144 DPI) | 1190 x 1684 px | ~8.0 MB RGBA raw buffer | |
| High-Detail Print / OCR | 3.0x (216 DPI) | 1785 x 2526 px | ~18.0 MB RGBA raw buffer |
3. Metadata Leaks: /Info Dictionaries, XMP Streams & Print Stamps
/Author, software build versions into /Creator and /Producer, and editing timestamps into /CreationDate and /ModDate. Worse, some tools embed duplicate XML inside the unencrypted /Metadata XMP streamโstripping both in-memory before sharing prevents identity disclosure.doc.setTitle('');
doc.setAuthor('');
doc.setSubject('');
doc.setKeywords([]);
doc.setProducer('');
doc.setCreator('');Overwrites identifying /Info dictionary attributes locally prior to byte serialization.
| Metadata Field | Common Exposure Risk | Sanitization Action | Copy |
|---|---|---|---|
| /Author & /Creator | Reveals employee full name, OS account ID, or corporate template name | Overwrite with empty string | |
| /Producer | Exposes exact Office, Chrome, or CAD suite version for fingerprinting | Clear string value | |
| /CreationDate & /ModDate | Discloses user timezone offset and exact editing timeline | Reset to epoch / scrub | |
| XMP /Metadata Stream | Duplicates XML edit history and UUIDs outside /Info dictionary | Strip or re-serialize cleanly |
4. Why Sensitive Contracts Belong Exclusively in Local Browser Memory
pdf-lib and pdfjs-dist inside local client-side memory transforms every byte purely on the user CPU with zero network egress.Related Free Online Tools
Put these techniques into practice immediately with our privacy-first browser utilities:
Combine JPG, PNG, and WebP scans into paginated A4 or US Letter PDFs locally in memory.
Rasterize multi-page PDF documents into crisp 1x-3x PNG, JPEG, or WebP images offline.
Inspect and scrub Author, Title, Creator, Producer, and XMP streams without server uploads.
Independent software developer focused on client-side architectures, local-first computing, WebAssembly, and privacy-first web systems.