简体   繁体   中英

JSPDF : Uncaught TypeError: Cannot read property 'length' of undefined

I have table with image i want export table data with image to pdf with JSPDF but i got message

Uncaught TypeError: Cannot read property 'length' of undefined

This is my code any body can help me?

function downloadPDF() {
  var elem = document.getElementById('basic-datatable');
  var imgElements = document.querySelectorAll('#basic-datatable tbody img');
  // var data = doc.autoTableHtmlToJson(elem);
  var images = [];
  var i = 0;

  var canvas = document.querySelector('#myChart');
  var canvasImg = canvas.toDataURL("image/png", 1.0); //creates image
  var doc = new jsPDF('landscape'); //creates PDF from img
  doc.text("Laporan Peminjaman Inventaris", 110, 25);
  doc.addImage(canvasImg, 'png', 10, 45, 280, 150);
  doc.addPage();
  var res = doc.autoTableHtmlToJson(document.getElementById("basic-datatable"));
  doc.autoTable(res.columns, res.data, {
    // bodyStyles: {rowHeight: 30},
    drawCell: function(cell, opts) {
      if (opts.column.dataKey === 7) {
        var img = imgElements[opts.row.index];
        images.push({
          url: img,
          x: cell.textPos.x,
          y: cell.textPos.y
        });
        i++;
      }
    },
    addPageContent: function() {
      for (var z = 0; z < images.length; z++) {
        doc.addImage(images[z].url, images[z].x, images[z].y, 20, 20);
      }
    }
  });
  var options = {
    beforePageContent: header,
    // startY: doc.autoTableEndPosY() + 20
  };

  doc.save('LaporanDataPeminjaman.pdf');
  window.history.back();
}

This happens when autotable can't get a table.

The error is for sure between this lines:
var res = doc.autoTableHtmlToJson(document.getElementById("basic-datatable"));
doc.autoTable(res.columns, res.data, {...})

Maybe you are not correctly referencing the table.

It may also be that the line doc.autoTableHtmlToJson (document.getElementById ("basic-datatable")), is unnecessary and that you only need to pass the id of the element to doc.autoTable (IdOfDomElement, res.data, {...} )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM