{"record":{"id":"7908ecaad4e6217c","repo":"mozilla/pdf.js","slug":"page-index-pageindex-not-found","errorCode":null,"errorMessage":"Page index ${pageIndex} not found.","messagePattern":"Page index (.+?) not found\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/core/catalog.js","lineNumber":1445,"sourceCode":"      // node further down in the tree (see issue5644.pdf, issue8088.pdf),\n      // and to ensure that we actually find the correct `Page` dict.\n      for (let last = kids.length - 1; last >= 0; last--) {\n        const lastKid = kids[last];\n        nodesToVisit.push(lastKid);\n\n        // Launch all requests in parallel so we don't wait for each one in turn\n        // when looking for a page near the end, if all the pages are top level.\n        if (\n          currentNode === this.toplevelPagesDict &&\n          lastKid instanceof Ref &&\n          !pageDictCache.has(lastKid)\n        ) {\n          pageDictCache.put(lastKid, xref.fetchAsync(lastKid));\n        }\n      }\n    }\n\n    throw new Error(`Page index ${pageIndex} not found.`);\n  }\n\n  /**\n   * Eagerly fetches the entire /Pages-tree; should ONLY be used as a fallback.\n   * @returns {Promise<Map>}\n   */\n  async getAllPageDicts(recoveryMode = false) {\n    const { ignoreErrors } = this.pdfManager.evaluatorOptions;\n\n    const queue = [{ currentNode: this.toplevelPagesDict, posInKids: 0 }];\n    const visitedNodes = new RefSet();\n\n    const pagesRef = this.#catDict.getRaw(\"Pages\");\n    if (pagesRef instanceof Ref) {\n      visitedNodes.put(pagesRef);\n    }\n    const map = new Map(),\n      xref = this.xref,","sourceCodeStart":1427,"sourceCodeEnd":1463,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/core/catalog.js#L1427-L1463","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before: trusting an externally supplied index\nconst page = await pdfDoc.getPage(maybeBadIndex);\n\n// after: bound-check against the real page count and handle missing page\nconst last = pdfDoc.numPages;\nconst idx = Math.min(Math.max(1, maybeBadIndex), last);\ntry {\n  return await pdfDoc.getPage(idx);\n} catch (e) {\n  // page index genuinely unreachable in this document\n  throw new Error(`page ${idx} of ${last} not reachable`, { cause: e });\n}","handlingStrategy":"validation","validationCode":"// Bound-check the requested page index against the real page count.\nconst total = pdf.numPages;\nconst safeIndex = Math.min(Math.max(1, requestedIndex), total);\nif (safeIndex !== requestedIndex) {\n  throw new RangeError(`page ${requestedIndex} out of range (1..${total})`);\n}\nconst page = await pdf.getPage(safeIndex);","typeGuard":null,"tryCatchPattern":"try {\n  return await pdf.getPage(idx);\n} catch (err) {\n  if (/Page index \\d+ not found/i.test(err?.message)) {\n    throw new RangeError(`page ${idx} not reachable in this document`, { cause: err });\n  }\n  throw err;\n}","preventionTips":["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."],"tags":["pdf-structure","pages-tree","page-retrieval","out-of-range","corrupt-pdf"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}