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:
-
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 apermissions
key to the configuration object that you pass to theexportPDF
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 thedocumentPermissions
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.
-
-
Save the output document. The
exportPDF
method returns aPromise
that resolves to anArrayBuffer
that contains the output PDF document. You can use the resultingArrayBuffer
to download or persist the output PDF in storage. For more information on downloading or persisting the exportedArrayBuffer
, 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:
PSPDFKit.load({ container: "#pspdfkit", document: "source.pdf", licenseKey: "YOUR_LICENSE_KEY" }).then((instance) => { instance.exportPDF({ 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:
PSPDFKit.load({ container: "#pspdfkit", document: "source.pdf", licenseKey: "YOUR_LICENSE_KEY" }) .then((instance) => instance.exportPDF({ 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); downloadPdf(objectUrl); window.URL.revokeObjectURL(objectUrl); }); function downloadPdf(blob) { const a = document.createElement("a"); a.href = blob; 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 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.