Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html2canvas screenshot capturing current window, not entire body

Trying to capture a screenshot of the entire body of a page (including the fields filled in by user) in javascript, but html2canvas only captures the current window, even when I set the height to a huge number. The html2canvas website examples appear to have my desired functionality, but I'm not able to understand what they're doing differently.

<button id="pdfbutton" type="button">Click Me!</button>

<script type="text/javascript">
$( "#pdfbutton" ).click(function() {
    html2canvas(document.body, {
        onrendered: function(canvas) {
            // document.body.appendChild(canvas);
            var img = canvas.toDataURL("image/png");
            console.log(img);
            document.body.appendChild(canvas);

        }
        // height: 10000
    });
});
</script>

Please let me know if there are any other ways to capture a screenshot on a button click in javascript (without inserting a URL, because the user fills in fields on my page).

like image 899
user2657795 Avatar asked Apr 25 '15 17:04

user2657795


People also ask

Why is the produced canvas empty or cuts off half way through?

Why is the produced canvas empty or cuts off half way through? Make sure that canvas element doesn't hit browser limitations for the canvas size or use the window configuration options to set a custom window size based on the canvas element: await html2canvas(element, { windowWidth: element.

How to take screenshot using html2canvas?

Create screenshot() function where initialize html2canvas on the body. By default, html2canvas set the image background color to black if save the screenshot. With the use of background: '#fff' set background white. Get taken screenshot image base64 URL using canvas.

How to take screenshot of webpage using JavaScript?

A screenshot of any element in JavaScript can be taken using the html2canvas library. This library can be downloaded from its official website.


1 Answers

Solved this problem by removing style attribute (height, position) which does not support by html2canvas and adding it after capturing the screenshot. In my case I faced problem with position.

  $('.element').css('position','initial'); // Change absolute to initial
  $my_view = $('#my-view');
  var useHeight = $('#my-view').prop('scrollHeight');
  html2canvas($my_view[0], {
    height: useHeight,
    useCORS: true,
    allowTaint: true,
    proxy: "your proxy url",
    onrendered: function (canvas) {
        var imgSrc = canvas.toDataURL();
        var popup = window.open(imgSrc);
        $('.element').css('position','absolute');
    }
  });

This will take screenshot of the whole screen including scrollable part. check this solution

like image 86
Meroshini Avatar answered Oct 23 '22 04:10

Meroshini