mozilla/pdf.js · error · Jbig2Error

JBig2 failed to initialize

Error message

JBig2 failed to initialize

What it means

Thrown by JBig2CCITTFaxImage.decode when _getModule(JBig2) returns a falsy module, meaning neither the wasm build (jbig2.wasm) nor the JS fallback (jbig2_nowasm_fallback.js) could be loaded. JBIG2/CCITT decoding is entirely module-backed, so a missing module prevents any decoding.

Source

Thrown at src/core/jbig2_ccittFax.js:43

class JBig2CCITTFaxImage extends WasmImage {
  _filename = "jbig2.wasm";

  _noWasmFilename = "jbig2_nowasm_fallback.js";

  static get instance() {
    return shadow(
      this,
      "instance",
      new JBig2CCITTFaxImage(/* trackInstance = */ true)
    );
  }

  async decode(bytes, width, height, globals, CCITTOptions) {
    const module = await this._getModule(JBig2);

    if (!module) {
      throw new Jbig2Error("JBig2 failed to initialize");
    }
    let ptr, globalsPtr;

    try {
      const size = bytes.length;
      ptr = module._malloc(size);
      module.writeArrayToMemory(bytes, ptr);

      if (CCITTOptions) {
        module._ccitt_decode(
          ptr,
          size,
          width,
          height,
          CCITTOptions.K,
          CCITTOptions.EndOfLine ? 1 : 0,
          CCITTOptions.EncodedByteAlign ? 1 : 0,
          CCITTOptions.BlackIs1 ? 1 : 0,

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Provide a correct wasmUrl and ship jbig2.wasm alongside qcms_bg.wasm from pdfjs-dist.
  2. Ensure useWorkerFetch:true is set on getDocument so the worker can resolve the module URL.
  3. Adjust CSP to allow worker-src and wasm; serve .wasm as application/wasm.
  4. If wasm is unavailable, ensure the non-wasm fallback JS file is reachable at the configured location.

Example fix

// before
getDocument({ url });

// after
getDocument({
  url,
  useWorkerFetch: true,
  wasmUrl: '/pdfjs-dist/web/',  // contains jbig2.wasm
});
Defensive patterns

Strategy: validation

Validate before calling

// Validate the decoder asset configuration before loading:
function jbig2Configured(opts) {
  return !!(opts && opts.useWorkerFetch && opts.wasmUrl);
}

Type guard

function hasDecoderAssetConfig(opts) {
  return opts != null && opts.useWorkerFetch === true &&
    typeof opts.wasmUrl === 'string' && opts.wasmUrl.length > 0;
}

Try / catch

try { await page.render({ canvasContext }).promise; }
catch (err) {
  if (err?.name === 'Jbig2Error' && /failed to initialize/.test(err.message)) {
    console.warn('JBIG2 module unavailable; check wasmUrl/useWorkerFetch.');
  } else throw err;
}

Prevention

When it happens

Trigger: Decoding a JBIG2-encoded image or a CCITT Group 3/4 image while the jbig2.wasm asset cannot be fetched (wrong wasmUrl, 404, CSP block) AND the non-wasm fallback also failed to load. Reached via getDocument rendering any JBIG2 image.

Common situations: Deployments that omit the JBIG2 assets, set an incorrect wasmUrl, run under a CSP that blocks the worker/wasm fetch, or disable worker fetch. Also seen in Node.js usage where the fallback loader has no DOM.

Related errors


AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13). Data as JSON: /api/errors/e6967fd78fd1e1f0. Report an issue: GitHub.