- Introduced new pages and logic for converting JSON files to PDF and vice versa. - Implemented web workers for handling conversion processes in the background. - Updated Vite configuration to include new HTML pages for the conversion tools. - Enhanced user interface with file upload sections and status messages for conversion progress. - Added TypeScript definitions for worker communication and response handling.
52 lines
1.3 KiB
JavaScript
52 lines
1.3 KiB
JavaScript
self.importScripts('/coherentpdf.browser.min.js');
|
|
|
|
function convertPDFsToJSONInWorker(fileBuffers, fileNames) {
|
|
try {
|
|
const jsonFiles = [];
|
|
const transferBuffers = [];
|
|
|
|
for (let i = 0; i < fileBuffers.length; i++) {
|
|
const buffer = fileBuffers[i];
|
|
const fileName = fileNames[i];
|
|
const uint8Array = new Uint8Array(buffer);
|
|
const pdf = coherentpdf.fromMemory(uint8Array, '');
|
|
|
|
//TODO:@ALAM -> add options for users to select these settings
|
|
// parse_content: true, no_stream_data: false, decompress_streams: false
|
|
const jsonData = coherentpdf.outputJSONMemory(true, false, false, pdf);
|
|
|
|
const jsonBuffer = jsonData.buffer.slice(0);
|
|
jsonFiles.push({
|
|
name: fileName,
|
|
data: jsonBuffer,
|
|
});
|
|
transferBuffers.push(jsonBuffer);
|
|
|
|
coherentpdf.deletePdf(pdf);
|
|
}
|
|
|
|
self.postMessage(
|
|
{
|
|
status: 'success',
|
|
jsonFiles: jsonFiles,
|
|
},
|
|
transferBuffers
|
|
);
|
|
} catch (error) {
|
|
self.postMessage({
|
|
status: 'error',
|
|
message:
|
|
error instanceof Error
|
|
? error.message
|
|
: 'Unknown error during PDF to JSON conversion.',
|
|
});
|
|
}
|
|
}
|
|
|
|
self.onmessage = (e) => {
|
|
if (e.data.command === 'convert') {
|
|
convertPDFsToJSONInWorker(e.data.fileBuffers, e.data.fileNames);
|
|
}
|
|
};
|
|
|