mozilla/pdf.js · error · Error
Invalid pageIndex request.
Error message
Invalid pageIndex request.
What it means
Thrown by PDFDocumentProxy.getPageIndex(ref) when the supplied reference does not look like a PDF object reference. The guard isRefProxy requires an object with integer properties num>=0 and gen>=0 (matching an indirect object identifier). It exists because the worker message GetPageIndex needs a real reference; anything else cannot be resolved to a page.
Source
Thrown at src/display/api.js:3059
}
const page = new PDFPageProxy(
pageIndex,
pageInfo,
this,
this.pagesMapper,
this._params.pdfBug
);
this.#pageCache.set(pageIndex, page);
return page;
});
this.#pagePromises.set(pageIndex, promise);
return promise;
}
async getPageIndex(ref) {
if (!isRefProxy(ref)) {
throw new Error("Invalid pageIndex request.");
}
const index = await this.messageHandler.sendWithPromise("GetPageIndex", {
num: ref.num,
gen: ref.gen,
});
const pageNumber = this.pagesMapper.getPageNumber(index + 1);
if (pageNumber === 0) {
throw new Error("GetPageIndex: page has been removed.");
}
return pageNumber - 1;
}
getAnnotations(pageIndex, intent) {
return this.messageHandler.sendWithPromise("GetAnnotations", {
pageIndex: this.pagesMapper.getPageId(pageIndex + 1) - 1,
intent,
});
}View on GitHub (pinned to 5903d58d58)
Solutions
- Validate the value with the same shape as isRefProxy before calling: object with integer num>=0 and gen>=0.
- If the source is a destination array, check Array.isArray(dest) && typeof dest[0] === 'object' before calling getPageIndex(dest[0]).
- Use pdfDocument.getPageIndex only after pdfDocument.getPage has already resolved a ref, or cache refs from getPage output.
Example fix
// before
const idx = await pdfDocument.getPageIndex(dest[0]);
// after
const ref = dest?.[0];
if (ref && Number.isInteger(ref.num) && ref.num >= 0 && Number.isInteger(ref.gen) && ref.gen >= 0) {
const idx = await pdfDocument.getPageIndex(ref);
} else {
// handle string/null destination via getDestination
} Defensive patterns
Strategy: type-guard
Validate before calling
function isRefLike(v) {
return v != null &&
typeof v === 'object' &&
Number.isInteger(v.num) && v.num >= 0 &&
Number.isInteger(v.gen) && v.gen >= 0;
}
// before calling:
if (!isRefLike(ref)) return null; Type guard
const isRefLike = (v): v is { num: number; gen: number } =>
!!v && typeof v === 'object' &&
Number.isInteger((v as any).num) && (v as any).num >= 0 &&
Number.isInteger((v as any).gen) && (v as any).gen >= 0; Try / catch
try {
const idx = await pdf.getPageIndex(ref);
} catch (e) {
if (e.message === 'Invalid pageIndex request.') { /* skip bad dest */ }
else throw e;
} Prevention
- Only pass refs obtained from PDFPageProxy or resolved destinations.
- Validate ref shape at the boundary where destinations enter your code.
- Keep refs fully typed as { num: number; gen: number } in your codebase.
When it happens
Trigger: Calling pdfDocument.getPageIndex(ref) with undefined, null, a plain number, a string, or an object missing/overflowing num or gen. Common when chaining from getPage(dest[0]) where dest is an array that does not start with a ref, or when passing a deserialized ref that lost its fields.
Common situations: Resolving a named/remote GoTo destination whose target array begins with a page number instead of a ref; passing an outline destination straight through without validating; ref objects built from JSON without Number-casting their fields.
Related errors
- Invalid updateScale options: either `steps` or `scaleFactor`
- BinaryCMapReader.process: Invalid dataSize.
- Page count in top-level pages dictionary is not an integer.
- Invalid type in PageLabel dictionary.
- Invalid style in PageLabel dictionary.
AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13).
Data as JSON: /api/errors/4bd6f98cc5b498fe.
Report an issue: GitHub.