Skip to content

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub

Encrypt and Protect PDF ​

This sample shows how to set permission passwords for opening documents and decrypt them.

javascript
// Import the JS file of ComPDFKit Web Demo.
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();

    // Sample1: Encrypt
    try {
      await doc.setPassword({ userPassword: 'password' });
      console.log('File is encrypted.');
    } catch (e) {
      console.log('File encrypt failed');
    }

    // Sample2: Unlock
    try {
      await doc.setPassword({});
      console.log('File is unlocked.');
    } catch (e) {
      console.log('File unlock failed');
    }
  })
})