Password Protect PDF



Secure PDF with Password

Password Protect PDF





body { font-family: Arial, sans-serif; background-color: #f4f4f4; display: flex; justify-content: center; align-items: center; height: 100vh; } .container { background-color: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); text-align: center; } input { padding: 10px; margin: 10px; width: 100%; border: 1px solid #ccc; border-radius: 4px; } button { padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; } button:hover { background-color: #0056b3; } #downloadLink { margin-top: 20px; display: block; color: #007bff; text-decoration: none; }const form = document.getElementById('pdfForm'); const downloadLink = document.getElementById('downloadLink'); form.addEventListener('submit', async (e) => { e.preventDefault(); const fileInput = document.getElementById('pdfFile').files[0]; const password = document.getElementById('password').value; if (fileInput && password) { const reader = new FileReader(); reader.onload = async function () { const pdfBytes = new Uint8Array(reader.result); const pdfDoc = await PDFLib.PDFDocument.load(pdfBytes); pdfDoc.setPassword(password); const securedPdfBytes = await pdfDoc.save(); const blob = new Blob([securedPdfBytes], { type: 'application/pdf' }); const url = URL.createObjectURL(blob); downloadLink.href = url; downloadLink.download = 'secured.pdf'; downloadLink.style.display = 'block'; }; reader.readAsArrayBuffer(fileInput); } else { alert('Please upload a PDF file and enter a password.'); } });