Skip to content

保存文档

ComPDFKit 支持获取文档数据保存到本地文件或保存到服务器。

以下是如何保存文档的示例代码:

javascript
// 导入 ComPDFKit Web Demo 的JS文件。
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' });
    })
  });

保存到本地文件

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);

保存到服务器

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,
});