Skip to content
DEVPANTRY.COM
Back to All Engineering Guides
Media & Graphics 5 min read October 2, 2026

Client-Side PDF Manipulation & Structural Compression in the Browser

Learn how to process, merge, and compress PDF documents securely in the browser using ArrayBuffers, PDF-lib, and structural optimization without server uploads.

DevPantry EngineeringMedia & Graphics Architecture
Interactive Tool ImplementationTest this exact engineering implementation live in your browser.
Open PDF Exam & Photo Kit

Key Architectural Takeaways

  • 01.Client-side processing avoids uploading document contents for PDF conversion and merging.
  • 02.PDF-lib allows for deep structural manipulation by modifying cross-reference tables and stripping out hidden metadata.
  • 03.This optimizer rewrites object streams and clears common document-info fields; it does not downsample embedded images.
  • 04.Converting modern image formats (like WebP) into PDF structures requires careful rasterization and canvas handling.

1. The Privacy Problem with Online PDF Tools

Every day, millions of users upload highly sensitive documents—Aadhar cards, SSNs, passports, and exam marksheets—to third-party server-side PDF compressors. These files reside on temporary cloud storage, often with unclear retention policies, creating a massive privacy risk.

By moving PDF manipulation entirely to the client, we eliminate this attack surface. Browsers are incredibly powerful environments. Using modern Web APIs like FileReader, ArrayBuffer, and Blob, we can load megabytes of document data directly into browser memory, process them, and download them instantly—zero HTTP uploads required.

2. How Client-Side PDF Compression Works

When compressing a PDF purely in the browser without WebAssembly image engines, we are inherently limited to structural optimization.

Our implementation uses pdf-lib to clear common title, author, subject, producer, and creator fields, then saves the document with object streams enabled. It does not deliberately remove embedded fonts, XMP packets, form fields, or image pixels.

The output may be smaller, unchanged, or larger, depending on the original structure. A PDF containing a large scanned JPEG will generally still contain that JPEG. Compare the measured input and output sizes instead of assuming a particular reduction.

Try it yourself using the Compress PDF tool!

3. Combining Images into PDFs

When creating PDFs from images, especially to hit strict < 200 KB limits enforced by job portals, the workflow should be:

  1. First, compress the raw images to your target size budget (e.g. 150KB).
  2. Second, embed those compressed images into a new, cleanly-structured PDF envelope.

Try it yourself using the Image to PDF Converter tool!

Embedding Images into a PDF with pdf-libtypescript
import { PDFDocument } from 'pdf-lib';

async function createPdfFromImages(imageFiles: File[]) {
  const pdfDoc = await PDFDocument.create();
  
  for (const file of imageFiles) {
    const arrayBuffer = await file.arrayBuffer();
    
    // Embed the JPEG/PNG based on type
    const image = file.type === 'image/jpeg' 
      ? await pdfDoc.embedJpg(arrayBuffer)
      : await pdfDoc.embedPng(arrayBuffer);
      
    // Create a new page matching the image dimensions
    const page = pdfDoc.addPage([image.width, image.height]);
    
    // Draw the image onto the page
    page.drawImage(image, {
      x: 0,
      y: 0,
      width: image.width,
      height: image.height,
    });
  }
  
  // Save structurally optimized PDF
  const pdfBytes = await pdfDoc.save({ useObjectStreams: false });
  return new Blob([pdfBytes], { type: 'application/pdf' });
}
Tags:#PDF#Compression#Client-Side#Privacy#PDF-lib#JavaScript
Launch PDF Exam & Photo Kit