feat: enable loading of encrypted PDFs by adding ignoreEncryption option

This commit is contained in:
alam00000
2026-03-26 12:55:50 +05:30
parent 816dc311ae
commit 9d362b1cf8
24 changed files with 56 additions and 17 deletions

View File

@@ -139,7 +139,9 @@ async function updateUI() {
pageState.file = result.file; pageState.file = result.file;
showLoader('Loading PDF...'); showLoader('Loading PDF...');
pageState.pdfDoc = await PDFLibDocument.load(result.bytes); pageState.pdfDoc = await PDFLibDocument.load(result.bytes, {
ignoreEncryption: true,
});
const pageCount = pageState.pdfDoc.getPageCount(); const pageCount = pageState.pdfDoc.getPageCount();
metaSpan.textContent = `${formatBytes(pageState.file.size)}${pageCount} pages`; metaSpan.textContent = `${formatBytes(pageState.file.size)}${pageCount} pages`;

View File

@@ -85,6 +85,7 @@ async function updateUI() {
showLoader('Loading PDF...'); showLoader('Loading PDF...');
pageState.file = result.file; pageState.file = result.file;
pageState.pdfDoc = await PDFLibDocument.load(result.bytes, { pageState.pdfDoc = await PDFLibDocument.load(result.bytes, {
ignoreEncryption: true,
throwOnInvalidObject: false, throwOnInvalidObject: false,
}); });
result.pdf.destroy(); result.pdf.destroy();

View File

@@ -63,7 +63,9 @@ async function handleFiles(files: FileList) {
if (!result) return; if (!result) return;
showLoader('Loading PDF...'); showLoader('Loading PDF...');
result.pdf.destroy(); result.pdf.destroy();
pageState.pdfDoc = await PDFLibDocument.load(result.bytes); pageState.pdfDoc = await PDFLibDocument.load(result.bytes, {
ignoreEncryption: true,
});
pageState.file = result.file; pageState.file = result.file;
updateFileDisplay(); updateFileDisplay();
document.getElementById('options-panel')?.classList.remove('hidden'); document.getElementById('options-panel')?.classList.remove('hidden');

View File

@@ -184,7 +184,9 @@ async function handleFiles(fileList: FileList) {
if (!result) continue; if (!result) continue;
showLoader('Loading PDFs...'); showLoader('Loading PDFs...');
result.pdf.destroy(); result.pdf.destroy();
const pdfDoc = await PDFDocument.load(result.bytes); const pdfDoc = await PDFDocument.load(result.bytes, {
ignoreEncryption: true,
});
files.push({ file: result.file, pageCount: pdfDoc.getPageCount() }); files.push({ file: result.file, pageCount: pdfDoc.getPageCount() });
} }

View File

@@ -82,6 +82,7 @@ async function updateUI() {
result.pdf.destroy(); result.pdf.destroy();
pageState.file = result.file; pageState.file = result.file;
pageState.pdfDoc = await PDFLibDocument.load(result.bytes, { pageState.pdfDoc = await PDFLibDocument.load(result.bytes, {
ignoreEncryption: true,
throwOnInvalidObject: false, throwOnInvalidObject: false,
}); });
hideLoader(); hideLoader();

View File

@@ -95,6 +95,7 @@ async function handleFile(file: File) {
showLoader('Loading PDF...'); showLoader('Loading PDF...');
deleteState.file = result.file; deleteState.file = result.file;
deleteState.pdfDoc = await PDFDocument.load(result.bytes, { deleteState.pdfDoc = await PDFDocument.load(result.bytes, {
ignoreEncryption: true,
throwOnInvalidObject: false, throwOnInvalidObject: false,
}); });
deleteState.pdfJsDoc = result.pdf; deleteState.pdfJsDoc = result.pdf;

View File

@@ -87,7 +87,9 @@ async function updateUI() {
pageState.file = result.file; pageState.file = result.file;
showLoader('Loading PDF...'); showLoader('Loading PDF...');
pageState.pdfDoc = await PDFLibDocument.load(result.bytes); pageState.pdfDoc = await PDFLibDocument.load(result.bytes, {
ignoreEncryption: true,
});
pageState.totalPages = pageState.pdfDoc.getPageCount(); pageState.totalPages = pageState.pdfDoc.getPageCount();
hideLoader(); hideLoader();

View File

@@ -229,6 +229,7 @@ async function updateUI() {
result.pdf.destroy(); result.pdf.destroy();
pageState.file = result.file; pageState.file = result.file;
pageState.pdfDoc = await PDFLibDocument.load(result.bytes, { pageState.pdfDoc = await PDFLibDocument.load(result.bytes, {
ignoreEncryption: true,
throwOnInvalidObject: false, throwOnInvalidObject: false,
}); });
hideLoader(); hideLoader();

View File

@@ -100,6 +100,7 @@ async function handleFile(file: File) {
extractState.file = result.file; extractState.file = result.file;
result.pdf.destroy(); result.pdf.destroy();
extractState.pdfDoc = await PDFDocument.load(result.bytes, { extractState.pdfDoc = await PDFDocument.load(result.bytes, {
ignoreEncryption: true,
throwOnInvalidObject: false, throwOnInvalidObject: false,
}); });
extractState.totalPages = extractState.pdfDoc.getPageCount(); extractState.totalPages = extractState.pdfDoc.getPageCount();

View File

@@ -3140,7 +3140,9 @@ async function handlePdfUpload(file: File) {
if (!result) return; if (!result) return;
const arrayBuffer = result.bytes; const arrayBuffer = result.bytes;
uploadedPdfjsDoc = result.pdf; uploadedPdfjsDoc = result.pdf;
uploadedPdfDoc = await PDFDocument.load(arrayBuffer); uploadedPdfDoc = await PDFDocument.load(arrayBuffer, {
ignoreEncryption: true,
});
// Check for existing fields and update counter // Check for existing fields and update counter
existingFieldNames.clear(); existingFieldNames.clear();

View File

@@ -68,7 +68,9 @@ async function handleFiles(files: FileList) {
if (!result) return; if (!result) return;
showLoader('Loading PDF...'); showLoader('Loading PDF...');
pageState.pdfDoc = await PDFLibDocument.load(result.bytes); pageState.pdfDoc = await PDFLibDocument.load(result.bytes, {
ignoreEncryption: true,
});
pageState.file = result.file; pageState.file = result.file;
result.pdf.destroy(); result.pdf.destroy();

View File

@@ -70,7 +70,9 @@ async function handleFiles(files: FileList) {
if (!result) return; if (!result) return;
showLoader('Loading PDF...'); showLoader('Loading PDF...');
result.pdf.destroy(); result.pdf.destroy();
pageState.pdfDoc = await PDFLibDocument.load(result.bytes); pageState.pdfDoc = await PDFLibDocument.load(result.bytes, {
ignoreEncryption: true,
});
pageState.file = result.file; pageState.file = result.file;
updateFileDisplay(); updateFileDisplay();
document.getElementById('options-panel')?.classList.remove('hidden'); document.getElementById('options-panel')?.classList.remove('hidden');

View File

@@ -75,6 +75,7 @@ async function updateUI() {
result.pdf.destroy(); result.pdf.destroy();
pageState.file = result.file; pageState.file = result.file;
pageState.pdfDoc = await PDFLibDocument.load(result.bytes, { pageState.pdfDoc = await PDFLibDocument.load(result.bytes, {
ignoreEncryption: true,
throwOnInvalidObject: false, throwOnInvalidObject: false,
}); });
hideLoader(); hideLoader();

View File

@@ -177,6 +177,7 @@ async function handleFile(file: File) {
showLoader('Loading PDF...'); showLoader('Loading PDF...');
organizeState.pdfDoc = await PDFDocument.load(result.bytes, { organizeState.pdfDoc = await PDFDocument.load(result.bytes, {
ignoreEncryption: true,
throwOnInvalidObject: false, throwOnInvalidObject: false,
}); });
organizeState.pdfJsDoc = result.pdf; organizeState.pdfJsDoc = result.pdf;

View File

@@ -341,7 +341,9 @@ async function handleFileSelect(files: FileList | null) {
result.pdf.destroy(); result.pdf.destroy();
pageState.file = result.file; pageState.file = result.file;
pageState.pdfDoc = await PDFDocument.load(result.bytes); pageState.pdfDoc = await PDFDocument.load(result.bytes, {
ignoreEncryption: true,
});
updateUI(); updateUI();
analyzeAndDisplayDimensions(); analyzeAndDisplayDimensions();
} catch (e) { } catch (e) {

View File

@@ -89,7 +89,9 @@ async function handleFiles(files: FileList) {
if (!result) return; if (!result) return;
showLoader('Loading PDF...'); showLoader('Loading PDF...');
pageState.pdfDoc = await PDFLibDocument.load(result.bytes); pageState.pdfDoc = await PDFLibDocument.load(result.bytes, {
ignoreEncryption: true,
});
pageState.file = result.file; pageState.file = result.file;
result.pdf.destroy(); result.pdf.destroy();

View File

@@ -108,7 +108,9 @@ async function handleFileUpload(file: File) {
if (!result) return; if (!result) return;
showLoader('Loading PDF...'); showLoader('Loading PDF...');
result.pdf.destroy(); result.pdf.destroy();
pageState.pdfDoc = await PDFDocument.load(result.bytes); pageState.pdfDoc = await PDFDocument.load(result.bytes, {
ignoreEncryption: true,
});
pageState.file = result.file; pageState.file = result.file;
updateFileDisplay(); updateFileDisplay();
document.getElementById('options-panel')?.classList.remove('hidden'); document.getElementById('options-panel')?.classList.remove('hidden');

View File

@@ -122,7 +122,9 @@ async function handleFileUpload(file: File) {
if (!result) return; if (!result) return;
showLoader('Loading PDF...'); showLoader('Loading PDF...');
result.pdf.destroy(); result.pdf.destroy();
pageState.pdfDoc = await PDFDocument.load(result.bytes); pageState.pdfDoc = await PDFDocument.load(result.bytes, {
ignoreEncryption: true,
});
pageState.file = result.file; pageState.file = result.file;
pageState.detectedBlankPages = []; pageState.detectedBlankPages = [];
updateFileDisplay(); updateFileDisplay();

View File

@@ -150,7 +150,9 @@ async function removeMetadata() {
if (loaderModal) loaderModal.classList.remove('hidden'); if (loaderModal) loaderModal.classList.remove('hidden');
if (loaderText) loaderText.textContent = 'Removing all metadata...'; if (loaderText) loaderText.textContent = 'Removing all metadata...';
result.pdf.destroy(); result.pdf.destroy();
const pdfDoc = await PDFDocument.load(result.bytes); const pdfDoc = await PDFDocument.load(result.bytes, {
ignoreEncryption: true,
});
removeMetadataFromDoc(pdfDoc); removeMetadataFromDoc(pdfDoc);

View File

@@ -236,6 +236,7 @@ async function updateUI() {
showLoader('Loading PDF...'); showLoader('Loading PDF...');
pageState.pdfDoc = await PDFLibDocument.load(result.bytes, { pageState.pdfDoc = await PDFLibDocument.load(result.bytes, {
ignoreEncryption: true,
throwOnInvalidObject: false, throwOnInvalidObject: false,
}); });

View File

@@ -208,6 +208,7 @@ async function updateUI() {
showLoader('Loading PDF...'); showLoader('Loading PDF...');
pageState.pdfDoc = await PDFLibDocument.load(result.bytes, { pageState.pdfDoc = await PDFLibDocument.load(result.bytes, {
ignoreEncryption: true,
throwOnInvalidObject: false, throwOnInvalidObject: false,
}); });

View File

@@ -98,7 +98,9 @@ document.addEventListener('DOMContentLoaded', () => {
} }
result.pdf.destroy(); result.pdf.destroy();
state.files[0] = result.file; state.files[0] = result.file;
state.pdfDoc = await PDFLibDocument.load(result.bytes); state.pdfDoc = await PDFLibDocument.load(result.bytes, {
ignoreEncryption: true,
});
} }
// Update page count // Update page count
metaSpan.textContent = `${formatBytes(file.size)}${state.pdfDoc.getPageCount()} pages`; metaSpan.textContent = `${formatBytes(file.size)}${state.pdfDoc.getPageCount()} pages`;
@@ -146,7 +148,9 @@ document.addEventListener('DOMContentLoaded', () => {
} }
result.pdf.destroy(); result.pdf.destroy();
state.files[0] = result.file; state.files[0] = result.file;
state.pdfDoc = await PDFLibDocument.load(result.bytes); state.pdfDoc = await PDFLibDocument.load(result.bytes, {
ignoreEncryption: true,
});
showLoader('Rendering page previews...'); showLoader('Rendering page previews...');
} else { } else {
throw new Error('No PDF document loaded'); throw new Error('No PDF document loaded');

View File

@@ -75,7 +75,9 @@ async function handleFiles(files: FileList) {
if (!result) return; if (!result) return;
showLoader('Loading PDF...'); showLoader('Loading PDF...');
result.pdf.destroy(); result.pdf.destroy();
pageState.pdfDoc = await PDFLibDocument.load(result.bytes); pageState.pdfDoc = await PDFLibDocument.load(result.bytes, {
ignoreEncryption: true,
});
pageState.file = result.file; pageState.file = result.file;
updateFileDisplay(); updateFileDisplay();
document.getElementById('options-panel')?.classList.remove('hidden'); document.getElementById('options-panel')?.classList.remove('hidden');

View File

@@ -792,7 +792,7 @@ export async function loadPdfWithPasswordPrompt(
let currentFile = file; let currentFile = file;
try { try {
const pdf = await getPDFDocument(bytes).promise; const pdf = await getPDFDocument(bytes.slice(0)).promise;
return { pdf, bytes, file: currentFile }; return { pdf, bytes, file: currentFile };
} catch (err: unknown) { } catch (err: unknown) {
if ( if (
@@ -808,7 +808,7 @@ export async function loadPdfWithPasswordPrompt(
files[index] = decryptedFile; files[index] = decryptedFile;
} }
bytes = (await readFileAsArrayBuffer(decryptedFile)) as ArrayBuffer; bytes = (await readFileAsArrayBuffer(decryptedFile)) as ArrayBuffer;
const pdf = await getPDFDocument(bytes).promise; const pdf = await getPDFDocument(bytes.slice(0)).promise;
return { pdf, bytes, file: currentFile }; return { pdf, bytes, file: currentFile };
} }
throw err; throw err;