mozilla/pdf.js · error · FormatError
Bad encoding in flate stream
Error message
Bad encoding in flate stream
What it means
Thrown by getBits() when the underlying byte stream returns -1 (EOF) before enough bits have been accumulated to satisfy the requested bit count. This means the DEFLATE-compressed data is truncated mid-code — the decoder needs more input bytes but the stream has ended.
Source
Thrown at src/core/flate_stream.js:200
this.stream.dict
);
this.reset();
return null;
}
get isAsync() {
return this.#isAsync;
}
getBits(bits) {
const str = this.stream;
let codeSize = this.codeSize;
let codeBuf = this.codeBuf;
let b;
while (codeSize < bits) {
if ((b = str.getByte()) === -1) {
throw new FormatError("Bad encoding in flate stream");
}
codeBuf |= b << codeSize;
codeSize += 8;
}
b = codeBuf & ((1 << bits) - 1);
this.codeBuf = codeBuf >> bits;
this.codeSize = codeSize -= bits;
return b;
}
getCode(table) {
const str = this.stream;
const codes = table[0];
const maxLen = table[1];
let codeSize = this.codeSize;
let codeBuf = this.codeBuf;
View on GitHub (pinned to 5903d58d58)
Solutions
- Verify the PDF file is complete and not truncated — compare file size against Content-Length or the PDF's %%EOF marker presence.
- Re-download or re-fetch the PDF from the original source to eliminate partial-transfer corruption.
- Run qpdf --check on the file to detect broken streams.
- If fetching via range requests, ensure the server supports byte-range and the full content-length is correct.
Defensive patterns
Strategy: try-catch
Validate before calling
// Verify file completeness before loading — check for %%EOF marker
async function isPdfComplete(arrayBuffer) {
const bytes = new Uint8Array(arrayBuffer);
const tail = bytes.slice(-1024);
const text = new TextDecoder().decode(tail);
return text.includes('%%EOF');
} Try / catch
try {
await page.render(renderParams).promise;
} catch (err) {
if (err.message?.includes('Bad encoding in flate stream')) {
// stream is truncated — offer to re-download the PDF
showRetryDialog('PDF data appears incomplete. Please re-download.');
} else { throw err; }
} Prevention
- Verify Content-Length matches actual bytes received after download.
- Check for %%EOF at end of file before passing to getDocument.
- Use PDF.js's onProgress callback to detect interrupted transfers.
When it happens
Trigger: Calling getBits(bits) during readBlock() Huffman decoding (e.g., this.getBits(5) for numLitCodes, this.getBits(3) for code lengths) when the wrapped stream has been exhausted. Fires only on the JS fallback decoder path after native DecompressionStream fails.
Common situations: Truncated PDF downloads or partial file reads. Corrupted object streams or cross-reference streams where the declared length exceeds actual data. Network interruptions during range-request fetching that leave a stream incomplete.
Related errors
- FDICT bit set in flate stream: ${cmf}, ${flg}
- Bad uncompressed block length in flate stream
- Unknown block type in flate stream
- Invalid header in flate stream: ${cmf}, ${flg}
- Unknown compression method in flate stream: ${cmf}, ${flg}
AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13).
Data as JSON: /api/errors/a1fb38a17c092fce.
Report an issue: GitHub.