繁体   English   中英

打印表格而不预览

[英]print table without preview

你好朋友我正在使用 vue.js 创建一个表,我想要一个按钮来打印表中的数据而没有预览对话框那么我应该在 vue.js 中编写我的 javasript 代码吗? 这是我的桌子: 在此处输入图片说明

这是我的打印按钮代码

 Print: function(){
  var mycocument=document.getElementById('table_users');
  var newWin=window.open("","");
  newWin.document.open();
  newWin.document.write(mycocument.outerHTML);
  newWin.print();
  newWin.close();
}

选项1
你可以尝试这样做。

function printDiv() { 
            var divContents = document.getElementById("table_users").innerHTML; 
            var a = window.open('', '', 'height=500, width=500'); 
            a.document.write('<html>'); 
            a.document.write('<body > <h1>This is a sample print</h1> <br>'); 
            a.document.write(divContents); 
            a.document.write('</body></html>'); 
            a.document.close(); 
            a.print(); 
        } 

你可以检查这个例子https://codepen.io/misterjenuel/pen/OJPzqVL

选项 2
您可以使用https://github.com/MrRio/jsPDF 来完成

*****html*****

<div id="content">
     <h3>Hello, this is a H3 tag</h3>
     <table border="1px" style="border-collapse: collapse;"> 
            <tr> 
                <td>computer</td> 
                <td>Algorithm</td> 
            </tr> 
            <tr> 
                <td>Microwave</td> 
                <td>Infrared</td> 
            </tr> 
        </table> 
</div>
<div id="editor"></div>
<button id="cmd">generate PDF</button>

*****JavaScript:*****

var doc = new jsPDF();
var specialElementHandlers = {
    '#editor': function (element, renderer) {
        return true;
    }
};

$('#cmd').click(function () {
    doc.fromHTML($('#content').html(), 15, 15, {
        'width': 170,
            'elementHandlers': specialElementHandlers
    });
    doc.save('sample-file.pdf');
});

选项 3
.. 中的内容可以使用 jspdf 和 html2canvas 以 pdf 格式下载。

你需要参考两个js库,

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"></script>
<script type="text/javascript" src="https://html2canvas.hertzen.com/dist/html2canvas.js"></script>

然后调用下面的函数,

//Create PDf from HTML...
function CreatePDFfromHTML() {
    var HTML_Width = $(".html-content").width();
    var HTML_Height = $(".html-content").height();
    var top_left_margin = 15;
    var PDF_Width = HTML_Width + (top_left_margin * 2);
    var PDF_Height = (PDF_Width * 1.5) + (top_left_margin * 2);
    var canvas_image_width = HTML_Width;
    var canvas_image_height = HTML_Height;

    var totalPDFPages = Math.ceil(HTML_Height / PDF_Height) - 1;

    html2canvas($(".html-content")[0]).then(function (canvas) {
        var imgData = canvas.toDataURL("image/jpeg", 1.0);
        var pdf = new jsPDF('p', 'pt', [PDF_Width, PDF_Height]);
        pdf.addImage(imgData, 'JPG', top_left_margin, top_left_margin, canvas_image_width, canvas_image_height);
        for (var i = 1; i <= totalPDFPages; i++) { 
            pdf.addPage(PDF_Width, PDF_Height);
            pdf.addImage(imgData, 'JPG', top_left_margin, -(PDF_Height*i)+(top_left_margin*4),canvas_image_width,canvas_image_height);
        }
        pdf.save("Your_PDF_Name.pdf");
        $(".html-content").hide();
    });
}

参考: https : //www.freakyjolly.com/jspdf-multipage-example-generate-multipage-pdf-using-single-canvas-of-html-document-using-jspdf/

但是,如果您尝试直接打印它:

我遇到了另一个优雅的解决方案:

将您的可打印部分放在一个 div 中,其 id 如下所示:

<div id="printableArea">
      <h1>Print me</h1>
</div>

<input type="button" onclick="printDiv('printableArea')" value="print a div!" />

现在让我们创建一个非常简单的 javascript:

function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}

来源: https : //stackoverflow.com/a/7532581/1223045

暂无
暂无

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

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