DEV Community

toolzip
toolzip

Posted on

Compress PDFs in Your Browser — No Upload, No Server, Free

PDF compression is one of those tasks that sounds simple but hides a surprising amount of complexity. Most online tools solve it by uploading your file to a server. That's fine for generic documents — but becomes a problem when the PDF contains contracts, financial statements, medical records, or any sensitive information.

Here's how browser-based PDF compression works, why it matters for privacy-sensitive files, and the technical trade-offs involved.

Why PDFs Get So Large

Understanding file size requires understanding what's inside a PDF.

A PDF is essentially a container format. Inside it, you'll find some combination of:

  • Vector graphics (text, shapes drawn with math) — these compress extremely well
  • Raster images (JPEGs, PNGs embedded inside) — these dominate file size
  • Fonts (embedded typeface data) — can add several MB per typeface
  • Metadata (creation date, author, edit history, thumbnails) — usually small but unnecessary in shared documents
  • Hidden layers (from design tools like Illustrator or InDesign)

A scanned document is the worst case: it's essentially a series of raster images, one per page. A single A4 scan at 300 DPI runs about 1-2MB. A 30-page scanned document can easily be 40-50MB.

A pure text document created in Word or Pages is the best case: mostly vector data with embedded fonts, typically compressing 20-40%.

What "Compression" Actually Does

PDF compression is primarily image compression. When you reduce a PDF from 20MB to 5MB, you're almost entirely compressing the embedded images:

  1. Downsample images: Reduce image resolution from 300 DPI to 96-150 DPI. Imperceptible on screen, significant for file size.
  2. Re-encode images: Convert PNG or TIFF images inside the PDF to JPEG at reduced quality.
  3. Remove metadata: Strip thumbnail previews, edit history, comments.
  4. Subset fonts: Instead of embedding the entire typeface, only embed the specific characters used in the document.

The last technique is particularly effective for documents using many typefaces — a design-heavy document might include entire fonts that are only used for a headline.

Browser-Side Compression with pdf-lib

pdf-lib is the primary JavaScript library for PDF manipulation in the browser. It can read, modify, and write PDFs entirely client-side.

import { PDFDocument } from 'pdf-lib';

async function compressPdf(file) {
  const arrayBuffer = await file.arrayBuffer();
  const pdfDoc = await PDFDocument.load(arrayBuffer);

  // Re-save with compression enabled
  const compressedBytes = await pdfDoc.save({
    useObjectStreams: true,  // Compress internal structure
    addDefaultPage: false,
  });

  return new Blob([compressedBytes], { type: 'application/pdf' });
}
Enter fullscreen mode Exit fullscreen mode

useObjectStreams: true enables cross-reference stream compression, which reduces file size by compressing the PDF's internal object references. This is most effective for text-heavy documents.

For image compression, you need to re-encode embedded images. Here's a simplified approach:

import { PDFDocument, PDFImage } from 'pdf-lib';

async function compressImages(pdfDoc, quality = 0.7) {
  const pages = pdfDoc.getPages();

  for (const page of pages) {
    const { node } = page;
    // Access embedded XObject images
    const xObjects = node.Resources?.XObject;
    if (!xObjects) continue;

    for (const [name, ref] of Object.entries(xObjects.dict)) {
      const image = pdfDoc.context.lookup(ref);
      if (image?.dict?.get(PDFName.of('Subtype'))?.toString() === '/Image') {
        // Re-encode image at lower quality
        // (implementation varies by image type)
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

In practice, image compression inside PDFs requires handling JPEG, JPEG2000, JBIG2, and raw pixel formats separately. Full implementations are complex — this is why dedicated libraries exist.

Compression Results by Document Type

Document Type Before After (Medium) Reduction
Scanned pages (image-heavy) 20MB 4-6MB 70-80%
Design export (many fonts) 15MB 6-8MB 45-55%
Presentation slides 10MB 3-5MB 50-70%
Text-only report 2MB 1.5MB 25-30%
Already-compressed PDF 5MB 4.8MB ~5%

The last row illustrates an important point: compression is not always additive. If a PDF has already been compressed, re-compressing it won't help much and may slightly degrade image quality without significant size reduction.

Privacy Consideration: Why Browser-Side Matters

Most online PDF tools require uploading your file. For a generic PDF this is fine. But consider what typically gets compressed:

  • Contracts with counterparty signatures
  • Financial statements before sending to accountants
  • Medical records for insurance claims
  • Salary documents for rental applications
  • Tax returns

These files contain precisely the kind of information that makes a breach damaging. Browser-side processing means the file never leaves the device. There's no upload, no server log of what you processed, no temporary storage to be subpoenaed or breached.

You can verify this behavior directly: open DevTools → Network tab, then run a compression. You'll see the PDF library files load once from a CDN, then nothing. No file upload request.

Try It

ToolZip's PDF compressor runs entirely in your browser using pdf-lib. Three compression levels (high quality, balanced, maximum compression) let you trade quality for size.

toolzip.app/tools/pdf-compress

No signup. No upload. Works on any modern browser.


ToolZip — 48 free browser-based tools. Everything runs client-side.
toolzip.app

Top comments (0)