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
- Repair with qpdf --object-streams=generate or mutool clean to rebuild object streams.
- Re-export from the originating application.
- Wrap per-object access in try/catch and degrade gracefully for the affected page.
- 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
- Pre-validate PDFs with qpdf/mutool before handing to pdf.js.
- Catch FormatError at page boundaries to localize damage.
- If you generate PDFs, conform ObjStm /Type /N /First headers.
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
- bad ObjStm stream
- invalid object number in the ObjStm stream: ${num}
- invalid object offset in the ObjStm stream: ${offset}
- Invalid offset in the ObjStm stream.
- Bad (compressed) XRef entry: ${ref}
AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13).
Data as JSON: /api/errors/1fa2b7ad9bf6324c.
Report an issue: GitHub.