【问题标题】:Creating a jsPDF-Autotable with fixed column widths determining table width创建具有固定列宽确定表格宽度的 jsPDF-Autotable
【发布时间】:2019-08-07 04:29:25
【问题描述】:

我需要呈现一个从通常的数据呈现方式转置的表格(即在所需的表格中,每条记录由一列表示,每个字段由一行表示)。

我希望除第一列(标题列)之外的每一列都具有相同的宽度。

溢出的列将位于上表下方的新表中(见下文)。

对于最终表格(通常与位于其上方的表格的列数不同),列的宽度应该相同。

因此,在下面的 sn-p 生成的 .PDF 中,两个表格应该如此对齐(注意单元格的确切数量会有所不同):

+-----+----+----+----+----+----+----+----+----+----+----+
|Day  | 1  | 2  | 3  | 4  | 5  | 6  | 7  | 8  | 9  | 10 |
+-----+----+----+----+----+----+----+----+----+----+----+
+-----+----+----+----+----+
|Day  | 11 | 12 | 13 | 14 |
+-----+----+----+----+----+

// generating the data for the table
const cols = 14;
const headers = (new Array(cols + 1)).fill(0).map((x,i) => (i).toString());
headers[0] = 'DAY #:';
const today = new Date();
const data = [headers.map((x, i) => {
    const date = new Date(today);
    date.setDate(date.getDate() + i);
    return date.toLocaleDateString(navigator.languages);
  }),
  headers.map((x, i) => (1 - i / cols).toPrecision(3)),
];
data[0][0] = "Date:"
data[1][0] = "Dose:"

// splitting the table data at 10 columns + rowHeader wide
const maxCols = 10;
const tables = [];
const tableCount = Math.ceil(cols/maxCols)
for (let i = 0; i < tableCount; ++i) {
  const slicePos = 1 + i * maxCols;
  const mapColsInRange = (d) => [d[0]].concat(d.slice(slicePos, slicePos + maxCols));
  tables.push({
    headers: mapColsInRange(headers),
    data: data.map(mapColsInRange),
  });
}
// creating the tables
const doc = new jsPDF('l', 'mm', 'a4');
for (const t of tables) {
  doc.autoTable({
    theme: 'grid',
    head: [ t.headers ],
    body: t.data,
    pageBreak: 'avoid',
    styles: { halign: 'center', cellWidth: 21 },
    columnStyles: { 0: { halign: 'left', fontStyle: 'bold', cellWidth: 34 }},
  });
}
doc.save('test.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.1.4/jspdf.plugin.autotable.min.js"></script>

如何使用 jsPDF-autoTable 选项对齐列?

【问题讨论】:

    标签: jspdf-autotable


    【解决方案1】:

    添加 autotable 选项 tableWidth: 'wrap' 是解决方案

    【讨论】:

      【解决方案2】:
      const printableColumns = ['Account ID', 'First name', 'Last name', 'Created'];
      

      如果你想改变'Last name'的宽度,而不是在'columnStyles'中放置列索引号而不是dataKey并设置cellWidth

      doc.autoTable({
              head: [printableColumns],
              body: pdfData,            ,
              columnStyles: {
                  2: {cellWidth: 30},                           
              }            
          });
      

      【讨论】:

        【解决方案3】:

        您可以使用cellWidth 选项将列设置为固定宽度。

        【讨论】:

        • 感谢您的回答 - 不幸的是,它在 sn-p 中无法解决问题,该问题在样式和 columnStyles { 0 中设置单元格宽度。我也尝试将单元格宽度设置为每个 columnStyle [1...10],结果相同
        • 在repo的examples/examples.js中有一个例子展示了如何使用cellWidth样式。和我想的你想达到的很相似!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-26
        • 2014-04-17
        • 1970-01-01
        • 2018-09-05
        • 1970-01-01
        相关资源
        最近更新 更多