所以我使用jsPDF在客户端生成PDF文档。现在我已经到了需要添加一些表格的步骤,但我没有看到任何与表格自定义相关的内容。
到目前为止,我在 stackoverflow 上发现的有关此问题的内容与导入一个名为 auto-table 的新库有关,但我不想使用其他插件。我猜其他插件都是基于这个库构建的,因为这个库支持自定义,对吗?
我只想使用 jsPDF,而不使用其他库。首先我想:
这可能吗?如何实现?我认为这是应该支持的关键功能。
你需要做这样的事情:
const doc = new jsPDF();
const cellWidth = 40;
const headerBackgroundColor = '#3498db';
const font = 'Roboto';
const textColor = '#e74c3c';
const tableData = [...];
doc.setFont(font);
doc.setTextColor(textColor);
// Create a table with custom styling
doc.autoTable({
startY: 20,
head: [tableData[0]], // First row as header
body: tableData.slice(1), // Rest of the rows as body data
theme: 'grid',
headStyles: {
fillColor: headerBackgroundColor,
textColor: '#fff',
},
columnStyles: {
0: { cellWidth },
1: { cellWidth },
2: { cellWidth },
},
});