question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem with Screen resolution and zoom in browser when generating pdf file from HTML with JSPDF and Html2Canvas

See original GitHub issue

I’m trying to create a script allowing to create a pdf file from an HTML page. I’m using the library jspdf, html2canvas and jquery. It’s working correctly when the zoom in the browser is 100% but I have issues when the screen resolution is changed or if the zoom in the browser is changed.

    			function generatePDF() {				
    			
    				html2canvas(document.getElementById('content'), { scale: 1, width: 2000, height: 9000 }).then(canvas => {
    					var imgData = canvas.toDataURL("image/jpeg");
    					var pdf = new jsPDF('p', 'mm', [520, 1080]);
    					pdf.addImage(imgData, 'JPEG', 10, 10);
    					
    					pdf.save("Test.pdf");
    				});
    						
    			}

I tried several updates (margin, canvas, …)but I don’t find really a solution for that.

Could you please help me with that ?

Thanks in advance for your help.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:25 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
amadesecommented, Jul 31, 2020

Hi,

It’s me again. I’m using this version of jspdf: jspdf.pr2665.js (see the example on http://weihuiguo.com/) for getting a first example for creating the pdf file.

My code there with the updates from this post:

<script src="jspdf.pr2665.js"></script>
<script src="jquery-2.1.4.min.js"></script>
<script src="html2canvas.min.js"></script>
<!-- html2canvas 1.0.0-alpha.11 or higher version ihttps://html2canvas.hertzen.com/dist/html2canvas.min.jss needed -->
<script>
    function download() {
        let pdf = new jsPDF('p', 'pt', 'a4');
        let srcwidth = document.getElementById('toPDF').scrollWidth;

        pdf.html(document.getElementById('toPDF'), {
            html2canvas: {
				scale: 595.26 / srcwidth, //595.26 is the width of A4 page
				scrollY: 0
            },
            filename: 'jspdf',
            x: 0,
            y: 0,
            callback: function () {
                window.open(pdf.output('bloburl'));
            }
        });

    }
</script>

It’s working correctly after applying your comment from this page.

If I replace jspdf.pr2665.js by jspdf.js from the folder src on the master branch, the code is not working (the error “pdf.html is not a function”, could you help me please ? I don’t know what is the problem and how to solve that. Regards,

1reaction
amadesecommented, Jul 23, 2020

The reason the content is not in the center is that your scale is too small. Try scale: 595.28 / srcwidth, instead of 500. Note that 595.26 is the width of A4, which is your pdf page size.

Thank you very much it’s working now 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem with Screen resolution and zoom in browser when ...
I'm trying to create a script allowing to create a pdf file from an HTML page. I'm using the library jspdf, html2canvas and...
Read more >
Using jspdf to save html page as pdf, saved pdf contains ...
Here is how I managed to get the full page pdf while the page is zoomed in using jsPDF's new .html() method. First,...
Read more >
html2pdf.js | Client-side HTML-to-PDF rendering using pure JS.
html2pdf.js converts any webpage or element into a printable PDF entirely client-side using html2canvas and jsPDF. :warning: There have been several issues ......
Read more >
Angular 13 jsPDF & Html2Canvas Project to Export ... - YouTube
Angular 13 jsPDF & Html2Canvas Project to Export HTML Canvas With Background Image With Text & CSS.
Read more >
Print to scale example - OpenLayers
Example of printing a map to a specified scale. The print is exported as a PDF using the jsPDF library. Unlike the Export...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found