feat: add JSON to PDF and PDF to JSON conversion tools
- 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.
This commit is contained in:
51
public/workers/pdf-to-json.worker.js
Normal file
51
public/workers/pdf-to-json.worker.js
Normal file
@@ -0,0 +1,51 @@
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user