Dev.to WebDev 🛠 Dev 👁 0 📖 4 min read

My PDF compressor did nothing? 7 of 10 images were skipped on purpose

The first time a PDF compressor gives you back a file that is barely smaller, it feels like the tool is broken. Sometimes that is the tool being careful, and the way to see it is to open the file up: the test PDF here ho

The first time a PDF compressor gives you back a file that is barely smaller, it feels like the tool is broken. Sometimes that is the tool being careful, and the way to see it is to open the file up: the test PDF here holds ten kinds of images, and four tools were run on it, one image kind at a time. The file is 4,529,487 bytes, two A4 pages, one image of each kind. Among them are a plain RGB photo, JPEG 2000, a Lab image and a one-bit stamp mask, along with a CMYK print JPEG, a JBIG2 scan, a CCITT fax page, an ICC-tagged JPEG and a screenshot.

Everything below comes from one Apple-silicon Mac, one run per tool, one sample. One image per kind cannot stand in for "PDFs" in general. The photo is Bonnie Moreland's "Lake Mountain Landscape.jpg" from Wikimedia Commons (CC0). The JBIG2 page is bytes from the Internet Archive copy of the 1986 edition of the Table of Simplified Chinese Characters (Public Domain Mark 1.0). The rest is self-made, with fictional content.

Where the bytes actually are

The browser tool was ImgIng https://imging.ai/ in a desktop browser, at its electronic-book level, the default. The file went from 4,529,487 to 3,319,295 bytes, a 26.7% cut. That looks weak until the result card is read: it said 4 of 11 embedded images were re-encoded and, in the English rendering of its Chinese text, "7 images (3.10 MB) not re-encoded", with the reasons listed: CCITT, colour or bit-depth protection, scan-mask protection, JBIG2, JPEG 2000.

Adding up those seven gives 3,248,544 bytes, which is 71.7% of the whole file. They were kept byte for byte: the SHA-256 of the exported images matched before and after. The other three images (the RGB photo, the screenshot, the masked JPEG) went from 1,274,293 to 66,684 bytes. Of the 1,210,192 bytes saved, 99.8% came from those three, the photo alone 77.0%. The compressor did a lot, on a small slice of the file: about 28%, which is 1,274,293 of 4,529,487 bytes (calculated from those two numbers). If your picture of PDF images stops at JPEG and PNG, the seven are the surprise, because a PDF can carry JPEG 2000, JBIG2, CCITT and CMYK images as well.

What the more aggressive tools did instead

Ghostscript at its /ebook setting took the file to 235,036 bytes, a 94.8% cut, much smaller than the ImgIng result. CMYK, ICC and Lab images all became plain RGB, JPEG 2000 became ordinary JPEG, and JBIG2 became CCITT. It is small because the print colour space and original encodings are gone.

The macOS "Reduce File Size" filter gave 560,025 bytes (−87.6%) and did something odder to the one-bit pages. It turned the CCITT fax page into an 8-bit grey JPEG, and that image grew from 24,975 to 107,745 bytes, 4.3 times bigger, with softer letter edges. qpdf, which only rewrites structure, saved 0.7%; it only rewrites structure and barely touches images.

Colour moved too. On the CMYK image, the region's average colour changed by a ΔE of 4.57 in one renderer (MuPDF) and 4.21 in another (poppler) after the macOS filter. That is the average-colour difference of the region, one number per renderer, not a print-quality judgement. Forcing Ghostscript to re-encode everything and drop one-bit images to 100 dpi made the fax page's strokes break up.

One more experiment on the touchy CMYK image: the same JPEG bytes, with the /Decode entry deleted from the dictionary, render near-black in all three renderers tried, a ΔE of 49.3 from the original. That shows up when the dictionary and the bytes disagree; no tool produced it from the normal file. Separately, decoding those bytes in a browser gave an average colour 8.3 ΔE from what PDF viewers show. That is a reason not to guess at CMYK colour; how any particular tool decodes such an image was not looked at here.

So when the file stays big

ImgIng's documentation draws the line like this: when a browser cannot re-encode them reliably, JPEG 2000, JBIG2, CCITT fax pages, CMYK print JPEGs and images needing full ICC, Lab or DeviceN colour management are kept as they are. DeviceN was not tested, so that part is only the quoted sentence. If a PDF holds mostly those, a careful tool hands it back nearly unchanged, which is caution rather than failure.

A text-only PDF barely shrinks. On a separate 10-page text-only sample it was 4.8% in ImgIng, and macOS made that file 3.4% bigger; ImgIng's notes put text-only PDFs at roughly 5% to 20%. An encrypted or permission-locked PDF has to be unlocked first, and rewriting a digitally signed PDF breaks the signature. In these runs it was free with no sign-up, no upload requests showed up between choosing the file and saving it, and the page says parsing, image re-encoding and writing all happen on the device. Only a desktop browser was used; phones and Windows tools were not tested.

How much of that 71.7% a real file would hold is not answered here, since the sample was built to contain every kind at once. A separate two-page scanned sample shrank 72.9% at the same level, so the split depends on what the file contains. The cheap first check, before blaming the compressor, is what kinds of images the file holds.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.