gpt4 book ai didi

jspdf - 使用 jspdf 和 jspdf-autotable 在 pdf 中嵌套表格

转载 作者:行者123 更新时间:2023-12-02 00:07:13 32 4
gpt4 key购买 nike

如何使用jspdf和jspadf-autotable在PDF中实现嵌套表格?类似于下图的内容:

Nested tables in pdf

最佳答案

jspdf-autotable 中不支持嵌套表格,但您可以使用 didDrawCell Hook 绘制任何内容(包括其他自动​​表)。

var elem = document.getElementById("generate");
elem.onclick = function () {
var doc = new jsPDF();
doc.autoTable({
html: '#table',
didDrawCell: function (data) {
if (data.column.dataKey === 5 && data.cell.section === 'body') {
doc.autoTable({
head: [["One", "Two", "Three", "Four"]],
body: [
["1", "2", "3", "4"],
["1", "2", "3", "4"],
["1", "2", "3", "4"],
["1", "2", "3", "4"]
],
startY: data.cell.y + 2,
margin: {left: data.cell.x + data.cell.padding('left')},
tableWidth: 'wrap',
theme: 'grid',
styles: {
fontSize: 7,
cellPadding: 1,
}
});
}
},
columnStyles: {
5: {cellWidth: 40}
},
bodyStyles: {
minCellHeight: 30
}
});
doc.save('table.pdf');
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.2.3/jspdf.plugin.autotable.min.js"></script>
<button id="generate">Generate PDF</button>

<table id="table" style="display: none;">
<thead>
<tr>
<th>ID</th>
<th>First name</th>
<th>Last name</th>
<th>Email</th>
<th>Country</th>
<th>Table</th>
</tr>
</thead>
<tbody>
<tr>
<td align="right">1</td>
<td>Donna</td>
<td>Moore</td>
<td>dmoore0@furl.net</td>
<td>China</td>
<td></td>
</tr>
<tr>
<td align="right">2</td>
<td>Janice</td>
<td>Henry</td>
<td>jhenry1@theatlantic.com</td>
<td>Ukraine</td>
<td></td>
</tr>
<tr>
<td align="right">3</td>
<td>Ruth</td>
<td>Wells</td>
<td>rwells2@constantcontact.com</td>
<td>Trinidad and Tobago</td>
<td></td>
</tr>
<tr>
<td align="right">4</td>
<td>Jason</td>
<td>Ray</td>
<td>jray3@psu.edu</td>
<td>Brazil</td>
<td></td>
</tr>
<tr>
<td align="right">5</td>
<td>Jane</td>
<td>Stephens</td>
<td>jstephens4@go.com</td>
<td>United States</td>
<td></td>
</tr>
<tr>
<td align="right">6</td>
<td>Adam</td>
<td>Nichols</td>
<td>anichols5@com.com</td>
<td>Canada</td>
<td></td>
</tr>
</tbody>
</table>

关于jspdf - 使用 jspdf 和 jspdf-autotable 在 pdf 中嵌套表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39386969/

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