diff --git a/src/pages/MediaManip/MediaManip.jsx b/src/pages/MediaManip/MediaManip.jsx index 27f62532..bbc3957b 100644 --- a/src/pages/MediaManip/MediaManip.jsx +++ b/src/pages/MediaManip/MediaManip.jsx @@ -88,11 +88,13 @@ export default function MediaManip() { canvas.height = img.height; canvas.getContext("2d").drawImage(img, 0, 0); const dataUrl = canvas.toDataURL( - `image/${format !== "pdf" ? format : "png"}` + `image/${format!=="pdf" ? format : "png"}` ); - if (format === "pdf") { - dataUrls.push(dataUrl); - if (dataUrls.length === files.length) { + const width = img.width; + const height = img.height; + if(format === "pdf") { + dataUrls.push({ dataUrl, width, height }); + if(dataUrls.length === files.length) { downloadPdf(dataUrls); return; } @@ -108,30 +110,24 @@ export default function MediaManip() { }); }); }; - - const downloadPdf = async (images) => { - const doc = new jsPDF("p", "pt", "a4"); - const width = doc.internal.pageSize.width; - const height = doc.internal.pageSize.height; + + const downloadPdf = (images) => { + const doc = new jsPDF("l", "px", [images[0].width, images[0].height]); + doc.deletePage(1); doc.text(10, 20, ""); - images.forEach((imgDataUri, i) => { - if (i > 0) { - doc.addPage(); - doc.setPage(i + 1); + images.forEach(({ dataUrl, width, height }, i) => { + doc.addPage([width, height]); + doc.setPage(i + 1); + doc.addImage(dataUrl, "PNG", 0, 0, width, height); + if(i === images.length - 1) { + doc.save("converted.pdf"); //save PDF + setConvertedFiles([]); } - doc.addImage(imgDataUri, "PNG", 0, 0, width, height); - }); - - await new Promise((resolve) => { - // Wait for PDF download - doc.save("document.pdf"); //save PDF - resolve(true); - setConvertedFiles([]); }); }; - + const onDownload = () => { - if (convertedFiles.length === 0) return; + if(convertedFiles.length === 0) return; convertedFiles.forEach((item, index) => { zip.file(`${index}.${item.type}`, item.data, { base64: true }); });