jsPDF表格定制

问题描述 投票:0回答:1

所以我使用jsPDF在客户端生成PDF文档。现在我已经到了需要添加一些表格的步骤,但我没有看到任何与表格自定义相关的内容。

到目前为止,我在 stackoverflow 上发现的有关此问题的内容与导入一个名为 auto-table 的新库有关,但我不想使用其他插件。我猜其他插件都是基于这个库构建的,因为这个库支持自定义,对吗?

我只想使用 jsPDF,而不使用其他库。首先我想:

  • 设置单元格宽度
  • 设置标题背景颜色
  • 在表格中设置字体系列和文本颜色

这可能吗?如何实现?我认为这是应该支持的关键功能。

jspdf
1个回答
-1
投票

你需要做这样的事情:

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 },
  },
});
© www.soinside.com 2019 - 2024. All rights reserved.