gpt4 book ai didi

jspdf - 如何使用 jspdf/autotable 在最后一页显示表页脚?

转载 作者:行者123 更新时间:2023-12-05 06:11:40 25 4
gpt4 key购买 nike

我有自动表的问题。我的表由 thead、tbody 和 tfoot 组成。我的脚是我在每一列上的总值(value)。我能够生成 pdf,但 tfoot 或总页脚不断打印在每一页上。我在检查文档时使用 showFoot: 'lastPage' 但它没有帮助。我不知道为什么我放的时候没有什么不同。

这是我的代码:

$(document).on("click", "#btnExportToPDF", function (e) { 
e.preventDefault();
var date = "as of "+(new Date()).toString().split(' ').splice(1,3).join(' ');
$("#loading").show();

var title = '12 Years Forecast';

// === Calculate text width ====
var text_width = $.fn.textWidth(title,'14px');
var half_width = (text_width/2); //times pt unit


var doc = new jsPDF('l', 'pt');
var totalPagesExp = '{total_pages_count_string}'
var base64Img = 'data:image/png;base64,;

if (base64Img) {
doc.addImage(base64Img, 'JPEG', 40, 8, 140,30, 'MEDIUM')
}

doc.autoTable({
html: '#year_table',
startY: 50,
didDrawPage: function (data) {
var pageSize = doc.internal.pageSize

},
// pageBreak: 'avoid',
bodyStyles: {
fontSize: 8,
halign: 'right',
},
headStyles: {
fontSize: 10,
halign: 'center'
},
});
doc.autoTable({
html: '#clients_table',
startY: doc.lastAutoTable.finalY + 25,
didDrawPage: function (data) {
var pageSize = doc.internal.pageSize
// Header
doc.setFontSize(14)
doc.setTextColor(40)
doc.setFontStyle('bold')
doc.text(title, (doc.internal.pageSize.getWidth()/2)-half_width, 22)
doc.setFontSize(10)
doc.setTextColor(150);
doc.setFontStyle('nomal')
doc.text(date, (doc.internal.pageSize.getWidth()/2)-42, 33)

// Footer
var str = 'Page ' + doc.internal.getNumberOfPages()
// Total page number plugin only available in jspdf v1.0+
if (typeof doc.putTotalPages === 'function') {
str = str + ' of ' + totalPagesExp
}
doc.setFontSize(10)
doc.setTextColor(150);
// jsPDF 1.4+ uses getWidth, <1.4 uses .width
var pageHeight = pageSize.height ? pageSize.height : pageSize.getHeight()
doc.text(str, 40, pageHeight - 10)
},

// pageBreak: 'avoid',
headStyles: {
fontSize: 10,
halign: 'center'
},
bodyStyles: {
fontSize: 8,
halign: 'right',
company: {
halign: 'left',
}
},
columnStyles: {
0: {
halign : 'left',
textColor: [28, 126, 214],
},
},
footStyles:{
fontSize: 10,
halign: 'right'
},
drawCell: function(cell, data) {
if (data.table.rows.length === 0 || data.table.rows.length - 1) {
cell.styles.setFontStyle = 'bold';
}
},
showFoot: 'lastPage',
});
// Total page number plugin only available in jspdf v1.0+
if (typeof doc.putTotalPages === 'function') {
doc.putTotalPages(totalPagesExp)
}
doc.save("reports.pdf")
$("#loading").hide();
});

希望有人能帮我解决这个问题。我找不到任何文件或论坛谈论这个。谢谢。

最佳答案

添加 showFoot: 'lastPage' 对我有用。我将在下面添加一个示例代码段。

doc.autoTable({
startY: currentHeight,
head: ["#", "Loan Id", "Customer Name", "Invoice Id", "Amount", "Late Fee"],
body: param.tableBody,
foot: [["", "", "", "Total", "1000", "60"]],,
didParseCell: (data) => {
if (data.cell && data.cell.section === "head") {
switch (data.cell.raw) {
case "Amount":
data.cell.styles.halign = "right";
break;
case "Late Fee":
data.cell.styles.halign = "right";
break;
default:
data.cell.styles.halign = "left";
break;
}
}
if (data.cell && data.cell.section === "foot") {
data.cell.styles.halign = "right";
}
},
columnStyles: {
4: { halign: "right" },
5: { halign: "right" },
},
headStyles: {
fillColor: [217, 217, 214],
textColor: [0, 0, 0],
fontSize: 10,
},
footStyles: {
fillColor: [217, 217, 214],
textColor: [0, 0, 0],
fontSize: 12,
},
showFoot: "lastPage",
});

table footer image

关于jspdf - 如何使用 jspdf/autotable 在最后一页显示表页脚?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63898782/

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