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

标签 jspdf jspdf-autotable

我有自动表的问题。我的表由 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/

相关文章:

javascript - jsPDF 服务器端 (node.js) 使用 node-jspdf

javascript - 如何禁用 jsPDF 嵌入默认字体列表?

javascript - jsPDF autotable右对齐x位置错误

javascript - jsAutotable 是否可以选择在超过某个 Y 后继续在新页面上?

javascript - 类中的 JSPDF 自动表格自定义样式

jspdf - 在 jspdf autotable 插件中访问嵌套的 JSON 属性

javascript - jsPDF 总是在控制台上返回 jsPDF PubSub 错误

jspdf - 在表格前后添加内容(jsPDF autotable)

javascript pdf表格生成

jspdf-autotable - 创建一个 jsPDF-Autotable,其中固定列宽决定表格宽度