2025-12-04 23:53:00 +05:30
self . importScripts ( '../coherentpdf.browser.min.js' ) ;
2025-11-18 20:31:13 +05:30
2025-11-19 13:19:14 +05:30
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 ) {
2025-11-18 20:31:13 +05:30
try {
const uint8Array = new Uint8Array ( pdfBuffer ) ;
2025-11-19 13:19:14 +05:30
2025-11-18 20:31:13 +05:30
let pdf ;
try {
pdf = coherentpdf . fromMemory ( uint8Array , '' ) ;
} catch ( error ) {
2025-11-19 13:19:14 +05:30
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 } `
} ) ;
}
2025-11-18 20:31:13 +05:30
return ;
}
2025-11-19 13:19:14 +05:30
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 ;
}
}
2025-11-18 20:31:13 +05:30
for ( let i = 0 ; i < attachmentBuffers . length ; i ++ ) {
try {
const attachmentData = new Uint8Array ( attachmentBuffers [ i ] ) ;
const attachmentName = attachmentNames [ i ] ;
2025-11-19 13:19:14 +05:30
if ( attachmentLevel === 'document' ) {
coherentpdf . attachFileFromMemory ( attachmentData , attachmentName , pdf ) ;
} else {
for ( const pageNum of targetPages ) {
coherentpdf . attachFileToPageFromMemory ( attachmentData , attachmentName , pdf , pageNum ) ;
}
}
2025-11-18 20:31:13 +05:30
} catch ( error ) {
console . warn ( ` Failed to attach file ${ attachmentNames [ i ] } : ` , error ) ;
self . postMessage ( {
status : 'error' ,
message : ` Failed to attach file ${ attachmentNames [ i ] } : ${ error . message || error } `
} ) ;
coherentpdf . deletePdf ( pdf ) ;
return ;
}
}
2025-11-19 13:19:14 +05:30
2025-11-18 20:31:13 +05:30
// Save the modified PDF
const modifiedBytes = coherentpdf . toMemory ( pdf , false , false ) ;
coherentpdf . deletePdf ( pdf ) ;
const buffer = modifiedBytes . buffer . slice (
2025-11-19 13:19:14 +05:30
modifiedBytes . byteOffset ,
2025-11-18 20:31:13 +05:30
modifiedBytes . byteOffset + modifiedBytes . byteLength
) ;
self . postMessage ( {
status : 'success' ,
modifiedPDF : buffer
} , [ buffer ] ) ;
2025-11-19 13:19:14 +05:30
2025-11-18 20:31:13 +05:30
} catch ( error ) {
self . postMessage ( {
status : 'error' ,
message : error instanceof Error
? error . message
: 'Unknown error occurred while adding attachments.'
} ) ;
}
}
self . onmessage = ( e ) => {
if ( e . data . command === 'add-attachments' ) {
addAttachmentsToPDFInWorker (
2025-11-19 13:19:14 +05:30
e . data . pdfBuffer ,
e . data . attachmentBuffers ,
e . data . attachmentNames ,
e . data . attachmentLevel || 'document' ,
e . data . pageRange || ''
2025-11-18 20:31:13 +05:30
) ;
}
} ;