{"record":{"id":"f6930abdbf57aa06","repo":"Stirling-Tools/Stirling-PDF","slug":"pdfium-failed-to-create-page-f6930a","errorCode":null,"errorMessage":"PDFium: failed to create page","messagePattern":"PDFium: failed to create page","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"frontend/editor/src/core/utils/imageToPdfUtils.ts","lineNumber":105,"sourceCode":"        drawHeight = pageWidth / imageAspectRatio;\n        drawX = 0;\n        drawY = (pageHeight - drawHeight) / 2;\n      } else {\n        drawHeight = pageHeight;\n        drawWidth = pageHeight * imageAspectRatio;\n        drawY = 0;\n        drawX = (pageWidth - drawWidth) / 2;\n      }\n    }\n\n    // Create new PDF document\n    const docPtr = m.FPDF_CreateNewDocument();\n    if (!docPtr) throw new Error(\"PDFium: failed to create document\");\n\n    try {\n      // Create a page\n      const pagePtr = m.FPDFPage_New(docPtr, 0, pageWidth, pageHeight);\n      if (!pagePtr) throw new Error(\"PDFium: failed to create page\");\n\n      // Create bitmap from RGBA data (PDFium uses BGRA)\n      const bitmapPtr = m.FPDFBitmap_Create(imageWidth, imageHeight, 1);\n      if (!bitmapPtr) throw new Error(\"PDFium: failed to create bitmap\");\n\n      const bufferPtr = m.FPDFBitmap_GetBuffer(bitmapPtr);\n      const stride = m.FPDFBitmap_GetStride(bitmapPtr);\n\n      // Bulk RGBA → BGRA copy via shared utility\n      copyRgbaToBgraHeap(m, rgba, bufferPtr, imageWidth, imageHeight, stride);\n\n      // Create image page object\n      const imageObjPtr = m.FPDFPageObj_NewImageObj(docPtr);\n      if (!imageObjPtr) {\n        m.FPDFBitmap_Destroy(bitmapPtr);\n        throw new Error(\"PDFium: failed to create image object\");\n      }\n","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/Stirling-Tools/Stirling-PDF/blob/9ef20dcab80b85041912f045e17a6aea1d08f969/frontend/editor/src/core/utils/imageToPdfUtils.ts#L87-L123","documentation":"Thrown when FPDFPage_New(docPtr, 0, pageWidth, pageHeight) returns null after the document was successfully created. The page (with the computed width/height in PDF points) could not be allocated inside the document. Failure at this stage typically indicates resource exhaustion or invalid page dimensions.","triggerScenarios":"The document pointer is valid but FPDFPage_New returns 0. Possible causes: WASM memory exhausted after document creation; pageWidth/pageHeight are non-finite, negative, or absurdly large values that PDFium rejects. Reached only after FPDF_CreateNewDocument succeeded.","commonSituations":"Very large image producing extreme page dimensions (pageFormat 'keep' on a huge image). NaN/Infinity in pageWidth/pageHeight from a division by zero (e.g. zero-aspect or corrupt image metrics). Memory pressure after allocating the document.","solutions":["Validate imageWidth/imageHeight are finite positive numbers before computing page dimensions.","Use a fixed page format (A4/letter) instead of 'keep' for very large images to bound page size.","Reduce image resolution to lower overall memory pressure.","Catch and surface a user-friendly 'could not build page' error with the image dimensions logged."],"exampleFix":"// before\n// pageFormat 'keep' on a 50000x50000 image -> huge page -> null\nconvertImageToPdf(file, { pageFormat: 'keep' })\n// after\nconvertImageToPdf(file, { pageFormat: 'A4', imageResolution: 'reduced' })","handlingStrategy":"validation","validationCode":"function areValidPageDimensions(w: number, h: number): boolean {\n  return Number.isFinite(w) && Number.isFinite(h) && w > 0 && h > 0 && w < 14400 && h < 14400;\n}\n// Validate imageWidth/imageHeight from decodeImageToRgba before computing page size.","typeGuard":"function areFiniteDimensions(w: unknown, h: unknown): w is number {\n  return typeof w === 'number' && typeof h === 'number' &&\n    Number.isFinite(w) && Number.isFinite(h) && w > 0 && h > 0;\n}","tryCatchPattern":"try {\n  await convertImageToPdf(file, { pageFormat: 'A4' });\n} catch (e) {\n  const reason = (e as Error & { cause?: Error }).cause?.message ?? e.message;\n  if (reason.includes('failed to create page')) {\n    showUser('Could not create a PDF page for this image. Try a smaller resolution or A4 format.');\n  } else { throw e; }\n}","preventionTips":["Avoid pageFormat: 'keep' for very large images (produces huge pages).","Validate decoded image dimensions are finite and positive.","Default to a fixed page size (A4/Letter) for oversized images.","Reduce resolution to bound memory usage."],"tags":["image-to-pdf","pdfium","wasm","memory","page"],"backgroundTag":null,"analyzedSha":"9ef20dcab80b85041912f045e17a6aea1d08f969","analyzedAt":"2026-08-13T22:11:39.827Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}