---
title: "JavaScript password-protected PDF: Add password to PDF | Nutrient"
canonical_url: "https://www.nutrient.io/guides/web/document-security/password-protect-pdf/"
md_url: "https://www.nutrient.io/guides/web/document-security/password-protect-pdf.md"
last_updated: "2026-05-25T18:42:17.819Z"
description: "Discover how to create a password-protected PDF document. Use password-protected PDFs if your document contains sensitive information."
---

# 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.

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

1. [Load the source document](https://www.nutrient.io/guides/web/open-a-document.md).

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](#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](https://www.nutrient.io/guides/web/save-a-document.md).

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. [Try it in the Playground](https://www.nutrient.io/demo/sandbox?p=ewogICJ2IjogMSwKICAic2V0dGluZ3MiOiB7CiAgICAiZmlsZU5hbWUiOiAiYmFzaWMucGRmIgogIH0sCiAgImpzIjogIk51dHJpZW50Vmlld2VyLmxvYWQoYmFzZU9wdGlvbnMpLnRoZW4oKGluc3RhbmNlKSA9PiB7XG4gIGluc3RhbmNlLmV4cG9ydFBERih7XG4gICAgcGVybWlzc2lvbnM6IHtcbiAgICAgIHVzZXJQYXNzd29yZDogXCJ1JGVycEBzc3cwcmRcIixcbiAgICAgIG93bmVyUGFzc3dvcmQ6IFwib3duZXJwQHNzdzByZFwiLFxuICAgICAgZG9jdW1lbnRQZXJtaXNzaW9uczogW11cbiAgICB9XG4gIH0pLnRoZW4oKGJ1ZmZlcikgPT4ge1xuICAgIGNvbnNvbGUubG9nKFwiUERGIGV4cG9ydGVkIHdpdGggcGFzc3dvcmQgcHJvdGVjdGlvbiFcIik7XG4gICAgY29uc29sZS5sb2coXCJCdWZmZXIgc2l6ZTpcIiwgYnVmZmVyLmJ5dGVMZW5ndGgsIFwiYnl0ZXNcIik7XG4gIH0pO1xufSk7IiwKICAiY3NzIjogIiIKfQo%3D).

```js

NutrientViewer.load({
  container: "#pspdfkit",

  document: "source.pdf",
  licenseKey: "YOUR_LICENSE_KEY"
}).then(async (instance) => {
    console.log("Document loaded");
    const buffer = await instance.exportPDF({
      permissions: {
        userPassword: "u$erp@ssw0rd",
        ownerPassword: "ownerp@ssw0rd",
        documentPermissions: []
      }
    });
    console.log("Password-protected PDF exported");
    return buffer;
  }).catch((error) => {
    console.error("Failed to process document:", error.message);
  });

```

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:

```js

NutrientViewer.load({
  container: "#pspdfkit",

  document: "source.pdf",
  licenseKey: "YOUR_LICENSE_KEY"
}).then((instance) =>
    instance.exportPDF({
      permissions: {
        userPassword: "userp@ssw0rd",
        ownerPassword: "ownerp@ssw0rd",
        documentPermissions: [
          NutrientViewer.DocumentPermissions.annotationsAndForms,
          NutrientViewer.DocumentPermissions.assemble,
          NutrientViewer.DocumentPermissions.extract,
          NutrientViewer.DocumentPermissions.extractAccessibility,
          NutrientViewer.DocumentPermissions.fillForms,
          NutrientViewer.DocumentPermissions.modification,
          NutrientViewer.DocumentPermissions.printHighQuality,
          NutrientViewer.DocumentPermissions.printing
        ]
      }
    })
  ).then((buffer) => {
    const blob = new Blob([buffer], { type: "application/pdf" });
    const objectUrl = window.URL.createObjectURL(blob);
    downloadPdf(objectUrl);
    window.URL.revokeObjectURL(objectUrl);
    console.log("Password-protected PDF downloaded");
  }).catch((error) => {
    console.error("Failed to process document:", error.message);
  });

function downloadPdf(blobUrl) {
  const a = document.createElement("a");
  a.href = blobUrl;
  a.style.display = "none";
  a.download = "output.pdf";
  a.setAttribute("download", "output.pdf");
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
}

```

When exporting a document, you have several options. Refer to our guides on [flattening annotations](https://www.nutrient.io/guides/web/annotations/flatten.md) and [incremental saving](https://www.nutrient.io/guides/web/features/document-processing.md) for more details.

Auto saving can be configured for different scenarios and use cases. You can find more information in our [auto save](https://www.nutrient.io/guides/web/features/saving.md) 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.
---

## Related pages

- [Document security in our JavaScript PDF viewer](/guides/web/document-security.md)
- [Add watermarks to PDFs using JavaScript](/guides/web/document-security/add-a-watermark.md)
- [Open PDFs without downloading using JavaScript](/guides/web/document-security/open-without-downloading.md)
- [Disable download and print options in our JavaScript PDF viewer](/guides/web/document-security/prevent-print-or-download.md)
- [Document security with Nutrient Web SDK](/guides/web/document-security/with-server-backed.md)

