gpt4 book ai didi

javascript - 如何使用 jspdf.js 正确打印 HTML 表格和 PDF?

转载 作者:行者123 更新时间:2023-11-30 11:56:00 25 4
gpt4 key购买 nike

这就是我想要实现的目标,我试图打印一个包含 HTML 表格的 html 表格,也循环我的详细信息(我有标题和详细信息)数据。但是当我使用 jspdf.js 打印我的 HTML 表格时,pdf 中的表格已损坏,看起来不像 HTML,主表格内的循环表格很乱,看起来它不会创建内部表格。如何正确打印表内表?

这是我的 HTML 样子

index.html

    <div id="customers">
<div class="table-responsive">
<table id="tbl" class="table table-hover">
<thead>
<tr>
<th style="background-color: #928989; color; white;">No BPS</th>
<th style="background-color: #928989; color; white;">Tanggal BPS</th>
<th style="background-color: #928989; color; white;">Tanggal Input</th>
<th style="background-color: #928989; color; white;">Status</th>
</tr>
</thead>
<tbody>
<tr ng-repeat-start="listLaporanRetur in listLaporanReturs | limitTo:quantity">
<td class="btn btn-mini btn-primary pull-center">BPXXXXXXX</td>
<td>2016-06-22</td>
<td>2016-06-22</td>
<td>SENT</td>
</tr>
<tr ng-repeat-end="">
<td colspan="10" style="padding: 0">
<div>
<table class="table table-bordered table-hover">
<tr>
<td style="background-color: #80A3C1;">Kode Barang</td>
<td style="background-color: #80A3C1;">Qty</td>
<td style="background-color: #80A3C1;">Merk</td>
<td style="background-color: #80A3C1;">Hasil</td>
</tr>
<tr ng-repeat-start="details in listLaporanRetur.returDetailList">
<td>STUFFID1</td>
<td>10</td>
<td>APPLE</td>
<td>BOOM</td>
</tr>
<tr>
<td>STUFFID2</td>
<td>40</td>
<td>SONY</td>
<td>BREAK</td>
</tr>
<tr ng-repeat-end=""></tr>
</table>

</div>
</td>

</tr>
</tbody>
</table>
</div>
</div>
<button onclick="javascript:demoFromHTML();">PDF</button>

然后这是我的javascript

function demoFromHTML() {
var pdf = new jsPDF('p', 'pt', 'letter');
// source can be HTML-formatted string, or a reference
// to an actual DOM element from which the text will be scraped.
source = $('#customers')[0];

// we support special element handlers. Register them with jQuery-style
// ID selector for either ID or node name. ("#iAmID", "div", "span" etc.)
// There is no support for any other type of selectors
// (class, of compound) at this time.
specialElementHandlers = {
// element with id of "bypass" - jQuery style selector
'#bypassme': function (element, renderer) {
// true = "handled elsewhere, bypass text extraction"
return true
}
};
margins = {
top: 80,
bottom: 60,
left: 10,
width: 700
};
// all coords and widths are in jsPDF instance's declared units
// 'inches' in this case
pdf.fromHTML(
source, // HTML string or DOM elem ref.
margins.left, // x coord
margins.top, { // y coord
'width': margins.width, // max width of content on PDF
'elementHandlers': specialElementHandlers
},

function (dispose) {
// dispose: object with X, Y of the last line add to the PDF
// this allow the insertion of new lines after html
pdf.save('Test.pdf');
}, margins);
}

这是我的 jsfiddle http://jsfiddle.net/ugD5L/126/

如何正确打印表格,这样我就不必在 pdf 中弄乱表格?如果你们能给我看 jsfiddle 示例就更好了

最佳答案

正如答案嵌套表之一中提到的,到目前为止,JsPDF 不受支持,但如果您可以修改您的 html 位(如果可能),那么您可以解决您的问题。

    <tr ng-repeat-end="" class="table table-bordered table-hover">

<td style="background-color: #80A3C1;">Kode Barang</td>
<td style="background-color: #80A3C1;">Qty</td>
<td style="background-color: #80A3C1;">Merk</td>
<td style="background-color: #80A3C1;">Hasil</td>
</tr>

Demo

关于javascript - 如何使用 jspdf.js 正确打印 HTML 表格和 PDF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37958957/

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