简体   繁体   English

如何使用html2canvas打印画布

[英]How to print a canvas using html2canvas

I am trying to print a div with an image inside it using html2canvas . 我正在尝试使用html2canvas打印带有图像的div。

function print_cert(){
        var element = jQuery("#cert_viewer")[0];
        html2canvas(element).then(function (canvas) {
            console.log("ready");
            var myImage = canvas.toDataURL("image/png");
            var tWindow = window.open("");
            $(tWindow.document.body).html("<img id='Image' src=" + myImage + " style='width:100%;'></img>").ready(function() {
            tWindow.focus();
            tWindow.print();
            });
        });
    }

When I try to print the canvas, the image inside it is not included in the print page 当我尝试打印画布时,其中的图像不包含在打印页面中

EDIT: 编辑:

The div which I need to get printed: 我需要打印的div:

<div id = "cert_viewer" class = "style_view" style="padding-top: 11px; margin-top: 36px;">

            <p id = "matter1"></p>
            <p id = "matter2"></p>

            <img id="qrcode">
</div>

Depending on which version you've used the onrendered method was deprecated "Remove deprecated onrendered callback, calling html2canvas returns a Promise" https://github.com/niklasvh/html2canvas/blob/9a6e57aa00e422bf22482dbf56de33d5aada3633/CHANGELOG.md 根据您使用哪个版本的onrendered方法,不建议使用“删除不建议使用的onrendered回调,调用html2canvas返回一个Promise” https://github.com/niklasvh/html2canvas/blob/9a6e57aa00e422bf22482dbf56de33d5aada3633/CHANGELOG.md

You can try this version which uses the promises interface. 您可以尝试使用使用promises接口的版本。

function print_cert(){
  var element = jQuery("#cert_viewer")[0];
  html2canvas(element).then(function (canvas) {
    console.log("ready");
    var myImage = canvas.toDataURL("image/png");
    var tWindow = window.open("");
    $(tWindow.document.body).html("<img id='Image' src=" + myImage + " style='width:100%;'></img>").ready(function() {
      tWindow.focus();
      tWindow.print();
    });
  });
}

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

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