fromHTML not converting tables with 'colspan' attribute
If a table is declared like this:
<table border="0" cellpadding="2" cellspacing="2" width="100%">
<tr><td colspan="5">Col1</td><td colspan="3">Col2</td></tr>
<tr><td>SubCol1</td><td>SubCol2</td><td>SubCol3</td><td>SubCol4</td><td>SubCol5</td><td>SubCol6</td><td>SubCol7</td><td>SubCol8</td></tr>
<tr><td>...</td><td>...</td><td>...</td><td>...</td><td>...</td><td>...</td><td>...</td><td>...</td></tr>
</table>
the jsPDF fromHTML function shows the following error message:
jspdf.debug.js:7403 Uncaught TypeError: Cannot read property 'name' of undefined at tableToJson (jspdf.debug.js:7403) at DrillForContent (jspdf.debug.js:7547) at jspdf.debug.js:7743 at done (jspdf.debug.js:7618) at loadImgs (jspdf.debug.js:7647) at process (jspdf.debug.js:7741) at Object.jsPDFAPI.fromHTML (jspdf.debug.js:8140) at window.onload (mypage.htm:60) tableToJson @ jspdf.debug.js:7403 DrillForContent @ jspdf.debug.js:7547 (anonymous) @ jspdf.debug.js:7743 done @ jspdf.debug.js:7618 loadImgs @ jspdf.debug.js:7647 process @ jspdf.debug.js:7741 jsPDFAPI.fromHTML @ jspdf.debug.js:8140 window.onload @ mypage.htm:60
I've been hours around this till I found the glitch. I hope somebody fixes this bug. Thank you.
Can you provide a jsfiddle?
This is an example. The error shows up in browser's debug console.
https://jsfiddle.net/hrf8npkw/
I see the problem. It happens in fromHTML.js in line 297-300.
while (j < tableRow.cells.length) {
rowData[headers[j].name] = tableRow.cells[j].textContent.replace(/\r?\n/g, '');
j++;
}
It expects more cells, than it actually has. In your case the j-variable is 2 and higher but headers.length is 1 (=> 2 elements) maybe it gets fixed, when you change it to
while (j < tableRow.cells.length) {
if (j < headers.length) {
rowData[headers[j].name] = tableRow.cells[j].textContent.replace(/\r?\n/g, '');
}
j++;
}
Doesnt work as expected hmm
https://jsfiddle.net/L2qrtb4w/4/
We are closing this issue, because we will not support any longer fromHTML and addHTML.
Explaination:
We are working on a new html2pdf plugin, which will be based on html2canvas and our context2d plugin. This should lead to more reliable results for your projects. And it will give us the time to focus on the core functionality of pdf-generation because we will not use our energy for writing/supporting/extending 2 html plugins. If you still want to use addHTML or fromHTML you can still use jsPDF 1.4.1.
Best Regards
I see the problem. It happens in fromHTML.js in line 297-300.
while (j < tableRow.cells.length) { rowData[headers[j].name] = tableRow.cells[j].textContent.replace(/\r?\n/g, ''); j++; }It expects more cells, than it actually has. In your case the j-variable is 2 and higher but headers.length is 1 (=> 2 elements) maybe it gets fixed, when you change it to
while (j < tableRow.cells.length) { if (j < headers.length) { rowData[headers[j].name] = tableRow.cells[j].textContent.replace(/\r?\n/g, ''); } j++; }
Working but design broken.