FPDF_CloseDocument
FPDF_CloseDocument(document)
Description
Closes a PDF document and releases all resources associated with it. This function must be called when you’re done with a PDF document that was opened with FPDF_LoadMemDocument
, FPDF_LoadCustomDocument
, or any other document loading function.
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 |
---|---|---|
document | number | A document handle obtained from FPDF_LoadMemDocument or other document loading functions. |
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 { init, WrappedPdfiumModule } from '@embedpdf/pdfium';
// Singleton for PDFium instance
let pdfiumInstance: WrappedPdfiumModule | null = null;
async function getPdfiumInstance() {
if (pdfiumInstance) return pdfiumInstance;
const response = await fetch('https://cdn.jsdelivr.net/npm/@embedpdf/pdfium/dist/pdfium.wasm');
const wasmBinary = await response.arrayBuffer();
pdfiumInstance = await init({ wasmBinary });
// Initialize the PDFium extension library
pdfiumInstance.PDFiumExt_Init();
return pdfiumInstance;
}
async function openAndClosePdf(pdfData: Uint8Array) {
// Get PDFium instance
const pdfium = await getPdfiumInstance();
// Allocate memory for the PDF data
const filePtr = pdfium.pdfium.wasmExports.malloc(pdfData.length);
try {
// Copy the PDF data to the allocated memory
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();
throw new Error(`Failed to load PDF: ${error}`);
}
try {
// Do operations with the PDF...
const pageCount = pdfium.FPDF_GetPageCount(docPtr);
console.log(`PDF has ${pageCount} pages`);
// More PDF operations...
} finally {
// Always close the document when done
pdfium.FPDF_CloseDocument(docPtr);
console.log('PDF document closed successfully');
}
} finally {
// Always free the memory allocated for the PDF data
pdfium.pdfium.wasmExports.free(filePtr);
}
}
// Usage
fetch('sample.pdf')
.then(response => response.arrayBuffer())
.then(buffer => openAndClosePdf(new Uint8Array(buffer)))
.catch(error => {
console.error('Error:', error);
});
Best Practices
-
Always close documents: Call
FPDF_CloseDocument
for every document you open, even if operations on the document fail. This prevents memory leaks. -
Use try/finally blocks: Place your document operations inside a try block and the
FPDF_CloseDocument
call in a finally block to ensure the document is closed even if exceptions occur. -
Close in the correct order: If you have opened multiple resources (e.g., pages, text pages) from a document, close them in the reverse order before closing the document:
- Close text pages with
FPDFText_ClosePage
- Close pages with
FPDF_ClosePage
- Finally, close the document with
FPDF_CloseDocument
- Close text pages with
-
Don’t use the document after closing: After calling
FPDF_CloseDocument
, the document handle is no longer valid. Any attempts to use it will result in undefined behavior and likely crashes.
Related Functions
- FPDF_LoadMemDocument - Load a PDF document from memory