From 4a800520de384733acf48f7d86bf8fd7d6548309 Mon Sep 17 00:00:00 2001 From: N-Shar-ma Date: Sun, 30 May 2021 23:44:50 +0530 Subject: [PATCH 1/2] Fixed Image to PDF conversion, Resolution and Dimensions preserved --- src/pages/MediaManip/MediaManip.jsx | 34 +++++++++++++++-------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/pages/MediaManip/MediaManip.jsx b/src/pages/MediaManip/MediaManip.jsx index 3d57c40d..6287baa9 100644 --- a/src/pages/MediaManip/MediaManip.jsx +++ b/src/pages/MediaManip/MediaManip.jsx @@ -80,8 +80,10 @@ export default function MediaManip() { canvas.height = img.height; canvas.getContext("2d").drawImage(img, 0, 0); const dataUrl = canvas.toDataURL(`image/${format!=="pdf" ? format : "png"}`); + const width = img.width; + const height = img.height; if(format==="pdf") { - dataUrls.push(dataUrl); + dataUrls.push({ dataUrl, width, height }); if(dataUrls.length===files.length) { downloadPdf(dataUrls); return; @@ -99,24 +101,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); + // const pageWidth = doc.internal.pageSize.width; + // const pageHeight = doc.internal.pageSize.height; doc.text(10, 20, ""); - images.forEach((imgDataUri, i) => { - if(i>0) { - doc.addPage(); + images.forEach(({ dataUrl, width, height }, i) => { + // if(i>0) { + doc.addPage([width, height]); doc.setPage(i+1); - } - doc.addImage(imgDataUri, "PNG", 0, 0, width, height); - }); + // } + // if(width<=pageWidth && height<=pageHeight) - await new Promise((resolve) => { - // Wait for PDF download - doc.save("document.pdf"); //save PDF - resolve(true); - setConvertedFiles([]); + doc.addImage(dataUrl, "PNG", 0, 0, width, height); + if(i===images.length-1) { + doc.save("converted.pdf"); //save PDF + setConvertedFiles([]); + } }); }; From 496a65aa9ae68d2170a0d1d6f97ef955cc431939 Mon Sep 17 00:00:00 2001 From: N-Shar-ma Date: Mon, 31 May 2021 10:20:26 +0530 Subject: [PATCH 2/2] Removed unnecessary commennts --- src/pages/MediaManip/MediaManip.jsx | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/pages/MediaManip/MediaManip.jsx b/src/pages/MediaManip/MediaManip.jsx index 6287baa9..32623813 100644 --- a/src/pages/MediaManip/MediaManip.jsx +++ b/src/pages/MediaManip/MediaManip.jsx @@ -104,16 +104,10 @@ export default function MediaManip() { const downloadPdf = images => { const doc = new jsPDF("l", "px", [images[0].width, images[0].height]); doc.deletePage(1); - // const pageWidth = doc.internal.pageSize.width; - // const pageHeight = doc.internal.pageSize.height; doc.text(10, 20, ""); images.forEach(({ dataUrl, width, height }, i) => { - // if(i>0) { - doc.addPage([width, height]); - doc.setPage(i+1); - // } - // if(width<=pageWidth && height<=pageHeight) - + 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