{"record":{"id":"69c14f7e6c96d1d1","repo":"Stirling-Tools/Stirling-PDF","slug":"pdfium-failed-to-load-page-pageindex","errorCode":null,"errorMessage":"PDFium: failed to load page ${pageIndex}","messagePattern":"PDFium: failed to load page (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"frontend/editor/src/core/services/pdfiumService.ts","lineNumber":370,"sourceCode":"\n/**\n * Get page count for a raw document pointer.\n */\nexport async function getRawPageCount(docPtr: number): Promise<number> {\n  const m = await getPdfiumModule();\n  return m.FPDF_GetPageCount(docPtr);\n}\n\n/**\n * Get raw page dimensions { width, height } for a page.\n */\nexport async function getRawPageSize(\n  docPtr: number,\n  pageIndex: number,\n): Promise<{ width: number; height: number }> {\n  const m = await getPdfiumModule();\n  const pagePtr = m.FPDF_LoadPage(docPtr, pageIndex);\n  if (!pagePtr) throw new Error(`PDFium: failed to load page ${pageIndex}`);\n  const width = m.FPDF_GetPageWidthF(pagePtr);\n  const height = m.FPDF_GetPageHeightF(pagePtr);\n  m.FPDF_ClosePage(pagePtr);\n  return { width, height };\n}\n\n/**\n * Read a UTF-16LE string from PDFium memory at the given pointer up to `len`\n * bytes (including the trailing NUL pair).\n */\nexport function readUtf16(\n  m: WrappedPdfiumModule,\n  ptr: number,\n  byteLen: number,\n): string {\n  if (byteLen <= 2 || !ptr) return \"\";\n  return m.pdfium.UTF16ToString(ptr);\n}","sourceCodeStart":352,"sourceCodeEnd":388,"githubUrl":"https://github.com/Stirling-Tools/Stirling-PDF/blob/9ef20dcab80b85041912f045e17a6aea1d08f969/frontend/editor/src/core/services/pdfiumService.ts#L352-L388","documentation":"`getRawPageSize` calls `FPDF_LoadPage(docPtr, pageIndex)`; a null return means the page handle couldn't be created. The most common cause is `pageIndex` being out of range (`< 0` or `>= FPDF_GetPageCount(docPtr)`), but it also fires when the page object in the PDF is structurally damaged.","triggerScenarios":"Requesting page index N when the document has fewer pages; passing a negative index; the page dictionary references missing/invalid objects so PDFium can't instantiate it; calling after the document pointer was closed.","commonSituations":"Stale page count cached before a document was re-saved; iterating with an off-by-one (`<= count` instead of `< count`); a corrupt page in an otherwise-openable PDF.","solutions":["Validate `0 <= pageIndex < FPDF_GetPageCount(docPtr)` before calling `FPDF_LoadPage`.","Re-fetch the page count immediately before the loop rather than trusting a cached value.","On failure, read `FPDF_GetLastError()` (code 6 = page error) to distinguish range vs corruption.","Guard against a closed/null `docPtr`."],"exampleFix":"// before\nconst pagePtr = m.FPDF_LoadPage(docPtr, pageIndex);\nif (!pagePtr) throw new Error(`PDFium: failed to load page ${pageIndex}`);\n\n// after\nconst total = m.FPDF_GetPageCount(docPtr);\nif (pageIndex < 0 || pageIndex >= total) {\n  throw new RangeError(`Page index ${pageIndex} out of range (0..${total - 1})`);\n}\nconst pagePtr = m.FPDF_LoadPage(docPtr, pageIndex);\nif (!pagePtr) {\n  const err = m.FPDF_GetLastError();\n  throw new Error(`PDFium: failed to load page ${pageIndex} (error ${err})`);\n}","handlingStrategy":"validation","validationCode":"const count = m.FPDF_GetPageCount(docPtr);\nif (pageIndex < 0 || pageIndex >= count) {\n  throw new RangeError(`Page ${pageIndex} out of range (0..${count - 1})`);\n}","typeGuard":"function isInRange(idx: number, count: number): boolean {\n  return Number.isInteger(idx) && idx >= 0 && idx < count;\n}","tryCatchPattern":null,"preventionTips":["Always re-read FPDF_GetPageCount immediately before indexing pages.","Validate 0 <= pageIndex < count before FPDF_LoadPage.","Use < count, not <= count, in loops (off-by-one is the top cause).","Capture FPDF_GetLastError() (code 6 = page error) to distinguish range vs corruption."],"tags":["pdfium","page","validation","range"],"backgroundTag":null,"analyzedSha":"9ef20dcab80b85041912f045e17a6aea1d08f969","analyzedAt":"2026-08-13T22:11:39.827Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}