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.

html2canvas only showing viewport. not printing complete DIV

See original GitHub issue

I m using html2canvas along with jsPDF to create a pdf. Unfortunately documentation of both is not enough and i am banging my head. I want to create a pdf having multipe div in a single huge DIV. what i mean, is i have many divs inside a div which i want to print.

Problem:

PDF is only generating the viewport screenshot. i want to ask if there is an possible solution?

this is my function call

`function generate_pdf(){ var quotes = document.getElementById(‘blank_summary_table’);

html2canvas(quotes, {
    onrendered: function(canvas) {

        //! MAKE YOUR PDF
        var pdf = new jsPDF('p', 'pt', 'letter');

        for (var i = 0; i <= quotes.clientHeight/980; i++) {
            //! This is all just html2canvas stuff
            var srcImg  = canvas;
            var sX      = 0;
            var sY      = 980*i; // start 980 pixels down for every new page
            var sWidth  = 900;
            var sHeight = 980;
            var dX      = 0;
            var dY      = 0;
            var dWidth  = 900;
            var dHeight = 980;

            window.onePageCanvas = document.createElement("canvas");
            onePageCanvas.setAttribute('width', 900);
            onePageCanvas.setAttribute('height', 980);
            var ctx = onePageCanvas.getContext('2d');
            // details on this usage of this function:
            // https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Using_images#Slicing
            ctx.drawImage(srcImg,sX,sY,sWidth,sHeight,dX,dY,dWidth,dHeight);

            // document.body.appendChild(canvas);
            var canvasDataURL = onePageCanvas.toDataURL("image/png", 1.0);

            var width         = onePageCanvas.width;
            var height        = onePageCanvas.clientHeight;

            //! If we're on anything other than the first page,
            // add another page
            if (i > 0) {
                pdf.addPage(612, 791); //8.5" x 11" in pts (in*72)
            }
            //! now we declare that we're working on that page
            pdf.setPage(i+1);
            //! now we add content to that page!
            pdf.addImage(canvasDataURL, 'PNG', 20, 40, (width*.62), (height*.62));

        }
        //! after the for loop is finished running, we save the pdf.
        pdf.save('Test.pdf');
    }
});

}`

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
lennucommented, Apr 12, 2017

I haven’t really found a great solution to do webpage to pdf conversion on the frontend. The combination of jsPDF and html2canvas is a good one, if someone can get it working.

This eKoopmans, who has made the pull request to fix html2canvas is working on a solution that uses html2canvas and jspdf: https://github.com/eKoopmans/html2pdf

2reactions
lennucommented, Apr 11, 2017

This is an error in html2canvas, and there is a pull request open to fix the problem on html2canvas repository: https://github.com/niklasvh/html2canvas/pull/1083

You can use the branch of the repository with npm: npm install --save git+https://github.com/eKoopmans/html2canvas.git#develop or just download it from the github page.

Read more comments on GitHub >

github_iconTop Results From Across the Web

html2canvas only showing viewport. not printing complete ...
I m using html2canvas along with jsPDF to create a pdf. Unfortunately documentation of both is not enough and i am banging my...
Read more >
HTML2Canvas does not render full div, only what is visible ...
When this code runs, it only renders what is visible on the screen. The #potenzial-page div is essentially the entire page, minus the...
Read more >
Options | html2canvas
Name Default Description logging true Enable logging for debug purposes width Element width The width of the canvas height Element height The height of the canvas...
Read more >
HTML2Canvas does not render full div, only what is visible on ...
JavaScript : HTML2Canvas does not render full div, only what is visible on screen? [ Gift : Animated Search Engine ...
Read more >
HTML5 Canvas Export to High Quality Image Tutorial
Hi im having trouble with my canvas not saving properly, I am displaying pixelated images on the canvas and using the imageSmoothingEnabled(false) property...
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