closeDocument

Closes a previously opened PDF document, freeing up all associated memory and resources within the WASM engine.

Signature

closeDocument(doc: PdfDocumentObject): PdfTask<boolean>;

Description

It’s crucial to call this method when you are finished with a document. Failing to do so will result in a memory leak, as the entire PDF and its associated data structures will remain allocated in the WebAssembly heap.

Once a document is closed, its PdfDocumentObject handle becomes invalid and should not be used for any further operations.

Parameters

NameTypeDescription
docPdfDocumentObjectThe handle of the document to close, which you received from openDocumentUrl or openDocumentFromBuffer.

Returns

PdfTask<boolean>

A Task that resolves with true if the document was closed successfully. It will be rejected if the document handle is invalid or was already closed.

See Concepts: Tasks for more on how to handle asynchronous operations.

Example

// Assuming 'engine' and an open 'document' object are available console.log('Finished working with the document.'); try { await engine.closeDocument(document).toPromise(); console.log('Document closed and resources freed successfully.'); } catch (error) { console.error('Failed to close the document:', error); } // Any subsequent calls using the 'document' handle will now fail. // For example: await engine.renderPage(document, ...).toPromise(); -> REJECTION

See Also

Last updated on August 14, 2025

Need Help?

Join our community for support, discussions, and to contribute to EmbedPDF's development.