Whether a scanned PDF page can be decoded is a property of the deployment, not of the file. In this run, the same two pages went through a handful of common PDF tools on one Apple-silicon Mac, and the outcomes were: decoded cleanly, raised an error, printed a warning and returned an empty image, and quietly listed one image layer fewer. Nothing about the PDF changed between those runs. Only the decoder that happened to be present did. Treat "could not decode" as a result your code reports, not as a gap it papers over.
What the pages look like inside
Both samples are single pages split out of public-domain scans. Their page images are not plain JPEGs. The English page is stored as separate layers: a JPEG 2000 background, a JPEG 2000 foreground colour layer, and a 1-bit JBIG2 mask that carries the glyph shapes. The Chinese page uses the same kinds of encodings, two JPEG 2000 images plus a JBIG2 mask. On both, on top sits an invisible text layer added by the archive's OCR. Without that text layer, a tool that cannot read those image formats has nothing to fall back on. With it, the failure can hide.
The English page comes from the Internet Archive's copy of the 1921 edition of Psycho-Analysis and the War Neuroses (Ferenczi, Abraham, Simmel, Jones, preface by Freud), Public Domain Mark 1.0. Its reference text was proofread by hand against the page image. The Chinese page is half a page of the 1986 edition of the Simplified Character Table from the National Language Commission, also an Internet Archive item under Public Domain Mark 1.0. Two pages say little about scans in general; they only show what these two files did.
Four outcomes from the same file
| Tool | What happened on both pages |
|---|---|
| poppler, Ghostscript, macOS Quartz, PyMuPDF | Decoded all three layers, no warning |
| pdf.js with its wasm decoders available | Decoded both images |
| Pillow (with OpenJPEG) | Opened the JPEG 2000 layers, raised UnidentifiedImageError on the bare JBIG2 stream |
| pypdf image listing | Listed only the two JPEG 2000 layers; the mask was absent, with no message |
| pdf.js with the wasm path deliberately left out (a simulated deployment) | A warning per image (two lines here) that OpenJPEG failed to initialize, image objects empty, text layer returned as usual, no exception |
The last row is the one that deserves a design review. The text layer came back intact, so a caller that only looked at the return value would see a successful extraction. The warning was the only sign that the image half had failed. The pypdf row is quieter still, since there is not even a warning.
Why the file is not the variable
pdf.js loads its image decoders as wasm modules from a path the host application configures, and the last row was produced by leaving that path out; it does not mean pdf.js cannot decode these files by default. What follows is inference, not something that was run: a bundler that drops the folder, a CSP that blocks the fetch, or a serverless package that omits the files could each produce the same row without touching the PDF, and a Pillow build without OpenJPEG would behave differently from the one used here. Whoever tests locally with a full toolchain will see the clean rows and may conclude the format is handled.
A related trap sits one level down. On the English page the three layers do not hold equal amounts of text. Recognizing only the 755 by 1163 background layer returned 186 characters at a 93.62% character error rate, because that layer is the page with the writing wiped out. Recognizing the rendered whole page gave 0.26%. Decoding "an image" is not the same as decoding the right image. This is one page's layout; the Chinese page is stored differently and its first image does carry text.
Recording the result instead of hiding it
The cheap fix is to make "decoded or not" an explicit field per image: decoded, failed with a reason, or not listed. A pipeline can then refuse to treat a text-layer-only result as complete, or route the page to a path that renders it into a plain bitmap first. OCRmyPDF is a useful contrast. By default it exits with code 6 or 2 when it meets an existing text layer; with --skip-text it writes a single log line saying the page was skipped, and exits 0 with the old layer untouched. Both can be found by a pipeline that looks, which is less true of the quieter rows above.
Where this stops
This is two pages, one machine, one run each, and the tool versions are whatever was current at the time. The Windows side and other PDF engines were not tested. Which tools lose which layer will change with versions and builds, so the table is a list of failure shapes to look for, not a ranking. The text layers on these pages also differed in quality (0.15% errors on one, 8.98% on the other), so a layer being present says nothing about it being good.
ImgIng, at https://imging.ai/ , runs PDF content extraction in the browser, merging native text with text recognized inside images. For scans whose page images are JPEG 2000 or JBIG2 encoded, it currently cannot recognize them, so such files need to be rendered to ordinary images first. That is the same rule as above, seen from the user's side.
Top comments (1)
This is the kind of failure that keeps me up at night, because the worst row in your table isn't the one that errors — it's the one that looks fine. pdf.js returning empty image objects while the text layer stays intact is a silent-corruption trap: a caller checking "did I get text?" would happily ship a page with no visible content at all. Same for pypdf listing two layers instead of three with no message — the absence itself is the bug, and nothing in the output says so.
Your fix direction feels right: "decoded or not" has to be an explicit per-image field, not something inferred from whether the pipeline crashed. I'd go one step further and make a known-bad canary PDF part of the deploy itself — run it on every new environment and fail loudly if the JBIG2 mask or JPEG 2000 layers come back empty, before any real user file touches it. A warning buried in two log lines is not a signal anyone will see in production.
One thing I'm curious about: of the four outcomes, which one would you actually design for first — the loud error, or the silent partial success? And have you thought about what a minimal canary file would need to contain to catch all three failure modes in one run?