Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display table footer on last page using jspdf / autotable?

I have a problem with autotable. My table consist of thead, tbody and tfoot. My tfoot is for my total value on every column. I was able to generate a pdf but the tfoot or the total footer keeps printing on every page. I use showFoot: 'lastPage' as I check the documentation but it didn't help. I don't know why there is no different when I put it.

Here's is my code:

$(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();
});

Hope someone can help me about this. I can't find any documents or forum talking about this. Thanks.

like image 647
RonCajan Avatar asked Sep 20 '25 02:09

RonCajan


1 Answers

Adding showFoot: 'lastPage' worked for me. I will add an example code snippet.

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

like image 57
Aflah Najeeb Avatar answered Sep 21 '25 19:09

Aflah Najeeb