【问题标题】:Print page using html2canvas使用 html2canvas 打印页面
【发布时间】:2021-01-03 23:37:33
【问题描述】:

我正在使用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(),图像显示在新窗口中,但没有触发打印功能。有什么问题吗?

【问题讨论】:

  • 似乎没有关闭jsfiddle.net/dsf4wwkL
  • 我发的链接你试过了吗?
  • 是的,但它不适用于我的情况

标签: javascript html html2canvas


【解决方案1】:

试试这个,它会工作:

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();
            });
    }
});

【讨论】:

    【解决方案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();
      }
    });
    

    【讨论】:

      【解决方案3】:

      我终于找到了解决方案。我之前使用的处理应该分成2部分。

      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();                
      })
      

      【讨论】:

        【解决方案4】:

        我做到了

        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;
                }
            });
        }
        

        【讨论】:

          【解决方案5】:
           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>
          

          【讨论】:

          • 上面的代码实际上只是打印现有页面,而不是生成的画布。
          • 你只需要打印图像吗?它存储在myimage中,对吗?
          【解决方案6】:
          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();
                          });
              });
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-01-12
            • 2019-01-20
            • 2011-09-22
            • 2018-09-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-12-11
            相关资源
            最近更新 更多