mozilla/pdf.js · error · FormatError

Unknown compression method in flate stream: ${cmf}, ${flg}

Error message

Unknown compression method in flate stream: ${cmf}, ${flg}

What it means

Thrown by the FlateStream constructor when the CMF byte's low nibble (compression method) is not 8 — the only legal value per RFC 1950 (deflate). Any other method (0-7, 9-15) is not a deflate stream; PDF.js supports only method 8. This is the second of four sequential header-validation guards and fires only after the header bytes were successfully read (error 117 did not trigger).

Source

Thrown at src/core/flate_stream.js:139

  5,
];

class FlateStream extends DecodeStream {
  #isAsync = true;

  constructor(str, maybeLength) {
    super(maybeLength);

    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);

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Re-export the PDF through Ghostscript or Acrobat to normalize stream compression to standard zlib/deflate.
  2. If authoring, ensure your zlib writer emits the standard RFC-1950 header (CM=8, CINFO<=7) before the deflate payload.
  3. Repair with qpdf which can rebuild and re-encode stream filters.
  4. Isolate the failing page/stream and continue processing others via per-page try/catch.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await page.render({ canvasContext, viewport });
} catch (e) {
  if (e.name === 'FormatError' && /Unknown compression method in flate stream/.test(e.message)) {
    // stream is not standard zlib/deflate — re-encode the PDF
  } else throw e;
}

Prevention

When it happens

Trigger: A PDF stream declared as /FlateDecode whose first two bytes do not encode compression method 8 — e.g. the bytes are actually a raw deflate stream (no zlib wrapper), a gzip stream (method 0 with different framing), or random/corrupt data. Fires during FlateStream construction when the method nibble check fails.

Common situations: Producers that wrote raw deflate (no zlib header) but declared /FlateDecode, PDFs whose streams were re-compressed with a different algorithm by a faulty tool, or byte-level corruption of the stream header. Also seen when a stream was accidentally double-compressed or mislabeled.

Related errors


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