mozilla/pdf.js · error · FormatError

Missing endstream command.

Error message

Missing endstream command.

What it means

Error "Missing endstream command." thrown in mozilla/pdf.js.

Source

Thrown at src/core/parser.js:765

    // Get the length.
    let length = dict.get("Length");
    if (!Number.isInteger(length)) {
      info(`Bad length "${length && length.toString()}" in stream.`);
      length = 0;
    }

    // Skip over the stream data.
    stream.pos = startPos + length;
    lexer.nextChar();

    // Shift '>>' and check whether the new object marks the end of the stream.
    if (this.tryShift() && isCmd(this.buf2, "endstream")) {
      this.shift(); // 'stream'
    } else {
      // Bad stream length, scanning for endstream command.
      length = this.#findStreamLength(startPos);
      if (length < 0) {
        throw new FormatError("Missing endstream command.");
      }

      lexer.nextChar();
      this.shift();
      this.shift();
    }
    this.shift(); // 'endstream'

    stream = stream.makeSubStream(startPos, length, dict);
    const filter = dict.get("F", "Filter");
    // Streams that explicitly use `/Crypt` are decrypted in the filter chain,
    // so avoid applying the default stream cipher up-front.
    if (cipherTransform && !this.#hasCryptFilter(filter)) {
      stream = cipherTransform.createStream(stream, length);
    }
    stream = this.filter(stream, dict, length, cipherTransform);
    stream.dict = dict;
    return stream;

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Ensure the stream data ends with an `endstream` keyword; check that the /Length entry of the stream dictionary matches the actual stream data size.
  2. If the PDF is truncated, re-download or regenerate the file so the stream terminates correctly.

When it happens

Trigger: Thrown at src/core/parser.js:765 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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