feat: Add alerts for attachment extraction status and improve UI state handling after processing.
feat: add page and document level support for adding atttachments
This commit is contained in:
@@ -1,28 +1,89 @@
|
||||
self.importScripts('/coherentpdf.browser.min.js');
|
||||
|
||||
function addAttachmentsToPDFInWorker(pdfBuffer, attachmentBuffers, attachmentNames) {
|
||||
function parsePageRange(rangeString, totalPages) {
|
||||
const pages = new Set();
|
||||
const parts = rangeString.split(',').map(s => s.trim());
|
||||
|
||||
for (const part of parts) {
|
||||
if (part.includes('-')) {
|
||||
const [start, end] = part.split('-').map(s => parseInt(s.trim(), 10));
|
||||
if (isNaN(start) || isNaN(end)) continue;
|
||||
for (let i = Math.max(1, start); i <= Math.min(totalPages, end); i++) {
|
||||
pages.add(i);
|
||||
}
|
||||
} else {
|
||||
const pageNum = parseInt(part, 10);
|
||||
if (!isNaN(pageNum) && pageNum >= 1 && pageNum <= totalPages) {
|
||||
pages.add(pageNum);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Array.from(pages).sort((a, b) => a - b);
|
||||
}
|
||||
|
||||
function addAttachmentsToPDFInWorker(pdfBuffer, attachmentBuffers, attachmentNames, attachmentLevel, pageRange) {
|
||||
try {
|
||||
const uint8Array = new Uint8Array(pdfBuffer);
|
||||
|
||||
|
||||
let pdf;
|
||||
try {
|
||||
pdf = coherentpdf.fromMemory(uint8Array, '');
|
||||
} catch (error) {
|
||||
self.postMessage({
|
||||
status: 'error',
|
||||
message: `Failed to load PDF. Error: ${error.message || error}`
|
||||
});
|
||||
const errorMsg = error.message || error.toString();
|
||||
|
||||
if (errorMsg.includes('Failed to read PDF') ||
|
||||
errorMsg.includes('Could not read object') ||
|
||||
errorMsg.includes('No /Root entry') ||
|
||||
errorMsg.includes('PDFError')) {
|
||||
self.postMessage({
|
||||
status: 'error',
|
||||
message: 'The PDF file has structural issues and cannot be processed. The file may be corrupted, incomplete, or created with non-standard tools. Please try:\n\n• Opening and re-saving the PDF in another PDF viewer\n• Using a different PDF file\n• Repairing the PDF with a PDF repair tool'
|
||||
});
|
||||
} else {
|
||||
self.postMessage({
|
||||
status: 'error',
|
||||
message: `Failed to load PDF: ${errorMsg}`
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Add each attachment to the PDF
|
||||
const totalPages = coherentpdf.pages(pdf);
|
||||
|
||||
let targetPages = [];
|
||||
if (attachmentLevel === 'page') {
|
||||
if (!pageRange) {
|
||||
self.postMessage({
|
||||
status: 'error',
|
||||
message: 'Page range is required for page-level attachments.'
|
||||
});
|
||||
coherentpdf.deletePdf(pdf);
|
||||
return;
|
||||
}
|
||||
targetPages = parsePageRange(pageRange, totalPages);
|
||||
if (targetPages.length === 0) {
|
||||
self.postMessage({
|
||||
status: 'error',
|
||||
message: 'Invalid page range specified.'
|
||||
});
|
||||
coherentpdf.deletePdf(pdf);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < attachmentBuffers.length; i++) {
|
||||
try {
|
||||
const attachmentData = new Uint8Array(attachmentBuffers[i]);
|
||||
const attachmentName = attachmentNames[i];
|
||||
|
||||
// Attach file at document level (page 0)
|
||||
coherentpdf.attachFileFromMemory(attachmentData, attachmentName, pdf);
|
||||
|
||||
if (attachmentLevel === 'document') {
|
||||
coherentpdf.attachFileFromMemory(attachmentData, attachmentName, pdf);
|
||||
} else {
|
||||
for (const pageNum of targetPages) {
|
||||
coherentpdf.attachFileToPageFromMemory(attachmentData, attachmentName, pdf, pageNum);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn(`Failed to attach file ${attachmentNames[i]}:`, error);
|
||||
self.postMessage({
|
||||
@@ -33,13 +94,13 @@ function addAttachmentsToPDFInWorker(pdfBuffer, attachmentBuffers, attachmentNam
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Save the modified PDF
|
||||
const modifiedBytes = coherentpdf.toMemory(pdf, false, false);
|
||||
coherentpdf.deletePdf(pdf);
|
||||
|
||||
const buffer = modifiedBytes.buffer.slice(
|
||||
modifiedBytes.byteOffset,
|
||||
modifiedBytes.byteOffset,
|
||||
modifiedBytes.byteOffset + modifiedBytes.byteLength
|
||||
);
|
||||
|
||||
@@ -47,7 +108,7 @@ function addAttachmentsToPDFInWorker(pdfBuffer, attachmentBuffers, attachmentNam
|
||||
status: 'success',
|
||||
modifiedPDF: buffer
|
||||
}, [buffer]);
|
||||
|
||||
|
||||
} catch (error) {
|
||||
self.postMessage({
|
||||
status: 'error',
|
||||
@@ -61,9 +122,11 @@ function addAttachmentsToPDFInWorker(pdfBuffer, attachmentBuffers, attachmentNam
|
||||
self.onmessage = (e) => {
|
||||
if (e.data.command === 'add-attachments') {
|
||||
addAttachmentsToPDFInWorker(
|
||||
e.data.pdfBuffer,
|
||||
e.data.attachmentBuffers,
|
||||
e.data.attachmentNames
|
||||
e.data.pdfBuffer,
|
||||
e.data.attachmentBuffers,
|
||||
e.data.attachmentNames,
|
||||
e.data.attachmentLevel || 'document',
|
||||
e.data.pageRange || ''
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user