feat(merge): Migrate merge operations to Web Workers and reorganize tool categories
- Create merge.worker.js and alternate-merge.worker.js for offloading PDF merge operations to background threads - Migrate merge and alternate-merge logic to use Web Worker architecture for improved performance - Move flatten tool from singlePdfLoadTools to multiFileTools configuration - Update merge and alternate-merge tool subtitles to indicate bookmark preservation - Refactor alternate-merge.ts to use Web Worker instead of pdf-lib for interleaving operations - Refactor merge.ts to use Web Worker for standard merge operations - Update fileHandler.ts to recognize flatten as a multi-file tool requiring process button handling - Simplify form-creator.ts and flatten.ts implementations with Web Worker integration - Update UI state management to support Web Worker-based PDF processing - Improve performance by moving computationally intensive PDF operations off the main thread
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { showLoader, hideLoader, showAlert } from '../ui.js';
|
||||
import { downloadFile } from '../utils/helpers.js';
|
||||
import { downloadFile, readFileAsArrayBuffer } from '../utils/helpers.js';
|
||||
import { state } from '../state.js';
|
||||
import { PDFDocument } from 'pdf-lib';
|
||||
|
||||
export function flattenFormsInDoc(pdfDoc) {
|
||||
const form = pdfDoc.getForm();
|
||||
@@ -8,30 +9,78 @@ export function flattenFormsInDoc(pdfDoc) {
|
||||
}
|
||||
|
||||
export async function flatten() {
|
||||
if (!state.pdfDoc) {
|
||||
showAlert('Error', 'PDF not loaded.');
|
||||
if (state.files.length === 0) {
|
||||
showAlert('No Files', 'Please select at least one PDF file.');
|
||||
return;
|
||||
}
|
||||
showLoader('Flattening PDF...');
|
||||
try {
|
||||
flattenFormsInDoc(state.pdfDoc);
|
||||
|
||||
const flattenedBytes = await state.pdfDoc.save();
|
||||
downloadFile(
|
||||
new Blob([flattenedBytes], { type: 'application/pdf' }),
|
||||
'flattened.pdf'
|
||||
);
|
||||
try {
|
||||
if (state.files.length === 1) {
|
||||
showLoader('Flattening PDF...');
|
||||
const file = state.files[0];
|
||||
const arrayBuffer = await readFileAsArrayBuffer(file);
|
||||
const pdfDoc = await PDFDocument.load(arrayBuffer as ArrayBuffer, { ignoreEncryption: true });
|
||||
|
||||
try {
|
||||
flattenFormsInDoc(pdfDoc);
|
||||
} catch (e) {
|
||||
if (e.message.includes('getForm')) {
|
||||
// Ignore if no form found
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
const flattenedBytes = await pdfDoc.save();
|
||||
downloadFile(
|
||||
new Blob([flattenedBytes as any], { type: 'application/pdf' }),
|
||||
`flattened_${file.name}`
|
||||
);
|
||||
hideLoader();
|
||||
} else {
|
||||
showLoader('Flattening multiple PDFs...');
|
||||
const JSZip = (await import('jszip')).default;
|
||||
const zip = new JSZip();
|
||||
let processedCount = 0;
|
||||
|
||||
for (let i = 0; i < state.files.length; i++) {
|
||||
const file = state.files[i];
|
||||
showLoader(`Flattening ${i + 1}/${state.files.length}: ${file.name}...`);
|
||||
|
||||
try {
|
||||
const arrayBuffer = await readFileAsArrayBuffer(file);
|
||||
const pdfDoc = await PDFDocument.load(arrayBuffer as ArrayBuffer, { ignoreEncryption: true });
|
||||
|
||||
try {
|
||||
flattenFormsInDoc(pdfDoc);
|
||||
} catch (e) {
|
||||
if (e.message.includes('getForm')) {
|
||||
// Ignore if no form found
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
const flattenedBytes = await pdfDoc.save();
|
||||
zip.file(`flattened_${file.name}`, flattenedBytes);
|
||||
processedCount++;
|
||||
} catch (e) {
|
||||
console.error(`Error processing ${file.name}:`, e);
|
||||
}
|
||||
}
|
||||
|
||||
if (processedCount > 0) {
|
||||
const zipBlob = await zip.generateAsync({ type: 'blob' });
|
||||
downloadFile(zipBlob, 'flattened_pdfs.zip');
|
||||
showAlert('Success', `Processed ${processedCount} PDFs.`);
|
||||
} else {
|
||||
showAlert('Error', 'No PDFs could be processed.');
|
||||
}
|
||||
hideLoader();
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
if (e.message.includes('getForm')) {
|
||||
showAlert(
|
||||
'No Form Found',
|
||||
'This PDF does not contain any form fields to flatten.'
|
||||
);
|
||||
} else {
|
||||
showAlert('Error', 'Could not flatten the PDF.');
|
||||
}
|
||||
} finally {
|
||||
hideLoader();
|
||||
showAlert('Error', e.message || 'An unexpected error occurred.');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user