javascript - 如何将div导出为PDF?

标签 javascript html

我正在尝试将 div 导出为 PDF 格式。

当我单击该按钮时,会打开一个页面以将页面保存为 PDF 或打印。

这个页面应该显示div的内容,但是出现的是[object HTMLDivElement]:

Image with Problem

HTML:

<div id="PDF" class="tab-pane fade in active"> 
    <div class="table-responsive panel">
       <table class="table">
          <tbody>
              <tr>
                  <td class="text-success"><i class="fa fa-user"></i> Name</td>
                  <td>@Model.Name</td>
              </tr>
              <tr>
                  <td class="text-success"><i class="fa fa-home"></i> Adress</td>
                  <td>@Model.Adress</td>
              </tr>
           </tbody>
        </table>
    </div>
</div>
<button type="button" class="btn btn-primary btn-circle btn-xl" id="btnPrint" data-toggle="tooltip" title="Export"> </button>

JavaScript:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $("#btnPrint").live("click", function () {
        var divContents = $("#PDF").html();
        var printWindow = window.open('', '', 'height=400,width=800');
        printWindow.document.write('<html><head><title>PDF</title>');
        printWindow.document.write('</head><body >');
        printWindow.document.write(PDF);
        printWindow.document.write('</body></html>');
        printWindow.document.close();
        printWindow.print();
    });
</script>

如何解决这个问题?

最佳答案

通过替换以下行来更新您的代码

var printWindow = window.open('', '_self', 'height=400,width=800');
printWindow.document.write(divContents);

关于javascript - 如何将div导出为PDF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51771634/

相关文章:

javascript - 如何在 angularJs 中将参数传递给工厂?

javascript - 如何在 javascript 中正确使用 <a> 标签,但没有 href 链接

javascript - React-Native 发送html格式邮件

html - 虚线顶部和底部边框比文本短

jquery - 找到子图像宽度并将其应用于包含父div jquery

javascript - 对 Nodejs 中发出的事件进行单元测试的最佳方法是什么?

javascript - Angularjs 中 ng-view 位置更改后突出显示文本

javascript - POST 字段被 Chrome chop

javascript - 具有简单可变高度的 Div

php - 在 PHP 中分解字符串