diff --git a/src/js/logic/pdf-to-webp.ts b/src/js/logic/pdf-to-webp.ts index b3c8667..0e3d060 100644 --- a/src/js/logic/pdf-to-webp.ts +++ b/src/js/logic/pdf-to-webp.ts @@ -7,25 +7,35 @@ 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 pdfToWebp() { showLoader('Converting to WebP...'); + await yieldToUI(); try { const pdf = await getPDFDocument( await readFileAsArrayBuffer(state.files[0]) ).promise; if(pdf.numPages === 1) { + showLoader(`Processing the single page...`); + await yieldToUI(); const page = await pdf.getPage(1); const blob = await pageToBlob(page); downloadFile(blob, getCleanFilename() + '.webp'); } 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); zip.file(`page_${i}.webp`, blob as Blob); } + + showLoader('Compressing files into a ZIP...'); + await yieldToUI(); const zipBlob = await zip.generateAsync({ type: 'blob' }); downloadFile(zipBlob, getCleanFilename() + '_webps.zip'); }