Answered by:
Uncaught TypeError: Cannot read property 'length' of undefined [code pen] issue

Question
-
User81789783 posted
i run my code in code pen [ https://codepen.io/someatoms/pen/adojWy?editors=1010] ,any one can run below both files in code pen and test
i m facing above mentioned error i.e
Uncaught TypeError: Cannot read property 'length' of undefined
i have below html file and js file ,but its giving an error
here is html file
<button onclick="generate()">Generate PDF</button> <table id="basic-table" style="display: block;"> <thead> <tr style="padding:0rem;"> <th style="width:4rem;padding:0rem!important;text-align: center;color: darkmagenta;max-width:4rem;">Frequency</th> <th style="max-width:8rem;width:8rem;padding:0rem!important;text-align: center;color: darkmagenta;">Task Name</th> <th style="width: 2rem;max-width:2rem;padding:0rem!important;text-align: center;color: darkmagenta;">Shift</th> <th style="width: 3rem;max-width:3rem;padding:0rem!important;text-align: center;color: darkmagenta;">Range</th> </tr> </thead> <tbody> <tr> <td colspan="35" style="padding:0"> <table class="table table-bordered"> <tr> <td class="blank" style="width:4rem;padding:0rem!important;text-align: center;border:0px;color: black;max-width:4rem;"> dsadsa </td> <td class="blank" style="max-width:8rem;border-bottom:0px;width:8rem;padding:0rem!important;text-align: center;color: black;"> sadads </td> <td style="width:2rem;max-width:2rem;border:1px"> IST </td> <td class="blank" style="width:3rem;max-width:3rem;"> asdasd </td> </tr> </table> </td> </tr> </tbody> </table>
and here is js file
function generate() { var doc = new jsPDF('l', 'pt'); var res = doc.autoTableHtmlToJson(document.getElementById("basic-table")); doc.autoTable(res.columns, res.data, {margin: {top: 80}}); var header = function(data) { doc.setFontSize(8); doc.setTextColor(40); doc.setFontStyle('normal'); //doc.addImage(headerImgData, 'JPEG', data.settings.margin.left, 20, 50, 50); doc.text("Testing Report", data.settings.margin.left, 50); }; var options = { addPageContent : header, columnStyles: { 0: { columnWidth: 15 }, 1: { columnWidth: 22}, 2: { columnWidth: 8.5 }, 3: { columnWidth: 10.5}, }, margin: { top: 10, bottom: 40, left: 1, right: 1, }, startY: doc.autoTableEndPosY() + 20, styles: {overflow: 'linebreak'}, tableWidth: 'auto', }; doc.autoTable(res.columns, res.data, options); doc.save("table.pdf"); }
Thursday, August 29, 2019 5:55 AM
Answers
-
User665608656 posted
Hi erum,
According to your description, when I ran your code, I did make the same mistake as you did.
I compared your code with the code in the link you provided and found that your tbody also contains a table tag, and that's the reason for this error message.
I recommend you change your html code as follow:
<button onclick="generate()">Generate PDF</button> <table id="basic-table" style="display: block;"> <thead> <tr style="padding:0rem;"> <th style="width:4rem;padding:0rem!important;text-align: center;color: darkmagenta;max-width:4rem;">Frequency</th> <th style="max-width:8rem;width:8rem;padding:0rem!important;text-align: center;color: darkmagenta;">Task Name</th> <th style="width: 2rem;max-width:2rem;padding:0rem!important;text-align: center;color: darkmagenta;">Shift</th> <th style="width: 3rem;max-width:3rem;padding:0rem!important;text-align: center;color: darkmagenta;">Range</th> </tr> </thead> <tbody> <tr> <td class="blank" style="width:4rem;padding:0rem!important;text-align: center;border:0px;color: black;max-width:4rem;"> dsadsa </td> <td class="blank" style="max-width:8rem;border-bottom:0px;width:8rem;padding:0rem!important;text-align: center;color: black;"> sadads </td> <td style="width:2rem;max-width:2rem;border:1px"> IST </td> <td class="blank" style="width:3rem;max-width:3rem;"> asdasd </td> </tr> </tbody> </table>
Best Regards,
YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 30, 2019 2:11 AM