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
- Re-export the PDF through Ghostscript or Acrobat to normalize stream compression to standard zlib/deflate.
- If authoring, ensure your zlib writer emits the standard RFC-1950 header (CM=8, CINFO<=7) before the deflate payload.
- Repair with qpdf which can rebuild and re-encode stream filters.
- 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
- Re-export PDFs through Ghostscript or Acrobat to normalize all streams to standard zlib/deflate.
- When authoring, ensure your zlib writer emits the RFC-1950 header (CM=8, CINFO<=7).
- Screen PDFs with qpdf --check to catch mislabeled stream filters.
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
- Invalid header in flate stream: ${cmf}, ${flg}
- Bad FCHECK in flate stream: ${cmf}, ${flg}
- FontFile should be a stream
- FDICT bit set in flate stream: ${cmf}, ${flg}
- Invalid entry in 'Differences' array: ${data}
AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13).
Data as JSON: /api/errors/a98de314e2f8c472.
Report an issue: GitHub.