【问题标题】:How To Convert HTML to PDF using JavaScript如何使用 JavaScript 将 HTML 转换为 PDF
【发布时间】:2022-12-01 16:01:43
【问题描述】:

我想通过单击按钮将 HTML 转换为 PDF 并下载

HTML

<div id="pageprint">
   <div id="reportbox">Hello World!!</div>
</div> 
<button type="button" onclick="downloadCode();">Download HTML</button>

Javascript

<script>
    function generatePDF() {
    
    const element = document.getElementById("pageprint");
    document.getElementById("reportbox").style.display = "block";
    document.getElementById("reportbox").style.marginTop = "0px"; 
    document.getElementById("pageprint").style.border = "1px solid black";
    html2pdf().from(element).save('download.pdf'); 
    }
    
    function downloadCode(){
    var x = document.getElementById("reportbox");  
    generatePDF();
    setTimeout(function() { window.location=window.location;},3000);}
</script>

【问题讨论】:

    标签: javascript html jquery html-to-pdf


    【解决方案1】:

    您可以按照以下方式打印 html。

    <style type="text/css">
        @media print{ button {display:none} };
    </style>
    
    <div id="pageprint">
       <div id="reportbox">Hello World!!</div>
    </div> 
    <button type="button" onclick=javascript:window.print()>Download HTML</button>
    

    如果发现任何问题,请告诉我

    【讨论】:

      猜你喜欢
      • 2017-01-23
      • 1970-01-01
      • 2019-05-11
      • 1970-01-01
      • 1970-01-01
      • 2014-11-23
      • 2014-11-01
      • 2018-09-14
      • 1970-01-01
      相关资源
      最近更新 更多