Password Protect PDFs Using JavaScript

This guide explains how to create a password-protected PDF document. Use password-protected PDFs if your document contains sensitive information and if you want to set different permissions for users and owners.

The following conformance levels are supported:

  • PDF/A-1a

  • PDF/A-1b

  • PDF/A-2a

  • PDF/A-2b

  • PDF/A-2u

  • PDF/A-3a

  • PDF/A-3b

  • PDF/A-3u

  • PDF/A-4

  • PDF/A-4e

  • PDF/A-4f

To create a password-protected PDF from a document, follow these steps:

  1. Load the source document.

  2. Use the exportPDF method to convert the source document to a password-protected PDF. This method takes an object as its parameter, which configures the conversion. For conversion to a password-protected PDF, add a permissions key to the configuration object that you pass to the exportPDF method. The value of this key is an object with the following key-value pairs:

    • userPassword takes a string value that specifies the user password. Users might have limited access to the document depending on how you configure user permissions with the documentPermissions parameter.

    • ownerPassword takes a string value that specifies the owner password. Owners have full access to the document.

    • documentPermissions takes an array that specifies what users can do with the document. For more information on setting user permissions, see Setting User Permissions.

  3. Save the output document. The exportPDF method returns a Promise that resolves to an ArrayBuffer that contains the output PDF document. You can use the resulting ArrayBuffer to download or persist the output PDF in storage. For more information on downloading or persisting the exported ArrayBuffer, see the guides on saving a document.

The example below loads a PDF document and exports it to a PDF that can only be opened with the user password, and users can only view the document:

  container: "#pspdfkit",
  document: "source.pdf",
  licenseKey: "YOUR_LICENSE_KEY"
}).then((instance) => {
    permissions: {
      userPassword: "u$erp@ssw0rd",
      ownerPassword: "ownerp@ssw0rd",
      documentPermissions: []

The example below loads a PDF document, exports it to a PDF protected by both a user and owner password, and sets the user permissions. Finally, it downloads the output PDF in the client’s browser:

  container: "#pspdfkit",
  document: "source.pdf",
  licenseKey: "YOUR_LICENSE_KEY"
  .then((instance) =>
      permissions: {
        userPassword: "userp@ssw0rd",
        ownerPassword: "ownerp@ssw0rd",
        documentPermissions: [PSPDFKit.DocumentPermissions.annotationsAndForms, PSPDFKit.DocumentPermissions.assemble, PSPDFKit.DocumentPermissions.extract, PSPDFKit.DocumentPermissions.extractAccessibility, PSPDFKit.DocumentPermissions.fillForms, PSPDFKit.DocumentPermissions.modification, PSPDFKit.DocumentPermissions.printHighQuality, PSPDFKit.DocumentPermissions.printing]
  .then(function (buffer) {
    const blob = new Blob([buffer], { type: "application/pdf" });
    const objectUrl = window.URL.createObjectURL(blob);
function downloadPdf(blob) {
  const a = document.createElement("a");
  a.href = blob; = "none"; = "output.pdf";
  a.setAttribute("download", "output.pdf");

When exporting a document, you have several options. Refer to our guides on flattening annotations and incremental saving for more details.

Auto saving can be configured for different scenarios and use cases. You can find more information in our auto save guide.

Setting User Permissions

Specify what users can do to a password-protected document with the documentPermissions parameter. The value of this parameter is an array, where each element is a member of the DocumentPermissions enumeration. By adding members of the DocumentPermissions enumeration to the array, you can set the following permissions:

  • annotationsAndForms allows users to add or modify text annotations and fill in interactive form fields.

  • assemble allows users to insert, rotate, or delete pages and create document outline items or thumbnail images.

  • extract allows users to copy or otherwise extract text and graphics from the document.

  • extractAccessibility allows users to copy or otherwise extract text and graphics from the document using accessibility options.

  • fillForms allows users to fill in existing interactive form fields (including signature fields).

  • modification allows users to modify the document in any way not covered by the other permissions settings.

  • printHighQuality allows users to print the document in high quality.

  • printing allows users to print the document.