Skip to content

PDF 生成模板编辑器

开源可视化 PDF 生成引擎,支持自定义模板,并提供面向开发者的 API,灵活构建文档生成流程。

查看 GitHub

加密与保护 PDF ​

本示例展示如何为打开文档设置密码以及解除加密。

javascript
// 导入 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 } = instance.Core;
  documentViewer.addEventListener('documentLoaded', async () => {
    console.log('ComPDFKit Web Demo loaded');

    const doc = documentViewer.getDocument();

    // 示例 1:加密
    try {
      await doc.setPassword({ userPassword: 'password' });
      console.log('File is encrypted.');
    } catch (e) {
      console.log('File encrypt failed');
    }

    // 示例 2:解除加密
    try {
      await doc.setPassword({});
      console.log('File is unlocked.');
    } catch (e) {
      console.log('File unlock failed');
    }
  })
})