mozilla/pdf.js · error · FormatError

Unknown block type in flate stream

Error message

Unknown block type in flate stream

What it means

Thrown by readBlock() when the 2-bit block-type field (BTYPE) read from the DEFLATE stream is 3 (binary 11), which is a reserved/invalid value per RFC 1951. Only values 0 (stored), 1 (fixed Huffman), and 2 (dynamic Huffman) are valid; the decoder cannot proceed.

Source

Thrown at src/core/flate_stream.js:412

        } else {
          codeLengths[i++] = len = code;
          continue;
        }

        let repeatLength = this.getBits(bitsLength) + bitsOffset;
        while (repeatLength-- > 0) {
          codeLengths[i++] = what;
        }
      }

      litCodeTable = this.generateHuffmanTable(
        codeLengths.subarray(0, numLitCodes)
      );
      distCodeTable = this.generateHuffmanTable(
        codeLengths.subarray(numLitCodes, codes)
      );
    } else {
      throw new FormatError("Unknown block type in flate stream");
    }

    buffer = this.buffer;
    let limit = buffer ? buffer.length : 0;
    let pos = this.bufferLength;
    while (true) {
      let code1 = this.getCode(litCodeTable);
      if (code1 < 256) {
        if (pos + 1 >= limit) {
          buffer = this.ensureBuffer(pos + 1);
          limit = buffer.length;
        }
        buffer[pos++] = code1;
        continue;
      }
      if (code1 === 256) {
        this.bufferLength = pos;
        return;

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Obtain a pristine copy of the PDF from the original source.
  2. Rewrite the PDF with qpdf --check=verbose to identify and repair the damaged stream object.
  3. If generating PDFs, use a mature library (e.g., pdfkit, iText, Ghostscript) rather than hand-rolling DEFLATE output.
  4. Wrap page rendering in error handling and render remaining pages.
Defensive patterns

Strategy: try-catch

Validate before calling

// Check PDF stream integrity with zlib server-side
const zlib = require('zlib');
function canInflate(buf) {
  try { zlib.inflateSync(buf); return true; }
  catch { return false; }
}

Try / catch

try {
  await page.render(renderParams).promise;
} catch (err) {
  if (err.message?.includes('Unknown block type in flate stream')) {
    console.error('Severe DEFLATE corruption; PDF may be damaged.');
    showErrorToUser('This PDF appears to be corrupted.');
  } else { throw err; }
}

Prevention

When it happens

Trigger: readBlock() reads hdr = getBits(3), shifts right by 1, and the resulting block type is not 0, 1, or 2 — i.e., the raw 2-bit value was 3. This means the block header bits are corrupt or the stream has fallen out of sync.

Common situations: Severe bit-level corruption in a compressed stream. A stream that was truncated and then padded with garbage bytes. Misaligned parsing caused by earlier corruption that shifted the bit-reader's position. PDFs produced by buggy or experimental generators.

Related errors


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