Save a Document
ComPDF supports getting document data to save to the local file system or save to the server.
This example shows how to save a document:
javascript
// Import the JS file of ComPDFKit Web Demo.
import ComPDFKitViewer from "@compdfkit_pdf_sdk/webviewer";
const viewer = document.getElementById('webviewer');
ComPDFKitViewer.init({
pdf: '/',
pdfUrl: 'Your PDF Url',
license: 'Input your license here'
}, viewer)
.then((core) => {
const docViewer = core.docViewer;
docViewer.addEvent('documentloaded',async () => {
console.log('Document Loaded');
})
});Export the PDF Data
javascript
const bufferData = await docViewer.exportPDF(saveAs);
const blobData = new Blob([bufferData], { type: 'application/pdf' });| Name | Required | Type | Description | Default Value |
|---|---|---|---|---|
| saveAs | no | boolean | Whether to save as a new file | false |
Save to the local file system
javascript
const fileName = "document.pdf";
const url = URL.createObjectURL(blobData);
const a = document.createElement("a");
a.href = url;
a.style = "display: none";
a.download = fileName;
document.body.appendChild(a);
a.click();
URL.revokeObjectURL(url);
document.body.removeChild(a);Save to the server
javascript
const fileName = 'document.pdf';
const data = new FormData();
data.append('file', blobData, fileName);
const res = await fetch('https://example.com/api/test', {
method: 'POST',
body: data,
});