Every guide about compressing PDFs talks about file size. Almost none talk about the thing that actually breaks the workflow: page order.

I learned this the hard way while building a small batch PDF tool. Here is what actually goes wrong, and how to check for it.

The failure mode

You compress a 25 MB scan to 3.8 MB. The file opens, the size is right, and you send it. Then the recipient says "page 7 is missing" or "the pages are out of order".

Two separate bugs hide behind that:

  1. The compressor re-encodes pages and drops one. A 40-page document comes out with 39 pages, or 41.
  2. The pages survive but get shuffled. This happens with parallel page processing, or with tools that split and re-merge the document internally.

The first one is detectable by counting pages. The second one is not.

Why a page count is not enough

If you only compare len(original.pages) == len(output.pages), a shuffle passes silently. The file has the same number of pages, the same size, and opens fine. Only the content is in the wrong order.

To catch it you have to compare page by page:

  • For text PDFs: extract text from page N of both documents, take the first 200 characters, normalize whitespace, hash it, and compare.
  • For scanned PDFs: there is no text layer, so compare the page image instead.

Scanned pages are the hard part

The obvious approach is a small thumbnail: render each page to 16x16 grayscale and compare average intensity. That works for pages with very different ink coverage, and fails for ordinary white-paper scans. Two pages of text look almost identical at 16x16.

Better signals, in order:

  1. Exact bytes. If the page was not re-encoded, the image stream hash is identical. This catches a shuffle perfectly, and costs nothing.
  2. Perceptual hash. When the page was re-encoded, bytes change. A difference hash (compare adjacent pixels on a 9x8 grayscale downsample, 64 bits) survives JPEG re-encoding but still separates different pages. Compare with a Hamming distance threshold.
  3. Image count and dimensions. If a page had two images and now has one, that is a structural change worth flagging.

The combination matters: exact hash first, perceptual hash as a fallback. One alone either misses shuffles or produces false alarms after compression.

What to do when it fails

Do not "fix" the output. Delete it, keep the original, and report the page number. A compression tool that silently returns a broken document is worse than one that refuses to compress.

A checklist for any batch PDF tool

  • Does it verify page count after every operation?
  • Does it compare page content, not just count?
  • Does it handle scanned pages without a text layer?
  • Does it delete the output when verification fails?
  • Does it leave the original untouched?

If the answer to any of these is "no", the tool is one bad scan away from a very awkward email.

I built BatchPDF around this checklist: target-size compression, page-by-page verification for text and scans, merge with bookmarks, split by ranges, and a manifest of what happened to every file.