使用html2canvas打印页面

时间:2014-10-27 09:41:44

标签: javascript html html2canvas

我正在使用html2canvas在我的网站上构建打印页面功能。

function printthispage() {

    html2canvas($("#mydiv"), {
        onrendered: function (canvas) {
            var myImage = canvas.toDataURL("image/png");
            var printWindow = window.open(myImage);                        
            printWindow.document.close();
            printWindow.focus();
            printWindow.print();
            printWindow.close();
        }
    });
}

然而,窗户立即打开和关闭。我尝试删除close(),图片显示在新窗口中但未触发打印功能。有什么不对吗?

6 个答案:

答案 0 :(得分:4)

试试这个,它会起作用:

html2canvas($("#mydiv"), {
    onrendered: function(canvas) {
        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();
            });
    }
});

答案 1 :(得分:2)

Vanilla JS解决方案

// render HTML to canvas based on target element
html2canvas(document.querySelector('<YOUR SELECTOR>'), {

  // if the canvas is rendered
  onrendered: function (canvas) {

    // create a new window
    var nWindow = window.open('');

    // append the canvas to the body
    nWindow.document.body.appendChild(canvas);

    // focus on the window
    nWindow.focus();

    // print the window
    nWindow.print();

    // reload the page
    location.reload();
  }
});

答案 2 :(得分:1)

最后我找出了解决方案。我之前使用的处理应分为两部分。

1)使用html2canvas将页面呈现为图像,并在加载页面时将其存储在隐藏的div中。

html2canvas(divprint, {
    onrendered: function(canvas) {
        var canvasImg = canvas.toDataURL("image/jpg");
        $('#divhidden').html('<img src="'+canvasImg+'" alt="">');
    }
});

2)单击打印按钮后,打开一个新窗口,编写存储的div内容和jquery函数,以便在加载完成后进行打印。

$("#printbutton").click(function(e){
    var printContent = document.getElementById("divhidden");
    var printWindow = window.open("", "","left=50,top=50");                
    printWindow.document.write(printContent.innerHTML);
    printWindow.document.write("<script src=\'http://code.jquery.com/jquery-1.10.1.min.js\'><\/script>");
    printWindow.document.write("<script>$(window).load(function(){ print(); close(); });<\/script>");
    printWindow.document.close();                
})

答案 3 :(得分:1)

我完成了这个

function myprint() {
    html2canvas(jQuery('div.cart'), { // replace div.cart with your selector
        onrendered: function (canvas) {
            var myImage = canvas.toDataURL("image/png");
            var tmp = document.body.innerHTML;

            document.body.innerHTML = '<img src="'+myImage+'" alt="">';

            var printWindow = window.print();
            document.body.innerHTML = tmp;
        }
    });
}

答案 4 :(得分:0)

 try this code, it is working.                                      

<div id="mydiv">asdasfadsadfasfd</div>
    <script>
        html2canvas($("#mydiv"), {
            onrendered: function (canvas) {
               var myImage = canvas.toDataURL("image/png");
               var printWindow = window.print(myImage);                                          

            }
        });
    </script>

答案 5 :(得分:0)

html2canvas(document.querySelector("#mydiv")).then(canvas => {
            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();
                });
    });