Handling password-protected PDFs in Javascript
PDF is one of the simplest formats for sharing documents. They are portable and can provide basic access control through password protection. This post shows one of many ways to unlock and open password-protected PDF documents in JavaScript.
Specifically, this post uses PDF.js and client side JavaScript tools built into modern browsers to:
Read a PDF file from a user’s device.
Prompt for passwords only when the PDF is password-protected.
Display feedback for failed attempts to unlock a PDF file.
Render pages of the decrypted PDF using the browser’s Canvas API.
TL;DR: Handling password-protected PDFs in JavaScript isn’t as hard as it seems. You just need the right tools. This guide shows how to use PDF.js and other libraries to open and work with secured PDFs in the browser. It explains how to detect if a PDF has a password, ask the user for it, and unlock the file so you can read or display it. Whether you’re building a custom viewer or need to extract data, the key takeaway is that you can manage password-protected PDFs easily with a few smart code tricks and the right setup.
Start a New JavaScript Project
As a first step, let’s set up scaffolding for a vanilla JavaScript application using Vite. Run the below command in your terminal to create a new vanilla JavaScript web app named pdf-password and install its dependencies.
npm create vite@latest -- --template vanilla pdf-password && cd pdf-password && npm install
Next, install PDF.js as a project dependency:
npm install pdfjs-dist
Then, open the newly created pdf-password folder in your preferred code editor to begin building the PDF viewer.
Create HTML Elements to Handle User Input and PDF Rendering
Replace the contents of the project’s index.html file with the following.
The first form in the above markup handles file uploads from the user’s device, the second form collects a PDF’s password, while the canvas element will be used to render the PDF’s pages.
Since we’re aiming for a visually pleasing PDF upload form (even though it’s just a demo), let’s replace the contents of the project’s src/style.css file with the following style rules:
Import the Project’s Dependencies
Delete the contents of the project’s src/main.js file. Then, import and configure PDF.js and the project’s stylesheet into the src/main.js script shown below.

At this point, the project’s index page should look similar to the following image if you run npm run dev -- --open
from the root folder of the project.

Display the PDF’s Contents
Copy the following code into the src/main.js file. It attaches an event listener to the file input field of the pdf-form element defined in the index.html file. The event listener detects when a user selects a PDF and its handler attempts to display the document’s contents on the screen.
Now, let’s define the viewPDF event handler, which is passed as the second argument of the addEventListener method. Copy the following code into the src/main.js file. Check out the comments in the function definition for some insight into what each statement and expression does.
Prompt for a Password and unlock password-protected PDFs
The handlePDFPassword function set as the loadingTask’s onPassword event handler is undefined at this point. Let’s define it by adding the below function to the src/main.js file.
handlePDFPassword gets called only if PDF.js detects password protection when loading a PDF document. Users trying to view non-password-protected PDFs won’t be prompted for a password.
Render the Unlocked PDF’s pages
Finally, copy the following function definition to the src/main.js file. Like its name suggests, the renderPage function renders a page of the loaded PDF onto the web page.
Try It Out!
Open the app in your browser (by running npm run dev -- --open
), click the Choose file button, and select a password-protected PDF file.

The app will prompt for the PDF’s password using the popup form shown below before displaying the PDF’s contents.

Go Further
The renderPage function defined above renders only the first page of the PDF document. See these examples for guidance on adding pagination and/or better error handling to the app.
Need to build PDF capabilities inside your SaaS application? Joyfill makes it easy for developers to natively build and embed form and PDF experiences inside their own SaaS applications.