Privacy Note: Your files are decrypted locally in your browser. No passwords or documents are sent to our servers.
// — JAVASCRIPT SEPARATION START —// DOM Elements
const dropzone = document.getElementById(‘upload-stage’);
const fileInput = document.getElementById(‘fileInput’);
const passwordStage = document.getElementById(‘password-stage’);
const successStage = document.getElementById(‘success-stage’);
const filenameDisplay = document.getElementById(‘filename-display’);
const passwordInput = document.getElementById(‘pdf-password’);
const btnUnlock = document.getElementById(‘btn-unlock’);
const btnText = document.getElementById(‘btn-text’);
const btnLoader = document.getElementById(‘btn-loader’);
const errorMsg = document.getElementById(‘error-msg’);
const downloadBtn = document.getElementById(‘download-btn’);
const eyeIcon = document.getElementById(‘eye-icon’);let currentFile = null;
let isDarkMode = false;// — Event Listeners —
dropzone.addEventListener(‘click’, () => fileInput.click());
dropzone.addEventListener(‘dragover’, (e) => {
e.preventDefault();
dropzone.classList.add(‘dropzone-active’);
});
dropzone.addEventListener(‘dragleave’, () => {
dropzone.classList.remove(‘dropzone-active’);
});
dropzone.addEventListener(‘drop’, (e) => {
e.preventDefault();
dropzone.classList.remove(‘dropzone-active’);
if (e.dataTransfer.files.length) handleFile(e.dataTransfer.files[0]);
});fileInput.addEventListener(‘change’, (e) => {
if (e.target.files.length) handleFile(e.target.files[0]);
});passwordInput.addEventListener(‘keypress’, (e) => {
if (e.key === ‘Enter’) unlockPDF();
});// — Functions —async function handleFile(file) {
if (file.type !== ‘application/pdf’) {
alert(‘Please upload a valid PDF file.’);
return;
}currentFile = file;
// Check if file is actually encrypted first
// We try to load it without password. If it fails, it needs one.
try {
const arrayBuffer = await file.arrayBuffer();
await PDFLib.PDFDocument.load(arrayBuffer);
// If we reach here, it has no password!
// We can just “unlock” it immediately (essentially pass-through)
// But user expects an unlock tool for locked files.
// Let’s proceed to success directly if no password needed.
prepareDownload(arrayBuffer, file.name);
dropzone.classList.add(‘hidden’);
successStage.classList.remove(‘hidden’);
// Ideally change success text to “No password was found!”
} catch (error) {
// Error likely means password required
filenameDisplay.innerText = file.name;
dropzone.classList.add(‘hidden’);
passwordStage.classList.remove(‘hidden’);
passwordInput.focus();
}
}async function unlockPDF() {
const password = passwordInput.value;
if (!password) return;btnUnlock.disabled = true;
btnText.innerText = ‘Unlocking…’;
btnLoader.classList.remove(‘hidden’);
errorMsg.classList.add(‘hidden’);try {
const arrayBuffer = await currentFile.arrayBuffer();
// Load with password
const pdfDoc = await PDFLib.PDFDocument.load(arrayBuffer, { password: password });
// Save without encryption
const pdfBytes = await pdfDoc.save();
prepareDownload(pdfBytes, currentFile.name);
// Show Success
passwordStage.classList.add(‘hidden’);
successStage.classList.remove(‘hidden’);} catch (error) {
console.error(error);
errorMsg.classList.remove(‘hidden’);
// Reset button
btnUnlock.disabled = false;
btnText.innerText = ‘Unlock PDF’;
btnLoader.classList.add(‘hidden’);
}
}function prepareDownload(bytes, originalName) {
const blob = new Blob([bytes], { type: ‘application/pdf’ });
const url = URL.createObjectURL(blob);
downloadBtn.href = url;
downloadBtn.download = ‘unlocked_’ + originalName;
}function togglePasswordVisibility() {
if (passwordInput.type === “password”) {
passwordInput.type = “text”;
eyeIcon.classList.remove(“fa-eye”);
eyeIcon.classList.add(“fa-eye-slash”);
} else {
passwordInput.type = “password”;
eyeIcon.classList.remove(“fa-eye-slash”);
eyeIcon.classList.add(“fa-eye”);
}
}function resetApp() {
currentFile = null;
fileInput.value = ”;
passwordInput.value = ”;
dropzone.classList.remove(‘hidden’);
passwordStage.classList.add(‘hidden’);
successStage.classList.add(‘hidden’);
btnUnlock.disabled = false;
btnText.innerText = ‘Unlock PDF’;
btnLoader.classList.add(‘hidden’);
errorMsg.classList.add(‘hidden’);
}function toggleDarkMode() {
isDarkMode = !isDarkMode;
document.documentElement.classList.toggle(‘dark’);
const icon = document.getElementById(‘theme-icon’);
if(isDarkMode) {
icon.classList.remove(‘fa-moon’);
icon.classList.add(‘fa-sun’);
} else {
icon.classList.remove(‘fa-sun’);
icon.classList.add(‘fa-moon’);
}
}// — JAVASCRIPT SEPARATION END —
About the Unlock PDF tool
The Unlock PDF page is part of Articles Stock’s online utilities collection. Use the tool above for the task described in its interface, and check the available file types, settings and output before proceeding. This utility relates to document conversion and PDF workflows.
How to use this tool
Review the tool’s supported input and options.
Select or enter the required input and choose the settings that suit your task.
Run the tool and check the result before saving or sharing it.
Related online tools
For related document tasks, try Lock PDF to protect a file you are authorized to secure, or Merge PDF to combine documents.