Add password prompt functionality and tests while uploading encrypted PDF

This commit is contained in:
alam00000
2026-03-26 12:11:12 +05:30
parent f88f872162
commit 1dbf907eeb
79 changed files with 7869 additions and 4419 deletions

View File

@@ -4,6 +4,7 @@ import * as pdfjsLib from 'pdfjs-dist';
import JSZip from 'jszip';
import Sortable from 'sortablejs';
import { downloadFile, getPDFDocument } from '../utils/helpers';
import { loadPdfWithPasswordPrompt } from '../utils/password-prompt.js';
import {
renderPagesProgressively,
cleanupLazyRendering,
@@ -428,6 +429,12 @@ async function loadPdfs(files: File[]) {
arrayBuffer = await file.arrayBuffer();
}
hideLoading();
const pwResult = await loadPdfWithPasswordPrompt(file);
if (!pwResult) continue;
pwResult.pdf.destroy();
arrayBuffer = pwResult.bytes as ArrayBuffer;
const pdfDoc = await PDFLibDocument.load(arrayBuffer, {
ignoreEncryption: true,
throwOnInvalidObject: false,
@@ -848,15 +855,17 @@ async function handleInsertPdf(e: Event) {
if (insertAfterIndex === undefined) return;
try {
const arrayBuffer = await file.arrayBuffer();
const pdfDoc = await PDFLibDocument.load(arrayBuffer, {
const pwResult = await loadPdfWithPasswordPrompt(file);
if (!pwResult) return;
pwResult.pdf.destroy();
const pdfDoc = await PDFLibDocument.load(pwResult.bytes, {
ignoreEncryption: true,
throwOnInvalidObject: false,
});
currentPdfDocs.push(pdfDoc);
const pdfIndex = currentPdfDocs.length - 1;
// Load PDF.js document for rendering
const pdfBytes = await pdfDoc.save();
const pdfjsDoc = await getPDFDocument({ data: new Uint8Array(pdfBytes) })
.promise;