mozilla/pdf.js · error · FormatError

Invalid offset in the ObjStm stream.

Error message

Invalid offset in the ObjStm stream.

What it means

A FormatError raised in the second ObjStm pass: when slicing the stream into per-object sub-streams, offset[i+1] - offset[i] is negative, meaning the recorded offsets are not monotonically increasing. pdf.js computes each object's byte length from consecutive offsets; a negative length is impossible and the stream is internally inconsistent.

Source

Thrown at src/core/xref.js:1008

      // The entry in the xref table is the object number followed by the index.
      // So if index (gen number) is not the same as the index (i), we fix it
      // (fixes bug 1978317).
      const entry = this.getEntry(num);
      if (entry?.offset === tableOffset && entry.gen !== i) {
        entry.gen = i;
      }

      offsets[i] = offset;
    }

    const start = (stream.start || 0) + first;
    const entries = new Array(n);
    // read stream objects for cache
    for (let i = 0; i < n; ++i) {
      const length = i < n - 1 ? offsets[i + 1] - offsets[i] : undefined;
      if (length < 0) {
        throw new FormatError("Invalid offset in the ObjStm stream.");
      }
      parser = new Parser({
        lexer: new Lexer(
          stream.makeSubStream(start + offsets[i], length, stream.dict)
        ),
        xref: this,
        allowStreams: true,
      });

      const obj = parser.getObj();
      entries[i] = obj;
      if (obj instanceof BaseStream) {
        continue;
      }
      const num = nums[i],
        entry = this.#entries[num];
      if (entry && entry.offset === tableOffset && entry.gen === i) {
        if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Repair the PDF with qpdf --check / mutool clean to rebuild the object stream with sorted offsets.
  2. Re-export the source document.
  3. Catch FormatError around the failing page and continue with other pages.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await doc.getPage(n);
} catch (e) {
  if (e instanceof pdfjsLib.FormatError) { /* skip page */ }
}

Prevention

When it happens

Trigger: An ObjStm where the offset table is out of order (offset[i+1] < offset[i]); a writer that emitted offsets in a different order than the objects; decryption corruption that scrambled the offset values.

Common situations: Faulty generators, repaired-but-inconsistent files, partially overwritten object streams.

Related errors


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