← Blog
How it works AegisPDF

A render that succeeds can still draw nothing

“A customer says a scanned page shows up blank in our viewer. Other pages are fine, the page count is right, search still finds text, and our code logged no error at all, so I have nothing to go on and no idea whether the file is broken or we are.”

A viewer draws a five page PDF. The page count reads 1 / 5. The text layer is selectable, the highlight rectangles land where they should, and the OCR text in the side panel is intact. The scanned page itself is white. Nothing threw.

That report is real, and its cause is worth a post, because the thing that failed is not in the file and not in the code that reads it. It is a directory that was never deployed.

What moved

pdf.js used to decode every image filter in JavaScript it shipped in its own bundle. It no longer does, and the change came in steps. JPEG 2000 went first: release 5.0.375 (March 2025) moved the OpenJPEG decoder into a separate .wasm file, "which requires setting a new wasmUrl API option in order to use it". JBIG2 followed in 5.4.624 (February 2026), and in 5.7.284 (April 2026) the old built-in CCITT and JBIG2 decoders were deleted and replaced by "a JS version of the PDFium decoder" that lives in the same directory as the WebAssembly one. Release 6.0.227 then tidied the code into "an abstract WasmImage class, that JBig2CCITTFaxImage and JpxImage inherit from". Those two names cover the formats that scanners and fax machines produce: /CCITTFaxDecode, /JBIG2Decode and /JPXDecode. So the failure below applies to JPEG 2000 from 5.0 and to fax and JBIG2 scans from 5.7.

In pdfjs-dist 6.4.299 the implementation is plain enough to read. JBig2CCITTFaxImage declares

_filename = "jbig2.wasm";
_noWasmFilename = "jbig2_nowasm_fallback.js";

and JpxImage declares openjpeg.wasm and openjpeg_nowasm_fallback.js. Both inherit a #instantiateWasm that fetches ${WasmImage.#wasmUrl}${this._filename}. That #wasmUrl comes from one API parameter, documented in src/display/api.js as:

@property {string} [wasmUrl] - The URL where the wasm files are located.
  Include the trailing slash.

It sits in the same list as cMapUrl and standardFontDataUrl, two parameters that have been there for years, and it looks just as optional. Its reach is not comparable. CMaps matter for certain East Asian encodings; the standard font data matters for PDFs that do not embed their fonts. wasmUrl now governs whether an ordinary fax renders at all.

Five files in the package's wasm/ directory are named by the worker build: jbig2.wasm at 103,671 bytes, openjpeg.wasm at 251,131, qcms_bg.wasm at 96,589 for ICC colour spaces, and the two JavaScript fallbacks at 152,009 and 445,781. That is 1,049,181 bytes in all, a little over a mebibyte, and a bundler that copies pdf.mjs and pdf.worker.mjs has no reason to notice them.

One detail is worth pausing on. A single module, jbig2.wasm, serves both JBIG2 and CCITT: the class calls module._ccitt_decode or module._jbig2_decode out of the same instance. So a plain Group 4 fax, the most unremarkable scanner output there is, depends at run time on a file named after JBIG2.

Measured

We built two PDFs and rendered them in Chromium against pdfjs-dist 6.4.299, the version the report pins.

The first holds one /CCITTFaxDecode image, 1728 by 2156, real Group 4 data taken from a single strip TIFF, plus two lines of ordinary Helvetica text in the bottom band. The source raster is 23.54% black. Rendered at 100 dpi by poppler's pdftoppm, an independent engine, the page comes out 18.87% dark. That is the ground truth. Getting there also settled a detail by measurement rather than by reading: the strip needs /BlackIs1 true to match its source, because the TIFF is photometric=1, and with the PDF default of false poppler drew the exact inverse, 76.95% dark.

Then six configurations, counting pixels below mid grey on the canvas pdf.js drew:

wasmUrl other options render promise dark pixels of 902,700
A directory served defaults resolved 169,683 (18.80%)
B directory 404s defaults resolved 2,872 (0.32%)
C not passed at all defaults resolved 2,872 (0.32%)
D directory served useWasm: false resolved 169,683 (18.80%)
E directory 404s useWasm: false resolved 2,872 (0.32%)
F directory 404s stopAtErrors: true resolved 2,872 (0.32%)

A matches poppler to within a fifth of a percentage point. The 2,872 pixels left in every failing case are the two text lines. A second fixture, the same page with a /JPXDecode JPEG 2000 image in place of the fax, behaves identically: 18.68% with the directory, 0.20% without, and the failing run names openjpeg.wasm instead.

In every failing case page.getTextContent() returned both strings unchanged and doc.numPages was correct. Only the scan is gone.

Three things that follow

There is no fallback, because the fallback lives in the same place. #instantiateWasm catches its own failure and calls #getJsModule, which imports ${WasmImage.#wasmUrl}${this._noWasmFilename}. That is the same directory. A deploy that dropped wasm/ dropped jbig2_nowasm_fallback.js with it, so the pure JavaScript decoder written precisely for the case where WebAssembly is unavailable is unreachable for exactly the reason it was wanted. Case B's console shows both misses in order:

Warning: #instantiateWasm: UnknownErrorException: Unable to load wasm data at: /wasm_missing/jbig2.wasm
Warning: #getJsModule: TypeError: Failed to fetch dynamically imported module: .../wasm_missing/jbig2_nowasm_fallback.js
Warning: Unable to decode image "img_p0_1": "Jbig2Error: JBig2 failed to initialize".

