Skip to content
Guides

Save a Document

ComPDFKit 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/webviewer";

const viewer = document.getElementById('webviewer');
ComPDFKitViewer.init({
  pdfUrl: 'Your PDF Url',
  license: 'Input your license here'
}, viewer)
.then((core) => {
  const docViewer = core.docViewer;
  docViewer.addEvent('documentloaded',async () => {
    console.log('ComPDFKit Web Demo');

    const docStream = await docViewer.download()
    const docBlob = new Blob([docStream], { type: 'application/pdf' })
  })
})

Save to the local file system

javascript
const fileName = "document.pdf";

const url = URL.createObjectURL(docBlob);
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', docBlob, fileName);

const res = await fetch('https://example.com/api/test', {
  method: 'POST',
  body: data,
});