refactor(qpdf): move initialization logic to helpers module
Consolidate qpdf initialization code into helpers.ts to avoid duplication Improve error message for password protected PDFs Fortify encryption logic by auto-filling owner password when empty
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
import createModule from '@neslinesli93/qpdf-wasm';
|
||||
import { showLoader, hideLoader, showAlert } from '../ui';
|
||||
|
||||
const STANDARD_SIZES = {
|
||||
A4: { width: 595.28, height: 841.89 },
|
||||
Letter: { width: 612, height: 792 },
|
||||
@@ -146,3 +149,31 @@ export function formatIsoDate(isoDateString) {
|
||||
return isoDateString; // Return original string on any error
|
||||
}
|
||||
}
|
||||
|
||||
let qpdfInstance: any = null;
|
||||
|
||||
/**
|
||||
* Initialize qpdf-wasm singleton.
|
||||
* Subsequent calls return the same instance.
|
||||
*/
|
||||
export async function initializeQpdf() {
|
||||
if (qpdfInstance) return qpdfInstance;
|
||||
|
||||
showLoader('Initializing PDF engine...');
|
||||
try {
|
||||
qpdfInstance = await createModule({
|
||||
locateFile: () => '/qpdf.wasm',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize qpdf-wasm:', error);
|
||||
showAlert(
|
||||
'Initialization Error',
|
||||
'Could not load the PDF engine. Please refresh the page and try again.'
|
||||
);
|
||||
throw error;
|
||||
} finally {
|
||||
hideLoader();
|
||||
}
|
||||
|
||||
return qpdfInstance;
|
||||
}
|
||||
Reference in New Issue
Block a user