{"record":{"id":"a1fb38a17c092fce","repo":"mozilla/pdf.js","slug":"bad-encoding-in-flate-stream","errorCode":null,"errorMessage":"Bad encoding in flate stream","messagePattern":"Bad encoding in flate stream","errorType":"exception","errorClass":"FormatError","httpStatus":null,"severity":"error","filePath":"src/core/flate_stream.js","lineNumber":200,"sourceCode":"      this.stream.dict\n    );\n    this.reset();\n    return null;\n  }\n\n  get isAsync() {\n    return this.#isAsync;\n  }\n\n  getBits(bits) {\n    const str = this.stream;\n    let codeSize = this.codeSize;\n    let codeBuf = this.codeBuf;\n\n    let b;\n    while (codeSize < bits) {\n      if ((b = str.getByte()) === -1) {\n        throw new FormatError(\"Bad encoding in flate stream\");\n      }\n      codeBuf |= b << codeSize;\n      codeSize += 8;\n    }\n    b = codeBuf & ((1 << bits) - 1);\n    this.codeBuf = codeBuf >> bits;\n    this.codeSize = codeSize -= bits;\n\n    return b;\n  }\n\n  getCode(table) {\n    const str = this.stream;\n    const codes = table[0];\n    const maxLen = table[1];\n    let codeSize = this.codeSize;\n    let codeBuf = this.codeBuf;\n","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/core/flate_stream.js#L182-L218","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Verify file completeness before loading — check for %%EOF marker\nasync function isPdfComplete(arrayBuffer) {\n  const bytes = new Uint8Array(arrayBuffer);\n  const tail = bytes.slice(-1024);\n  const text = new TextDecoder().decode(tail);\n  return text.includes('%%EOF');\n}","typeGuard":null,"tryCatchPattern":"try {\n  await page.render(renderParams).promise;\n} catch (err) {\n  if (err.message?.includes('Bad encoding in flate stream')) {\n    // stream is truncated — offer to re-download the PDF\n    showRetryDialog('PDF data appears incomplete. Please re-download.');\n  } else { throw err; }\n}","preventionTips":["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."],"tags":["flate","deflate","truncated","pdf-stream","format-error"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}