如何将div导出为PDF?

问题描述 投票:0回答:2

我正在尝试将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>

如何解决这个问题?

javascript html
2个回答
1
投票

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

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

0
投票
var divContents = $("#PDF").html();

...

printWindow.document.write(PDF);

我相信您要保存要打印的内容,但不要使用它:

printWindow.document.write(divContents);
© www.soinside.com 2019 - 2024. All rights reserved.