What does this PDF compressor actually do?
This tool shrinks a PDF in two steps. First it finds the colour JPEG photos embedded in the file and re-encodes each one at a lower JPEG quality, at exactly the same pixel size, keeping the new version only when it is clearly smaller. Then it re-saves the document with compact object streams. Text, fonts and vector drawings are never touched.
If the finished file wouldn't be smaller than what you uploaded, you get no download, just a message saying the PDF is already as small as this tool can make it, so you never end up with a "compressed" copy that is bigger than the original.
Where a PDF's bytes go
A PDF is a collection of numbered objects (pages, fonts, images, drawing instructions) plus a cross-reference index telling a reader where each object starts (ISO 32000-1, section 7.5). Which kind of document you have decides how much this tool can do:
| Kind of PDF | Where most bytes are | What this tool can save |
|---|---|---|
| Scans saved as colour JPEG pages | Page photos | Often a lot |
| Photo-heavy brochures and reports | Embedded JPEG photos | Often a lot |
| Scans stored as black-and-white or lossless images | Page images | Little or nothing |
| Text reports from older software | Fonts and many small uncompressed objects | Somewhat |
| Text files written by modern tools | Already compact | Usually nothing |
Which images get recompressed
Inside a PDF, a JPEG photo is stored as an image stream with the DCTDecode filter, which is simply the JPEG file's bytes (section 7.4.8). The tool decodes each one with your browser, draws it onto a canvas of the same width and height, and saves it again as a JPEG at quality 0.6. To stay safe, it only touches images that are plain 8-bit colour JPEGs (DeviceRGB or an RGB ICC profile). It skips:
- images with a soft mask or mask (transparency), a decode array or extra decode parameters,
- greyscale, CMYK and indexed-colour JPEGs,
- any image that isn't a JPEG at all, such as Flate-compressed (PNG-style) images,
- images larger than 40 million pixels, and anything the browser can't decode.
A re-encoded photo replaces the original only if it is at most 90% of the old size. Placement on the page is unchanged, because the image keeps its dimensions and the page's drawing instructions aren't edited.
A worked example
This site's automated tests use a one-page report with a heading and a 480 x 320 photo saved at JPEG quality 0.95. The original file is 79,865 bytes. After compressing it is about 18,400 bytes, roughly 23% of the original, and the heading text is byte-for-byte identical. Rendered side by side, the two pages differ by about 2 levels out of 255 per colour channel on average, which is invisible at normal zoom.
The same test suite's tiny text-only PDF (854 bytes) shows the other case. It has no photos, and pdf-lib's re-save would have made it 6 bytes larger, so the tool reports that the file is already as small as it can make it and offers nothing to download.
To try it on your own file:
- Choose your PDF and click Convert.
- If a download appears, save
compressed.pdfand compare its size with the original. - Open it and zoom in on a photo to check the quality is acceptable for your use.
- If you see the "already as small" message, keep your original; nothing was lost.
What the re-save step changes
Object streams. PDF 1.5 added a way to pack many small objects into one compressed stream and to replace the plain-text cross-reference table with a compressed one (sections 7.5.7 and 7.5.8). Files written the old way shrink a little when those objects are packed.
Collapsed incremental updates. When a program saves edits by appending changes to the end of a file, old versions of changed objects stay behind (section 7.5.6). pdf-lib keeps only the latest version of each numbered object when it writes the file back out.
pdf-lib does not delete objects that nothing refers to, merge duplicate fonts, subset fonts or downsample images. Every PDF tool on this site saves with object streams, so a file you've already merged, split or rotated here will only shrink further if it contains JPEG photos.
When you need an even smaller file
- Before the PDF exists: shrink photos first with the image resizer and image compressor, then convert with JPG to PDF. A 4000-pixel-wide phone photo is far more than a document page needs, and this tool keeps the pixel count as it is.
- Remove what you don't need: dropping pages with Split PDF is often the quickest saving of all.
- On your own machine: Ghostscript's
pdfwritedevice can downsample images too. Its-dPDFSETTINGS=/ebookpreset targets 150 dpi colour images and/screentargets 72 dpi (Ghostscript documentation).
Limits
Password-protected PDFs and PDFs with permission restrictions are rejected: pdf-lib won't load any file with an encryption dictionary, and you'll see "Could not compress that PDF:" followed by a message saying the document is encrypted. Bookmarks, form fields and the document title and author are kept; pdf-lib updates the producer and modification date. There's no file size cap in the code, but the whole file and each photo being re-encoded are held in memory while it works.
Last updated