简体   繁体   English

html2canvas截图捕捉当前窗口,而不是整个身体

[英]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. 试图在javascript中捕获整个页面的屏幕截图(包括用户填写的字段),但是html2canvas只捕获当前窗口,即使我将高度设置为一个巨大的数字。 The html2canvas website examples appear to have my desired functionality, but I'm not able to understand what they're doing differently. html2canvas网站示例似乎具有我想要的功能,但我无法理解他们在做什么不同。

<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). 如果有任何其他方法可以捕获javascript按钮上的屏幕截图(请不要插入URL,因为用户填写了我的页面上的字段),请告诉我。

Solved this problem by removing style attribute (height, position) which does not support by html2canvas and adding it after capturing the screenshot. 通过删除html2canvas不支持的样式属性(高度,位置)并在捕获屏幕截图后添加它来解决此问题。 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 检查这个解决方案

Html2Canvas has an issue which forces it to render an element from the top window's boundary. Html2Canvas有一个问题,迫使它从顶部窗口的边界渲染一个元素。 You may consider scrolling to the top of the element. 您可以考虑滚动到元素的顶部。

The second issue is overflow handling. 第二个问题是溢出处理。 Set overflow: visible on the parent element and remove it after export. 设置overflow: visible在父元素上overflow: visible ,并在导出后将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM