FPDF_ClosePage
FPDF_ClosePage(page)
Description
Closes a PDF page and releases all resources associated with it. This function must be called when you’re done with a page that was opened with FPDF_LoadPage
.
Failing to call this function will result in memory leaks in your application.
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
Name | Type | Description |
---|---|---|
page | number | A page handle obtained from FPDF_LoadPage . |
Return Value
This function does not return a value.
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();
// Allocate memory for the PDF data
const filePtr = pdfium.pdfium.wasmExports.malloc(pdfData.length);
pdfium.pdfium.HEAPU8.set(pdfData, filePtr);
// Load the PDF document
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 page count
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))
.catch(error => console.error('Error:', error));
Best Practices
-
Always close pages: Call
FPDF_ClosePage
for every page you open, even if operations on the page fail. This prevents memory leaks. -
Use try/finally blocks: Place your page operations inside a try block and the
FPDF_ClosePage
call in a finally block to ensure the page is closed even if exceptions occur:const pagePtr = pdfium.FPDF_LoadPage(docPtr, pageIndex); try { // Operate on the page... } finally { pdfium.FPDF_ClosePage(pagePtr); }
-
Close in the correct order: If you have opened multiple resources from a page (e.g., text pages), close them in the reverse order before closing the page:
- Close text pages with
FPDFText_ClosePage
- Finally, close the page with
FPDF_ClosePage
- Close text pages with
-
Don’t use the page after closing: After calling
FPDF_ClosePage
, the page handle is no longer valid. Any attempts to use it will result in undefined behavior and likely crashes. -
Batch processing: When processing multiple pages, open and close them sequentially rather than keeping many pages open simultaneously, which can consume a lot of memory.
Common Issues
-
Memory leaks: The most common issue is forgetting to close pages, especially in error cases. This leads to memory leaks that can eventually crash your application.
-
Invalid page handles: Using a page handle after it has been closed can cause crashes or other unexpected behavior.
-
Rendering artifacts: If you’re rendering pages to a bitmap, make sure to close the page after rendering is complete. Not closing the page can sometimes lead to rendering artifacts when rendering subsequent pages.
Related Functions
- FPDF_LoadPage - Load a page from a document
- FPDF_LoadMemDocument - Load a PDF document from memory
- FPDF_CloseDocument - Close a document and release resources
- FPDFText_LoadPage - Load a page for text extraction