---
title: "Open password-protected PDFs in JavaScript | Nutrient SDK"
canonical_url: "https://www.nutrient.io/guides/web/features/password-protected-pdfs/"
md_url: "https://www.nutrient.io/guides/web/features/password-protected-pdfs.md"
last_updated: "2026-05-26T09:21:30.500Z"
description: "Open and edit password-protected PDFs in JavaScript using Nutrient Web SDK. Learn to set passwords, manage JWT authentication, and enhance document security."
---

# Open password-protected PDFs in JavaScript

Nutrient Web SDK supports opening and editing password-protected PDFs. Passwords can be supplied when loading a PDF, when entered by a user when accessing a document, or when included in the JSON Web Token (JWT).

[Try for Free](https://www.nutrient.io/sdk/web/getting-started.md)

[Launch Demo](https://www.nutrient.io/demo/password-protected-pdf/)

## Setting the default document password

To supply the password when loading a PDF, set the password via the initial `Configuration#password` option ([try it in the Playground](https://www.nutrient.io/demo/sandbox?p=eyJ2IjoxLCJjc3MiOiIvKiBBZGQgeW91ciBDU1MgaGVyZSAqL1xuIiwianMiOiJjb25zb2xlLmxvZyhcIkRlbW9uc3RyYXRpbmcgcGFzc3dvcmQtcHJvdGVjdGVkIFBERiBoYW5kbGluZy4uLlwiKTtcblxuY29uc29sZS5sb2coXCJcXG5QYXNzd29yZCBwcm90ZWN0aW9uIHNjZW5hcmlvczpcIik7XG5jb25zb2xlLmxvZyhcIjEuIFN1cHBseSBwYXNzd29yZCBkdXJpbmcgbG9hZDogcGFzc3dvcmQ6ICdzZWNyM3QnXCIpO1xuY29uc29sZS5sb2coXCIyLiBVc2VyIGVudGVycyBwYXNzd29yZCB2aWEgcHJvbXB0IChpZiBub3Qgc3VwcGxpZWQpXCIpO1xuY29uc29sZS5sb2coXCIzLiBQYXNzd29yZCBpbmNsdWRlZCBpbiBKV1QgKHNlcnZlci1zaWRlKVwiKTtcblxuY29uc29sZS5sb2coXCJcXG5UaGlzIGRlbW8gd2lsbCBjcmVhdGUgYSBwYXNzd29yZC1wcm90ZWN0ZWQgUERGIGZvciB5b3UgdG8gdGVzdCFcIik7XG5cbk51dHJpZW50Vmlld2VyLmxvYWQ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IubG9hZCh7XCIpO1xuICBjb25zb2xlLmxvZyhcIiAgZG9jdW1lbnQ6ICdwYXNzd29yZC1wcm90ZWN0ZWQucGRmJyxcIik7XG4gIGNvbnNvbGUubG9nKFwiICBwYXNzd29yZDogJ3NlY3IzdCdcIik7XG4gIGNvbnNvbGUubG9nKFwifSk7XCIpO1xufSk7XG4ifQ%253D%253D)):

```js

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

  document: "/path/to/protected.pdf",
  password: "secr3t"
}).then((instance) => {
    console.log("Password-protected document loaded");
  }).catch((error) => {
    if (error.message.includes("password")) {
      console.error("Invalid password provided");
    } else {
      console.error("Failed to load document:", error.message);
    }
  });

```

Note that setting this property will make loading PDF documents that aren't protected by a password fail. As such, be sure to only add it for documents protected by a password.

## Including the password in the JWT (Document Engine only)

To include the password in the JWT used by Document Engine to authenticate clients, add the `password` claim to your JWT:

```js

jwt.sign(
  {
    document_id: document_id,
    password: "secr3t"
    //...
  },
  fs.readFileSync("./jwt.pem"),
  {
    algorithm: "RS256",
    expiresIn: 10 * 365 * 24 * 60 * 60 // 10 yrs
  }
);

```

## User enters the password

If the password is neither included in the JWT or set via the `Configuration#password` option, the user will be asked to enter the password via a password prompt.

## Complete example with password handling

Below is a complete example showing how to load a password-protected PDF with proper error handling:

```js

async function loadPasswordProtectedPDF(documentUrl, password) {
  try {
    const instance = await NutrientViewer.load({
      container: "#pspdfkit",

      document: documentUrl,
      password: password
    });
    console.log("Password-protected document loaded successfully");
    return instance;
  } catch (error) {
    if (error.message.includes("password")) {
      // Password was incorrect or document requires a password
      console.error("Password error:", error.message);
      // You could prompt the user for the correct password here
    } else {
      console.error("Failed to load document:", error.message);
    }
    throw error;
  }
}

// Usage
loadPasswordProtectedPDF("/documents/protected.pdf", "user-password").then((instance) => {
    // Work with the loaded document
  }).catch((error) => {
    // Handle the error (show message to user, retry, etc.)
  });

```
---

## Related pages

- [Create and open encrypted PDFs using JavaScript](/guides/web/standalone/accessing-protected-files.md)
- [Restrict access to PDFs with HTTP basic authentication](/guides/web/open-a-document/secured-documents/http-authenticated.md)

