mozilla/pdf.js · error · Error
Page index ${pageIndex} not found.
Error message
Page index ${pageIndex} not found. What it means
Thrown at the end of Catalog.getPageDict() (catalog.js:1445) when the entire /Pages tree was traversed (nodesToVisit exhausted) without locating the requested page index. This is a generic Error (not FormatError), indicating the requested page index is out of range or the page tree is inconsistent (e.g., /Count overstates the real number of leaf pages).
Source
Thrown at src/core/catalog.js:1445
// node further down in the tree (see issue5644.pdf, issue8088.pdf),
// and to ensure that we actually find the correct `Page` dict.
for (let last = kids.length - 1; last >= 0; last--) {
const lastKid = kids[last];
nodesToVisit.push(lastKid);
// Launch all requests in parallel so we don't wait for each one in turn
// when looking for a page near the end, if all the pages are top level.
if (
currentNode === this.toplevelPagesDict &&
lastKid instanceof Ref &&
!pageDictCache.has(lastKid)
) {
pageDictCache.put(lastKid, xref.fetchAsync(lastKid));
}
}
}
throw new Error(`Page index ${pageIndex} not found.`);
}
/**
* Eagerly fetches the entire /Pages-tree; should ONLY be used as a fallback.
* @returns {Promise<Map>}
*/
async getAllPageDicts(recoveryMode = false) {
const { ignoreErrors } = this.pdfManager.evaluatorOptions;
const queue = [{ currentNode: this.toplevelPagesDict, posInKids: 0 }];
const visitedNodes = new RefSet();
const pagesRef = this.#catDict.getRaw("Pages");
if (pagesRef instanceof Ref) {
visitedNodes.put(pagesRef);
}
const map = new Map(),
xref = this.xref,View on GitHub (pinned to 5903d58d58)
Solutions
- Clamp the requested index: always call getPage(i) with 1 <= i <= pdfDocument.numPages and verify numPages first.
- If numPages is suspect (corrupt /Count), repair with qpdf/mutool to rebuild an accurate count.
- Catch this Error around getPage() and surface a clear 'page not found' to the user instead of crashing.
- For robustness, fall back to getAllPageDicts() recovery traversal if a normal getPage fails.
Example fix
// before: trusting an externally supplied index
const page = await pdfDoc.getPage(maybeBadIndex);
// after: bound-check against the real page count and handle missing page
const last = pdfDoc.numPages;
const idx = Math.min(Math.max(1, maybeBadIndex), last);
try {
return await pdfDoc.getPage(idx);
} catch (e) {
// page index genuinely unreachable in this document
throw new Error(`page ${idx} of ${last} not reachable`, { cause: e });
} Defensive patterns
Strategy: validation
Validate before calling
// Bound-check the requested page index against the real page count.
const total = pdf.numPages;
const safeIndex = Math.min(Math.max(1, requestedIndex), total);
if (safeIndex !== requestedIndex) {
throw new RangeError(`page ${requestedIndex} out of range (1..${total})`);
}
const page = await pdf.getPage(safeIndex); Try / catch
try {
return await pdf.getPage(idx);
} catch (err) {
if (/Page index \d+ not found/i.test(err?.message)) {
throw new RangeError(`page ${idx} not reachable in this document`, { cause: err });
}
throw err;
} Prevention
- Always clamp getPage arguments to [1, pdf.numPages] and check numPages first.
- Repair PDFs whose /Count overstates the real leaf count (qpdf/mutool).
- Watch for off-by-one: page numbers are 1-based in getPage, 0-based internally.
- Fall back to getAllPageDicts() recovery if a normal getPage fails.
When it happens
Trigger: pdfDocument.getPage(pageIndex) where pageIndex >= the actual number of reachable leaf pages, or where /Count claims more pages than the tree actually contains so the traversal runs out of nodes before reaching the index.
Common situations: Calling getPage with an index derived from a stale/overstated numPages (corrupt /Count); off-by-one when indexing (requesting page numPages instead of numPages-1); a truncated page tree; a PDF where /Count lies.
Related errors
- Pages tree contains circular reference.
- Page dictionary kid reference points to wrong type of object
- Page dictionary kids object is not an array.
- The reference does not point to a /Page dictionary.
- Node must be a dictionary.
AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13).
Data as JSON: /api/errors/7908ecaad4e6217c.
Report an issue: GitHub.