gpt4 book ai didi

javascript - 如何将div导出为PDF?

转载 作者:行者123 更新时间:2023-12-03 01:20:53 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com