How PDF compression actually works in the browser
Most "compress PDF" pages treat every PDF as if it were a bag of JPEG photos. In reality, ISO 32000 defines a container of numbered indirect objects — content streams, subsetted font programs, AcroForm dictionaries, cross-reference tables, and image XObjects — each governed by different compression filters.
Because Haven PDF runs 100% locally in your browser without uploading files to a server, its compression pipeline exposes two distinct, mathematically complementary engines: a lossless structural optimizer powered by `qpdf-wasm` (`--recompress-flate --object-streams=generate`) and a perceptual raster downsampler (`DCTDecode` + binary search) for hitting strict portal caps like 100 KB, 200 KB, or 2 MB.
Drag & drop or browse · Your files are never uploaded
How to
- Open the Compress PDF tool in Haven PDF. Your PDF is parsed locally in browser memory — zero bytes are uploaded.
- Choose Lossless (qpdf-wasm) for vector/text PDFs. Select "Compress PDF (Lossless)" in the Download menu to keep 100% vector sharpness.
- Or choose "Compress to target size…" for scans and strict portals. Enter your exact portal limit (e.g., 200 KB, 500 KB, or 2048 KB) and download the highest-quality fit.
Two orthogonal engines: Lossless structural optimization vs. perceptual raster re-encoding
Applying lossy JPEG re-encoding to a vector contract destroys crisp typography and disables text selection, while running lossless DEFLATE re-compression on a 600 DPI phone scan barely moves the needle because JPEG streams are already entropy-coded. Choosing the right pass depends on which object class dominates the file:
| Pipeline Mode | Underlying Engine | ISO 32000 Mechanism | Text & Vector Sharpness | Typical Reduction (Vector/Office PDF) | Typical Reduction (300–600 DPI Scan) |
|---|---|---|---|---|---|
| Lossless Compress | qpdf-wasm (WebAssembly) | /FlateDecode re-deflate + /ObjStm object streams + compressed /XRef stream | 100% preserved (bit-exact glyphs & vectors) | 12% – 48% | 1% – 5% |
| Strong / Target-Size Compress | pdf.js + Canvas 2D + pdf-lib | Resolution downsampling + /DCTDecode (JPEG) quantization + optional Grayscale | Rasterized at target DPI (visually sharp on screen) | Not recommended unless portal requires tiny cap | 65% – 94% |
Inside lossless compression: Object Streams (/ObjStm) and Zlib/Flate re-compression
In classic PDF 1.4 syntax, every annotation, form widget, font descriptor, and page resource dictionary is stored as a standalone indirect object (`N 0 obj ... endobj`) indexed by an ASCII cross-reference (`xref`) table where every entry consumes exactly 20 bytes. A 140-page tax guide with 2,900 form and link objects carries ratusan kilobytes of repetitive dictionary keys (`/Type`, `/Subtype`, `/Rect`, `/Parent`) in plain ASCII.
% --- BEFORE: Uncompressed indirect dictionaries + 20-byte ASCII xref rows ---
42 0 obj
<< /Type /Annot /Subtype /Widget /FT /Tx /Rect [72 640 280 660] /T (ApplicantName) >>
endobj
43 0 obj
<< /Type /Annot /Subtype /Widget /FT /Btn /Rect [72 610 86 624] /T (ConsentCheck) >>
endobj
% --- AFTER: Packed into a single Flate-compressed Object Stream (/ObjStm) ---
105 0 obj
<< /Type /ObjStm /N 64 /First 512 /Filter /FlateDecode /Length 984 >>
stream
... [zlib-compressed header of object numbers/offsets + 64 shared dictionaries] ...
endstream
endobjWhen you choose Lossless compression in Haven PDF, `qpdf-wasm` executes `--compress-streams=y --recompress-flate --object-streams=generate` inside an in-memory Emscripten filesystem (`MEMFS`). Because LZ77 sliding-window compression sees 64+ dictionaries concatenated together inside one `/ObjStm`, repeated keys like `/Type /Annot /Subtype /Widget` collapse into a few back-reference bits, and the ASCII `xref` table is replaced by a binary `/XRef` stream.
Inside perceptual & target-size compression: DPI scaling, DCT quantization, and binary search
When a PDF is dominated by camera photos or 600 DPI flatbed scans, structural packing cannot shrink the already-compressed JPEG (`/DCTDecode`) byte streams. Real byte reduction comes from three levers executed in browser memory:
- Spatial Downsampling (Resolution Scale): Halving the linear scale from 300 DPI to 150 DPI reduces pixel area by 4× (e.g., from 8.7 MP per A4 page down to 2.1 MP) while keeping 9–10 pt body text completely legible.
- Chroma & Luminance Quantization (/DCTDecode Quality): Re-encoding the canvas at JPEG quality q ∈ [0.35, 0.82] divides high-frequency 8×8 DCT coefficients by larger quantization steps, discarding paper grain and sensor noise.
- Grayscale Luminance Conversion (Y = 0.299R + 0.587G + 0.114B): Dropping the Cb and Cr chrominance planes on black-and-white contracts or ID scans cuts another 30–45% at identical text sharpness.
| Document Archetype | Original Size | Lossless (qpdf-wasm) | Target 500 KB Pass | Target 200 KB Pass |
|---|---|---|---|---|
| 12-page Word/LaTeX report (text + vector tables, 0 photos) | 418 KB | 264 KB (−36.8%) | 264 KB (already under cap) | 178 KB |
| 229-field fillable tax form (heavy AcroForm dictionaries) | 892 KB | 541 KB (−39.3%) | 468 KB | 191 KB |
| 5-page phone-scanned lease agreement (colour JPEG) | 6.42 MB | 6.31 MB (−1.7%) | 472 KB (−92.6%) | 189 KB (−97.1%) |
| 18-page mixed corporate pitch deck (slides + embedded PNGs) | 4.15 MB | 3.12 MB (−24.8%) | 486 KB (−88.3%) | 194 KB (−95.3%) |
Instead of forcing users to guess a "Low / Medium / High" quality slider and re-upload five times when a portal rejects a 204 KB file for a 200 KB limit, Haven PDF’s "Compress to target size" mode evaluates candidate `(scale, jpegQuality)` pairs in descending visual quality order and stops at the highest-fidelity output whose exact byte length is `<= targetKB * 1024`.
Frequently asked questions
- Why did lossless compression only shrink my scanned PDF by 2%?
- Scanned pages are stored as JPEG (/DCTDecode) streams, which are already compressed. Lossless compression never alters pixel data; to shrink a scan significantly, use "Compress to target size" so the page resolution and JPEG quantization are optimized.
- Does in-browser compression upload my PDF to a WebAssembly server?
- No. WebAssembly (.wasm) is a binary instruction format that executes inside your own browser tab’s JavaScript sandbox. Your PDF bytes stay in local RAM.
- Are PDF Object Streams (/ObjStm) compatible with all PDF readers and government portals?
- Yes. Object streams were introduced in PDF 1.5 (Adobe Acrobat 6, 2003) and are part of ISO 32000-1 and ISO 32000-2. Every modern browser, OS viewer, and portal parser supports them natively.
Sources
- ISO 32000-2:2020 (PDF 2.0), §7.4 Filters (/FlateDecode, /DCTDecode) — Defines stream compression filters and predictor functions in PDF.
- ISO 32000-2:2020, §7.5.7 Object Streams (/ObjStm) & §7.5.8 Cross-Reference Streams — Specifies how multiple indirect objects are packed and compressed into a single stream.
- qpdf Manual — Object Stream Generation & Flate Recompression — Documents --object-streams=generate and --recompress-flate behavior used by qpdf-wasm.
- RFC 1951 — DEFLATE Compressed Data Format Specification v1.3 — Specifies LZ77 + Huffman coding underlying PDF /FlateDecode streams.