jsPDF icon indicating copy to clipboard operation
jsPDF copied to clipboard

Lower screenshot quality with copy&paste canvas to PDF

Open Paul-Kijtapart opened this issue 8 years ago • 2 comments

Thank you for submitting an issue to jsPDF. Please read carefully.

Are you using the latest version of jsPDF? Yes ("jspdf": "^1.3.5")

Have you tried using jspdf.debug.js? Yes

Steps to reproduce

  1. Use html2canvas to take a screenshot of a SVGDashboard
html2canvas(SVGDashboard).then(canvas => {
           // add canvas to browser to see its quality
           body.appendChild(canvas); 

          // do step 2...
});

Note: I find the quality of canvas is pretty good (Hardly see any blur)

  1. Copy-Paste the Canvas from step 1 to PDF
 const pdf = new jsPDF({

                        orientation: canvas.width >= canvas.height ? 'landscape' : 'portrait',
                        unit: 'px',
                        format: [canvas.width, canvas.height]
                    });


                    const imgData = canvas.toDataURL('image/png');
                    pdf.addImage(
                        imgData,
                        'PNG',
                        0,
                        0,
                        canvas.width, canvas.height);

                    pdf.save('test.pdf');
  1. the dashboard is more blurred compared to the canvas.

What I saw Even thought html2canvas is able to generate a high-quality canvas from a SVG dashboard, jsPDF does not maintain the quality of the canvas.

What I expected Since the canvas returned from html2canvas does not get re-sized, I expect the quality of the canvas to remain the same on the PDF generated by jsPDF.

Possible solutions

  1. scale up the size of the SVGDashboard by 2 or more before taking screenshot with html2canvas
  2. As a result, I will end up with a bigger canvas. As the canvas gets scaled down with pdf.addImage(). its quality should improve.

I am wondering if jsPDF provides other ways to improve the quality of the canvas when pdf.addImage is called to place the image on PDF.

Any help is appreciated.

Paul-Kijtapart avatar Mar 13 '18 22:03 Paul-Kijtapart

Problems I found the this issues occurs due to limiting the number of pixels for the PDF with "format: [canvas.width, canvas.height]".

Solution While maintaining the aspect ratio of the paper, scales up the resolution by 3 times. After I use "scale: 3" on the html2canvas before passing the canvas to jsPDF, the final PDF looks as clear as the canvas version.

Sidenote it is recommended to use "FAST" as compress for addImage for small file size

Paul-Kijtapart avatar Mar 16 '18 23:03 Paul-Kijtapart

I've made an example here. But it does not work in Internet Explorer: any hints?

caneta avatar Dec 11 '18 11:12 caneta