FPDF_LoadPage

FPDF_LoadPage(document, page_index)

Description

Loads a specific page from a PDF document. This function returns a page handle that can be used with other page-related functions for operations like rendering, text extraction, and more.

Prerequisites

This example uses the initializePdfium helper function from our Getting Started guide. Make sure to include this function in your code before trying the examples below.

Parameters

NameTypeDescription
documentnumberA document handle obtained from FPDF_LoadMemDocument or other document loading functions.
page_indexnumberZero-based index of the page to load.

Return Value

Returns a page handle (a number) on success, or 0 on failure. When a failure occurs, you can call FPDF_GetLastError() to retrieve an error code that provides more information about what went wrong.

Example

// Note: The initializePdfium function is a helper that initializes the PDFium library. // For the full implementation, see: /docs/pdfium/getting-started import { initializePdfium } from './initialize-pdfium'; async function renderPdfPage(pdfData: Uint8Array, pageIndex: number) { // Initialize PDFium const pdfium = await initializePdfium(); // Load the PDF document const filePtr = pdfium.pdfium.wasmExports.malloc(pdfData.length); pdfium.pdfium.HEAPU8.set(pdfData, filePtr); const docPtr = pdfium.FPDF_LoadMemDocument(filePtr, pdfData.length, 0); if (!docPtr) { const error = pdfium.FPDF_GetLastError(); pdfium.pdfium.wasmExports.free(filePtr); throw new Error(`Failed to load PDF: ${error}`); } try { // Check if the page index is valid const pageCount = pdfium.FPDF_GetPageCount(docPtr); if (pageIndex < 0 || pageIndex >= pageCount) { throw new Error(`Invalid page index: ${pageIndex}. Document has ${pageCount} pages.`); } // Load the page const pagePtr = pdfium.FPDF_LoadPage(docPtr, pageIndex); if (!pagePtr) { throw new Error(`Failed to load page ${pageIndex}`); } try { // Perform operations with the page... const width = pdfium.FPDF_GetPageWidthF(pagePtr); const height = pdfium.FPDF_GetPageHeightF(pagePtr); console.log(`Page ${pageIndex} dimensions: ${width} x ${height} points`); // More page operations... } finally { // Always close the page when done pdfium.FPDF_ClosePage(pagePtr); } } finally { // Clean up document resources pdfium.FPDF_CloseDocument(docPtr); pdfium.pdfium.wasmExports.free(filePtr); } } // Usage fetch('sample.pdf') .then(response => response.arrayBuffer()) .then(buffer => renderPdfPage(new Uint8Array(buffer), 0)) // Render the first page .catch(error => console.error('Error:', error));

Best Practices

  1. Always check page index bounds: Before calling FPDF_LoadPage, verify that the requested page index is within the range of available pages in the document (use FPDF_GetPageCount to determine this).

  2. Always close pages: When you’re done with a page, make sure to call FPDF_ClosePage to release the resources associated with it. Ideally, use a try/finally block to ensure pages are closed even if errors occur.

  3. Load pages as needed: For large documents, avoid loading all pages at once. Instead, load pages as needed to conserve memory.

  4. Parallel operations: Be careful when loading multiple pages for parallel processing. While PDFium allows multiple pages to be open simultaneously, this can consume significant memory. Consider batching page operations for better performance and memory usage.

  5. Check for errors: If FPDF_LoadPage returns 0 (indicating failure), use FPDF_GetLastError() to get more information about what went wrong.

Common Issues

  1. Invalid page index: The most common error is attempting to load a page with an index that is out of bounds. Always check that the requested page index is within the range 0 to FPDF_GetPageCount(document) - 1.

  2. Memory exhaustion: Loading many pages simultaneously can exhaust available memory, especially for large PDFs with complex content. Always release page resources with FPDF_ClosePage when you’re done with them.

  3. Corrupted document: If a PDF document is corrupted, FPDF_LoadPage may fail for certain pages even if the document itself was loaded successfully.

Last updated on March 31, 2025