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

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(opens in a new tab)):

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:

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.

Password Prompt

Complete example with password handling

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

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.)
});