PDF 生成模板编辑器
开源可视化 PDF 生成引擎,支持自定义模板,并提供面向开发者的 API,灵活构建文档生成流程。
ComPDF 支持获取文档数据保存到本地文件或保存到服务器。
以下是如何保存文档的示例代码:
// 导入 ComPDFKit Web Demo 的JS文件。
import ComPDFKitViewer from "@compdfkit_pdf_sdk/webviewer";
const viewer = document.getElementById('webviewer');
ComPDFKitViewer({
path: '/',
initialDoc: 'Your PDF Url',
licenseKey: 'Input your license here'
}, viewer)
.then((instance) => {
const { documentViewer, SaveOptions } = instance.Core;
documentViewer.addEventListener('documentLoaded', async () => {
console.log('ComPDFKit Web Demo');
const docStream = await documentViewer.getDocument().getFileData();
const docBlob = new Blob([docStream], { type: 'application/pdf' });
})
});导出 PDF 数据文档
const bufferData = await documentViewer.getDocument().getFileData({ flags: SaveOptions.INCREMENTAL });
const blobData = new Blob([bufferData], { type: 'application/pdf' });| 名称 | 必填 | 类型 | 描述 | 默认值 |
|---|---|---|---|---|
| flags | 否 | SaveOptions | 保存标志。SaveOptions.INCREMENTAL 在原文件后增量追加修改;不传则另存为新文件 | — |
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);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,
});