mozilla/pdf.js · error · FormatError

invalid first and n parameters for ObjStm stream

Error message

invalid first and n parameters for ObjStm stream

What it means

A FormatError from XRef.fetchCompressed when an Object Stream's /First or /N dictionary entries are not integers. /N declares how many objects the stream contains and /First the byte offset where object data begins; without both as integers the stream cannot be interpreted. The stream object itself was valid, but its descriptive header is corrupt.

Source

Thrown at src/core/xref.js:966

          xrefEntry !== undefined,
          'fetchUncompressed: The "xrefEntry" cannot be undefined.'
        );
      }
      this.#cacheMap.set(num, xrefEntry);
    }
    return xrefEntry;
  }

  fetchCompressed(ref, xrefEntry, suppressEncryption = false) {
    const tableOffset = xrefEntry.offset;
    const stream = this.fetch(Ref.get(tableOffset, 0));
    if (!(stream instanceof BaseStream)) {
      throw new FormatError("bad ObjStm stream");
    }
    const first = stream.dict.get("First");
    const n = stream.dict.get("N");
    if (!Number.isInteger(first) || !Number.isInteger(n)) {
      throw new FormatError("invalid first and n parameters for ObjStm stream");
    }
    let parser = new Parser({
      lexer: new Lexer(stream),
      xref: this,
      allowStreams: true,
    });
    const nums = new Array(n);
    const offsets = new Array(n);
    // read the object numbers to populate cache
    for (let i = 0; i < n; ++i) {
      const num = parser.getObj();
      if (!Number.isInteger(num)) {
        throw new FormatError(
          `invalid object number in the ObjStm stream: ${num}`
        );
      }
      const offset = parser.getObj();
      if (!Number.isInteger(offset)) {

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Repair with qpdf --object-streams=generate or mutool clean to rebuild object streams.
  2. Re-export from the originating application.
  3. Wrap per-object access in try/catch and degrade gracefully for the affected page.
  4. If authoring, always emit integer /N and /First in ObjStm dicts.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await doc.getPage(n);
} catch (e) {
  if (e instanceof pdfjsLib.FormatError) {
    // mark page unreadable, continue with the rest
  }
}

Prevention

When it happens

Trigger: An ObjStm whose stream dict is missing /First or /N, has them as floats/strings, or where encryption produced garbage values; a writer bug that wrote fractional or null values.

Common situations: Damaged object-stream headers from faulty generators; post-processing that stripped dict entries; partial decryption corruption.

Related errors


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