mozilla/pdf.js · error · FormatError

FDICT bit set in flate stream: ${cmf}, ${flg}

Error message

FDICT bit set in flate stream: ${cmf}, ${flg}

What it means

Thrown by the FlateStream constructor when the zlib header's FLG byte has bit 5 (0x20) set, indicating a preset dictionary (FDICT) is present. PDF.js's JavaScript DEFLATE decoder does not support preset dictionaries, so it rejects the stream outright rather than attempting decompression without the dictionary. The cmf and flg values are included in the message for diagnostics.

Source

Thrown at src/core/flate_stream.js:147

    this.stream = str;
    this.dict = str.dict;

    const cmf = str.getByte();
    const flg = str.getByte();
    if (cmf === -1 || flg === -1) {
      throw new FormatError(`Invalid header in flate stream: ${cmf}, ${flg}`);
    }
    if ((cmf & 0x0f) !== 0x08) {
      throw new FormatError(
        `Unknown compression method in flate stream: ${cmf}, ${flg}`
      );
    }
    if (((cmf << 8) + flg) % 31 !== 0) {
      throw new FormatError(`Bad FCHECK in flate stream: ${cmf}, ${flg}`);
    }
    if (flg & 0x20) {
      throw new FormatError(`FDICT bit set in flate stream: ${cmf}, ${flg}`);
    }

    this.codeSize = 0;
    this.codeBuf = 0;
  }

  async getImageData(length, _decoderOptions) {
    const data = await this.asyncGetBytes();
    if (!data) {
      return this.getBytes(length);
    }
    if (data.length <= length) {
      return data;
    }
    return data.subarray(0, length);
  }

  async asyncGetBytes() {

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Re-export or re-save the source PDF with a standard PDF library (e.g., Ghostscript, qpdf) to regenerate clean FlateDecode streams without preset dictionaries.
  2. Validate the PDF with qpdf --check or a similar tool to confirm structural integrity before loading it with pdf.js.
  3. If you control PDF generation, ensure zlib.deflate is called without a preset dictionary (no zdict argument).
  4. Catch the error at the page-render level and fall back to a placeholder or re-fetch the document from the source.

Example fix

// before — generating a compressed PDF stream with a dictionary
const deflated = zlib.deflateSync(data, { dictionary: presetDict });

// after — omit the dictionary so the FLG FDICT bit is never set
const deflated = zlib.deflateSync(data);
Defensive patterns

Strategy: fallback

Validate before calling

// Before loading, check the PDF's stream filters for FlateDecode and
// pre-scan the first object stream bytes for the FDICT bit.
// This requires access to raw PDF bytes — only feasible server-side.
async function checkFlateHeader(bytes) {
  // find zlib-compressed streams; check FLG byte (byte index 1)
  const flg = bytes[1];
  if (flg & 0x20) {
    return { ok: false, reason: 'FDICT bit set — preset dictionary not supported' };
  }
  return { ok: true };
}

Try / catch

// pdf.js display API: catch at the page/task level
try {
  const page = await pdfDoc.getPage(pageNum);
  const renderTask = page.render({ canvasContext, viewport });
  await renderTask.promise;
} catch (err) {
  if (err instanceof Error && err.message.includes('FDICT bit set in flate stream')) {
    console.warn('Unsupported zlib preset dictionary in stream; page may be incomplete.');
    // render placeholder or skip
  } else {
    throw err;
  }
}

Prevention

When it happens

Trigger: Constructing a new FlateStream(stream, maybeLength) where the underlying stream's first two bytes form a zlib header with (flg & 0x20) !== 0. This occurs when PDF.js falls back to its JS decoder after the browser's native DecompressionStream API also fails on a stream that carries a preset dictionary.

Common situations: A PDF whose FlateDecode stream was compressed with a preset dictionary (rare in well-formed PDFs). Truncated or byte-corrupted PDF files where the header bytes coincidentally have the FDICT bit set. Hand-crafted or minimally-edited PDFs produced by non-standard tooling.

Related errors


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