feat: add PDF password prompt and centralized pdf-lib loader with auto-repair

This commit is contained in:
alam00000
2026-03-26 14:42:48 +05:30
parent 9278774b8a
commit bd44108296
19 changed files with 89 additions and 78 deletions

View File

@@ -85,9 +85,7 @@ async function updateUI() {
}
showLoader('Loading PDF...');
pageState.file = result.file;
pageState.pdfDoc = await loadPdfDocument(result.bytes, {
throwOnInvalidObject: false,
});
pageState.pdfDoc = await loadPdfDocument(result.bytes);
result.pdf.destroy();
hideLoader();

View File

@@ -174,9 +174,7 @@ async function handleFiles(files: FileList) {
showLoader(translate('tools:addPageLabels.loadingPdf', 'Loading PDF...'));
try {
const arrayBuffer = await readFileAsArrayBuffer(file);
const pdfDoc = await loadPdfDocument(arrayBuffer as ArrayBuffer, {
throwOnInvalidObject: false,
});
const pdfDoc = await loadPdfDocument(arrayBuffer as ArrayBuffer);
if (pdfDoc.isEncrypted) {
showAlert(

View File

@@ -82,9 +82,7 @@ async function updateUI() {
showLoader('Loading PDF...');
result.pdf.destroy();
pageState.file = result.file;
pageState.pdfDoc = await loadPdfDocument(result.bytes, {
throwOnInvalidObject: false,
});
pageState.pdfDoc = await loadPdfDocument(result.bytes);
hideLoader();
const pageCount = pageState.pdfDoc.getPageCount();

View File

@@ -306,9 +306,7 @@ async function performCrop() {
async function performMetadataCrop(
cropData: Record<number, any>
): Promise<Uint8Array> {
const pdfToModify = await loadPdfDocument(cropperState.originalPdfBytes!, {
throwOnInvalidObject: false,
});
const pdfToModify = await loadPdfDocument(cropperState.originalPdfBytes!);
for (const pageNum in cropData) {
const pdfJsPage = await cropperState.pdfDoc.getPage(Number(pageNum));
@@ -350,8 +348,7 @@ async function performFlatteningCrop(
): Promise<Uint8Array> {
const newPdfDoc = await PDFLibDocument.create();
const sourcePdfDocForCopying = await loadPdfDocument(
cropperState.originalPdfBytes!,
{ throwOnInvalidObject: false }
cropperState.originalPdfBytes!
);
const totalPages = cropperState.pdfDoc.numPages;

View File

@@ -188,8 +188,7 @@ async function performFlatteningCrop(cropData: any) {
// Load the original PDF with pdf-lib to copy un-cropped pages from
const sourcePdfDocForCopying = await loadPdfDocument(
cropperState.originalPdfBytes,
{ ignoreEncryption: true, throwOnInvalidObject: false }
cropperState.originalPdfBytes
);
const totalPages = cropperState.pdfDoc.numPages;
@@ -332,8 +331,7 @@ export async function setupCropperTool() {
finalPdfBytes = await newPdfDoc.save();
} else {
const pdfToModify = await loadPdfDocument(
cropperState.originalPdfBytes,
{ ignoreEncryption: true, throwOnInvalidObject: false }
cropperState.originalPdfBytes
);
await performMetadataCrop(pdfToModify, finalCropData);
finalPdfBytes = await pdfToModify.save();

View File

@@ -94,9 +94,7 @@ async function handleFile(file: File) {
}
showLoader('Loading PDF...');
deleteState.file = result.file;
deleteState.pdfDoc = await loadPdfDocument(result.bytes, {
throwOnInvalidObject: false,
});
deleteState.pdfDoc = await loadPdfDocument(result.bytes);
deleteState.pdfJsDoc = result.pdf;
deleteState.totalPages = deleteState.pdfDoc.getPageCount();
deleteState.pagesToDelete = new Set();

View File

@@ -229,9 +229,7 @@ async function updateUI() {
showLoader('Loading PDF...');
result.pdf.destroy();
pageState.file = result.file;
pageState.pdfDoc = await loadPdfDocument(result.bytes, {
throwOnInvalidObject: false,
});
pageState.pdfDoc = await loadPdfDocument(result.bytes);
hideLoader();
const pageCount = pageState.pdfDoc.getPageCount();

View File

@@ -100,9 +100,7 @@ async function handleFile(file: File) {
showLoader('Loading PDF...');
extractState.file = result.file;
result.pdf.destroy();
extractState.pdfDoc = await loadPdfDocument(result.bytes, {
throwOnInvalidObject: false,
});
extractState.pdfDoc = await loadPdfDocument(result.bytes);
extractState.totalPages = extractState.pdfDoc.getPageCount();
updateFileDisplay();

View File

@@ -75,9 +75,7 @@ async function updateUI() {
showLoader('Loading PDF...');
result.pdf.destroy();
pageState.file = result.file;
pageState.pdfDoc = await loadPdfDocument(result.bytes, {
throwOnInvalidObject: false,
});
pageState.pdfDoc = await loadPdfDocument(result.bytes);
hideLoader();
const pageCount = pageState.pdfDoc.getPageCount();

View File

@@ -177,9 +177,7 @@ async function handleFile(file: File) {
if (!result) return;
showLoader('Loading PDF...');
organizeState.pdfDoc = await loadPdfDocument(result.bytes, {
throwOnInvalidObject: false,
});
organizeState.pdfDoc = await loadPdfDocument(result.bytes);
organizeState.pdfJsDoc = result.pdf;
organizeState.file = result.file;
organizeState.totalPages = organizeState.pdfDoc.getPageCount();

View File

@@ -99,9 +99,7 @@ async function updateUI() {
pageState.pdfBytes = new Uint8Array(result.bytes);
pageState.pdfjsDoc = result.pdf;
pageState.pdfDoc = await loadPdfDocument(pageState.pdfBytes, {
throwOnInvalidObject: false,
});
pageState.pdfDoc = await loadPdfDocument(pageState.pdfBytes);
hideLoader();

View File

@@ -436,9 +436,7 @@ async function loadPdfs(files: File[]) {
pwResult.pdf.destroy();
arrayBuffer = pwResult.bytes as ArrayBuffer;
const pdfDoc = await loadPdfDocument(arrayBuffer, {
throwOnInvalidObject: false,
});
const pdfDoc = await loadPdfDocument(arrayBuffer);
currentPdfDocs.push(pdfDoc);
const pdfIndex = currentPdfDocs.length - 1;
@@ -859,9 +857,7 @@ async function handleInsertPdf(e: Event) {
if (!pwResult) return;
pwResult.pdf.destroy();
const pdfDoc = await loadPdfDocument(pwResult.bytes, {
throwOnInvalidObject: false,
});
const pdfDoc = await loadPdfDocument(pwResult.bytes);
currentPdfDocs.push(pdfDoc);
const pdfIndex = currentPdfDocs.length - 1;

View File

@@ -236,9 +236,7 @@ async function updateUI() {
}
showLoader('Loading PDF...');
pageState.pdfDoc = await loadPdfDocument(result.bytes, {
throwOnInvalidObject: false,
});
pageState.pdfDoc = await loadPdfDocument(result.bytes);
pageState.pdfJsDoc = result.pdf;

View File

@@ -208,9 +208,7 @@ async function updateUI() {
}
showLoader('Loading PDF...');
pageState.pdfDoc = await loadPdfDocument(result.bytes, {
throwOnInvalidObject: false,
});
pageState.pdfDoc = await loadPdfDocument(result.bytes);
pageState.pdfJsDoc = result.pdf;

View File

@@ -90,19 +90,17 @@ document.addEventListener('DOMContentLoaded', () => {
// Load PDF Document
try {
if (!state.pdfDoc) {
const result = await loadPdfWithPasswordPrompt(file);
if (!result) {
state.files = [];
updateUI();
return;
}
result.pdf.destroy();
state.files[0] = result.file;
state.pdfDoc = await loadPdfDocument(result.bytes);
const result = await loadPdfWithPasswordPrompt(file);
if (!result) {
state.files = [];
updateUI();
return;
}
// Update page count
metaSpan.textContent = `${formatBytes(file.size)}${state.pdfDoc.getPageCount()} pages`;
const pageCount = result.pdf.numPages;
result.pdf.destroy();
state.files[0] = result.file;
state.pdfDoc = await loadPdfDocument(result.bytes);
metaSpan.textContent = `${formatBytes(file.size)}${pageCount} pages`;
} catch (error) {
console.error('Error loading PDF:', error);
showAlert('Error', 'Failed to load PDF file.');