saveAsCopy
Saves the current state of a loaded document, including any modifications like added or removed annotations, into a new ArrayBuffer
.
Signature
saveAsCopy(doc: PdfDocumentObject): PdfTask<ArrayBuffer>;
Description
This method generates a complete PDF file as an ArrayBuffer
based on the document’s current state in the engine’s memory. It’s the primary way to get a saveable file after you have made programmatic changes to a PDF (e.g., after using createPageAnnotation
, removePageAnnotation
, or flattenPage
).
This operation is non-destructive; it creates a new, independent copy and does not alter the document handle you are currently working with.
Parameters
Name | Type | Description |
---|---|---|
doc | PdfDocumentObject | The handle of the document to save, which you received from an openDocument... method. |
Returns
PdfTask<ArrayBuffer>
A Task
that resolves with an ArrayBuffer
containing the complete binary data of the new PDF file. This buffer can then be used to download the file in a browser or write it to the filesystem in Node.js.
The Task
will be rejected if the document handle is invalid or the save operation fails.
See Concepts: Tasks for more on how to handle asynchronous operations.
Example (Browser Download)
// Assuming 'engine' and a modified 'document' object are available
async function downloadModifiedPdf() {
try {
const newPdfBuffer = await engine.saveAsCopy(document).toPromise();
// Create a Blob from the ArrayBuffer
const blob = new Blob([newPdfBuffer], { type: 'application/pdf' });
const url = URL.createObjectURL(blob);
// Create a temporary anchor element to trigger the download
const a = document.createElement('a');
a.href = url;
a.download = 'document-modified.pdf'; // The desired filename
document.body.appendChild(a);
a.click();
// Clean up by removing the element and revoking the URL
document.body.removeChild(a);
URL.revokeObjectURL(url);
console.log('Download initiated.');
} catch (error) {
console.error('Failed to save the document:', error);
}
}
See Also
Need Help?
Join our community for support, discussions, and to contribute to EmbedPDF's development.