openDocumentBuffer
Opens a PDF document from an in-memory buffer (ArrayBuffer
or Uint8Array
).
Signature
openDocumentBuffer(file: PdfFile, options?: PdfOpenDocumentBufferOptions): PdfTask<PdfDocumentObject>;
Description
This is the most direct way to load a PDF. It’s ideal for environments where you already have the file’s binary content, such as after reading a file from the local filesystem in Node.js or from a user file upload in the browser. The entire file is loaded into the engine’s memory for processing.
Parameters
Name | Type | Description |
---|---|---|
file | PdfFile | An object containing a unique id and the content of the PDF as an ArrayBuffer or Uint8Array . |
password | PdfOpenDocumentBufferOptions | (Optional) An object to configure loading, primarily for providing a password. |
PdfFile
Interface
export interface PdfFile {
id: string; // A unique identifier for the document
content: ArrayBuffer | Uint8Array;
}
PdfOpenDocumentBufferOptions
export interface PdfOpenDocumentBufferOptions {
password?: string;
}
Returns
PdfTask<PdfDocumentObject>
A Task
that resolves with a PdfDocumentObject
when the document is successfully opened. This object acts as a handle for all subsequent operations on the document.
If the operation fails (e.g., the data is corrupted or the password is incorrect), the Task
will be rejected with a PdfErrorReason
.
See Concepts: Tasks for more on how to handle asynchronous operations.
Example (Node.js)
import { readFile } from 'fs/promises';
// Assuming 'engine' is an initialized PdfiumEngine instance
try {
const pdfBuffer = await readFile('path/to/my-document.pdf');
const document = await engine.openDocumentBuffer(
{ id: 'local-file', content: pdfBuffer },
{ password: 'optional-password' }
).toPromise();
console.log(`Document loaded with ${document.pageCount} pages.`);
} catch (error) {
console.error('Failed to load document from buffer:', error);
}
See Also
Need Help?
Join our community for support, discussions, and to contribute to EmbedPDF's development.