Puneet Sharma
Puneet Sharma

Reputation: 305

jsPDF-AutoTable- Is there any function to get the page count inside the `didParseCell` hook

I want to add total amount if the page count is more the one.

I am using didParseCell to add the total amount in the table data and its working but I am not able to get the Total page count so I can put any condition

I have also added the comment on the git of the plugin.(https://github.com/simonbengtsson/jsPDF-AutoTable/issues/550)

I have tried doc.internal.getNumberOfPages(),data.table.pageNumber and data.table.pageCount inside the didParseCell of autoTable.

All are giving page count 1

Code

let doc     = new jsPDF({
      orientation: 'landscape',
      unit: 'pt'
    });

    let totalAmountRow = {column1:'Total Amount',column2:'INR',column2:'40'}

    doc.autoTable(headers, data, {
      startY:yPoint += 20,
      margin: {top: 100},
      theme: "grid",      
      didParseCell: function (data) {           
            console.log("Data getNumberOfPages",doc.internal.getNumberOfPages()) // Wrong page count here           
            console.log("Data Table PageCount",data.table.pageCount)
            console.log("data Table PageNumber",data.table.pageNumber)
            if(totalAmountRow != ''){
                data.table.settings.body.push(totalAmountRow);              
                totalAmountRow = '';
            }
      },
        didDrawCell: function (data) {
        doc.setFillColor(255, 255, 255);
      },
      columnStyles: {
        amount: {cellWidth: 60},
        "Demo Text": {cellWidth: 50}
      },
      didDrawPage: function (data) {
        let footerStr;
        let pageCount = doc.internal.getNumberOfPages(); // Here I am getting correct page count 
        if (typeof doc.putTotalPages === 'function') {
          footerStr = pageCount + " / " + totalPagesExp;
        }
        if(pageCount != 0) {
          doc.addImage('imgURL', 'JPEG', 35, 30);
          doc.setFontSize(14);
          doc.text('Title', 35, 80);
          yPoint = 300;
        }
        doc.setFontSize(10);
        doc.text(footerStr, 400, 590);        
      }
    });

    if (typeof doc.putTotalPages === 'function') {
      doc.putTotalPages(totalPagesExp);
    }
    doc.save("pdfName.pdf");

Upvotes: 1

Views: 2319

Answers (1)

Simon Bengtsson
Simon Bengtsson

Reputation: 8151

The didParseCell is called right away which means you will always get 1 from getNumberOfPages. The easiest way to do what you want is probably add the row in didParseCell and then modify the content in willDrawCell.

Upvotes: 1

Related Questions