Search code examples
javajqueryjspdf-autotable

Is there way to export all html table pagination data using jspdf and autotable in javascript


I am exporting HTML table data into PDF. I have a lot of data to export, therefore my HTML table has a pagination option. while exporting I am getting only data which is appearing on the JSP page. Other data which are on pagination is not fetching in PDF. Is there any way to get all HTML table pagination data? I must have to use pagination option in HTML table because Data may be too large.

function exportTableToPDF(filename,report_title) {
    var doc = new jsPDF('p', 'pt','a4');
    var header = function(data) {
      if (data.pageCount > 1) {
        return false;
      }
      else
      {
        doc.setFontSize(11);
        //doc.setTextColor(200, 0, 255);
        doc.setFontStyle('bold');
        doc.text(report_title, data.settings.margin.left + 35, 60);
        doc.text('<%=DateLib.getDateTimeNow()%>', data.settings.margin.left + 35, 80);
      }
    };

    var totalPagesExp = '{total_pages_count_string}';
    var footer = function(data) {
      var str = 'Page ' + data.pageCount;
      // Total page number plugin only available in jspdf v1.0+
      if (typeof doc.putTotalPages === 'function') {
        str = str + ' of ' + totalPagesExp;
        console.log('test');
      }
      doc.text(str, data.settings.margin.left, doc.internal.pageSize.height - 30);
    };

    var options = {
      beforePageContent: header,
      afterPageContent: footer,
      pagesplit: true,
      margin: {
        top: 100
      }
    };

    var elem = document.getElementById('datatable-1');
    var data = doc.autoTableHtmlToJson(elem);
    doc.autoTable(data.columns, data.rows, options);

    // Total page number plugin only available in jspdf v1.0+
    if (typeof doc.putTotalPages === 'function') {
      doc.putTotalPages(totalPagesExp);
    }

    doc.save(filename);
  }

This provided code only extract data which are appearing on JSP at that time. For example, if my HTML table on page 4 using the pagination option, this will extract only page 4 data.


Solution

  • All data might not exist locally. You could either create a way to fetch the data for the pdf as json or include it on page right away some other way.