useWasm: false does not remove the dependency. The option reads like a way to opt out of the whole arrangement. Compare D with E. With the directory served, useWasm: false renders the page perfectly, because it loads the fallback .js from that same directory. With the directory missing it fails exactly as the default does. The option chooses which file you need, not whether you need the directory.

The error channel never mentions it. This is the part worth keeping. stopAtErrors is documented as a way to "Reject certain promises, e.g. getOperatorList, getTextContent, and RenderTask, when the associated PDF data cannot be successfully parsed, instead of attempting to recover whatever possible of the data". Case F turns it on. The render promise still resolves, and the page is still blank. An application that wraps await page.render(...).promise in try/catch, which is every careful application, learns nothing in any of the six configurations. The only two signals are a console warning, which no code of yours is reading in production, and the pixels themselves.

So check it where it is still a fact

A missing asset is a static property of a deployment. By render time it has decayed into an absence that nothing reports. So test it at the boundary where it is still checkable: assert, in the build or in a test that runs against the deployed artifact, that every file the worker names is reachable at wasmUrl. Check the fallbacks as well as the .wasm files, and check those two differently. pdf.js reads a .wasm file as plain bytes and hands them to WebAssembly.instantiate, so its content type does not matter to pdf.js. The fallback .js files are loaded with a dynamic import(), and browsers refuse to run a module that is not served with a JavaScript content type. So a host that gets the .js type wrong breaks the fallback while the .wasm path still works, and you would not notice until WebAssembly failed for some other reason. The fix for the report that prompted this post went further and added a test that the app serves each decoder with the WebAssembly type, which costs nothing and is worth copying.

If you would rather have a run time signal, the honest one is a single fetch of one named file when the viewer initialises, before any document is opened, treated as a hard failure. That is cruder than catching an exception. It is also the only thing in the list that actually fires.

Where AegisPDF fits

AegisPDF does not use pdf.js. At its prod commit ba4da99 there is no pdfjs-dist dependency anywhere, lockfile included, and PrivHaven has none at its own prod either. AegisPDF renders with PDFium, the PDF engine in Chrome, compiled to WebAssembly and driven from our Rust engine. PDFium's fax, JBIG2 and JPEG 2000 decoders are compiled into that one binary, so there is no separate decoder file that can go missing on its own. If the binary itself is missing, no page renders at all, which nobody mistakes for a blank scan.

We do have the same shape of dependency in another place, and until 29 November it failed the way this post describes. Premium OCR loads ONNX Runtime Web, and its ort-wasm-simd-threaded.jsep.wasm is about 25.02 MiB, just over Cloudflare Workers' 25 MiB per asset cap. It cannot ship in the bundle, so the build strips it and production loads it from our own R2 host, with the version in the path. One directory, fetched at run time, holding something a feature cannot work without. And it is not only Premium mode that needs it: standard OCR re-reads a low-confidence page with Premium on its own, so a plain run over a poor scan reaches that directory too.

Until the AegisPDF release of 29 November, a failure there was quiet. The OCR run rolled the document back and set an error message, then redrew the page, and the redraw replaced the message with the usual file name and page number. The user got their document back unchanged and no reason. It was the same failure as the pdf.js one, an error that existed only in the console. It was found and fixed alongside other work on the same function, not by anyone hitting it, as far as we know.

Now, at ba4da99:

  • If the runtime cannot load, the run stops on that page and says so: "OCR stopped on page N", the reason, and which pages kept their new text layer. The pages before it keep their layer. If no page had finished, the message says "OCR failed on page N" and that nothing was changed.
  • A failed load is no longer remembered for the rest of the session. Before, one dropped download broke OCR until the page was reloaded. Now running "OCR all" again tries the download again, and it only OCRs the pages that still have no text.
  • The build fails if any ONNX Runtime wasm file is still in the bundle after the strip, and it warns when the R2 address is not set, because the code would then look for the runtime on our own origin, where it no longer is.

One gap is still open. Our production workflow notes that it "Assumes just r2-ort-upload has published this version", and it does exactly that. Nothing in the deploy checks that the files are at that URL before shipping a build that depends on them. That is the check this post argues for, and we are describing it here, not claiming it.

What was and was not verified

Measured here: pdfjs-dist 6.4.299 in Chromium, two fixtures we built, one /CCITTFaxDecode and one /JPXDecode, each with real text on the same page, at 100 dpi, against pdftoppm from poppler as the second engine. Pixel counts are from the canvas pdf.js drew.

Not measured. We did not build a /JBIG2Decode fixture, having no JBIG2 encoder to hand; the claim that it fails the same way rests on reading the class, which decodes both formats through one module, and not on a render. We did not test the ICC path that qcms_bg.wasm serves, which in the source sets an internal isUsable to false rather than raising, and which we would expect to degrade more quietly still. We tested one browser engine, not two, because the behaviour under examination is the library's own fetch and error handling rather than anything the browser decides. We did not run the application in the report that prompted this; every statement about it is attributed to its issue and its fix commit.

The AegisPDF section is a reading of the source at prod ba4da99 and of the code it replaced at 6ec7a14, with the new messages confirmed in the JavaScript that aegispdf.com served on 29 November. We did not remove the runtime from a live deploy to watch it fail. The closest thing is AegisPDF's own browser tests, which make the Premium model's download fail, a different file on the same path, and check that the run keeps the finished pages, says where it stopped and changes nothing when no page had finished. That PDFium's decoders sit inside its one binary comes from PDFium's source layout, not from taking our build apart.

Versions move. wasmUrl, useWasm and stopAtErrors are the parameter names in 6.4.299, and the pixel counts belong to that version.