feat(pdf-to-jpg): show processing in loader message

This commit is contained in:
Sebastian Espei
2025-12-06 00:26:14 +01:00
parent 650ff1fca5
commit ac32e09fe9

View File

@@ -7,9 +7,11 @@ import { PDFPageProxy } from 'pdfjs-dist';
pdfjsLib.GlobalWorkerOptions.workerSrc = new URL('pdfjs-dist/build/pdf.worker.min.mjs', import.meta.url).toString();
const yieldToUI = () => new Promise((r) => setTimeout(r, 0));
export async function pdfToJpg() {
showLoader('Converting to JPG...');
await yieldToUI();
try {
const pdf = await getPDFDocument(
await readFileAsArrayBuffer(state.files[0])
@@ -19,17 +21,23 @@ export async function pdfToJpg() {
const quality = qualityInput ? parseFloat(qualityInput.value) : 0.9;
if(pdf.numPages === 1) {
showLoader(`Processing the single page...`);
await yieldToUI();
const page = await pdf.getPage(1);
const blob = await pageToBlob(page, quality);
downloadFile(blob, getCleanFilename() + '.jpg');
} else {
const zip = new JSZip();
for (let i = 1; i <= pdf.numPages; i++) {
showLoader(`Processing page ${i} of ${pdf.numPages}...`);
await yieldToUI();
const page = await pdf.getPage(i);
const blob = await pageToBlob(page, quality);
zip.file(`page_${i}.jpg`, blob as Blob);
}
showLoader('Compressing files into a ZIP...');
await yieldToUI();
const zipBlob = await zip.generateAsync({ type: 'blob' });
downloadFile(zipBlob, getCleanFilename() + '_jpgs.zip');
}