title: Datatables - uncaught malformed table row a cell is undefined when trying to export to PDF
body: <p>I have the below table,</p>
  <pre><code>&lt;table id=&quot;dtBasicExample&quot; class=&quot;table table-bordered&quot; style=&quot;border : none;&quot;&gt;
      &lt;thead class=&quot;text-info&quot; style=&quot;border : none;&quot;&gt;
          &lt;tr&gt;
              &lt;th colspan=&quot;16&quot; style=&quot;border : none;&quot;&gt;&lt;/th&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;th colspan=&quot;2&quot; class=&quot;text-right&quot; style=&quot;border : none;&quot;&gt;Date&lt;/th&gt;
              &lt;th colspan=&quot;14&quot; style=&quot;border : none;&quot;&gt;: &lt;%=dstart%&gt; - &lt;%=dend%&gt;&lt;/th&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;th colspan=&quot;2&quot; class=&quot;text-right&quot; style=&quot;border:none;&quot;&gt;Personel&lt;/th&gt;
              &lt;th colspan=&quot;14&quot; style=&quot;border : none;&quot;&gt;: &lt;%=pers%&gt;&lt;/th&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;th colspan=&quot;2&quot; class=&quot;text-right&quot; style=&quot;border:none;&quot;&gt;Grade&lt;/th&gt;
              &lt;th colspan=&quot;14&quot; style=&quot;border : none;&quot;&gt;: &lt;%=grade%&gt;&lt;/th&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;th colspan=&quot;2&quot; class=&quot;text-right&quot; style=&quot;border:none;&quot;&gt;Date Generated&lt;/th&gt;
              &lt;th colspan=&quot;14&quot; style=&quot;border : none;&quot;&gt;: &lt;%=nowdate %&gt;&lt;/th&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;th colspan=&quot;16&quot; style=&quot;border-left : none; border-right: none;&quot;/&gt;
          &lt;/tr&gt;
          &lt;tr class=&quot;text-center&quot;&gt;
              &lt;th rowspan=&quot;3&quot;&gt;Date&lt;/th&gt;
              &lt;th colspan=&quot;14&quot;&gt;Number of Students&lt;/th&gt;
              &lt;th rowspan=&quot;3&quot;&gt;Grand Total&lt;/th&gt;
          &lt;/tr&gt;
          &lt;tr class=&quot;text-center&quot;&gt;
              &lt;th colspan=&quot;7&quot;&gt;Class A&lt;/th&gt;
              &lt;th colspan=&quot;7&quot;&gt;Class B&lt;/th&gt;
          &lt;/tr&gt;
          &lt;tr class=&quot;text-center&quot;&gt;
              &lt;th style=&quot;width:6.2%;&quot;&gt;Eng&lt;/th&gt;
              &lt;th style=&quot;width:6.2%;&quot;&gt;BM&lt;/th&gt;
              &lt;th style=&quot;width:6.2%;&quot;&gt;Man&lt;/th&gt;
              &lt;th style=&quot;width:6.2%;&quot;&gt;SC&lt;/th&gt;
              &lt;th style=&quot;width:6.2%;&quot;&gt;PHY&lt;/th&gt;
              &lt;th style=&quot;width:6.2%;&quot;&gt;CHE&lt;/th&gt;
              &lt;th style=&quot;width:6.2%;&quot;&gt;Sum&lt;/th&gt;
              &lt;th style=&quot;width:6.2%;&quot;&gt;Eng&lt;/th&gt;
              &lt;th style=&quot;width:6.2%;&quot;&gt;BM&lt;/th&gt;
              &lt;th style=&quot;width:6.2%;&quot;&gt;Man&lt;/th&gt;
              &lt;th style=&quot;width:6.2%;&quot;&gt;SC&lt;/th&gt;
              &lt;th style=&quot;width:6.2%;&quot;&gt;PHY&lt;/th&gt;
              &lt;th style=&quot;width:6.2%;&quot;&gt;CHE&lt;/th&gt;
              &lt;th style=&quot;width:6.2%;&quot;&gt;Sum&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
  ...
  </code></pre>
  <p>Below is my js for my PDF exporting.</p>
  <pre><code>{
      action: function (e, dt, button, config) {
          showNotification('top', 'right', 'PDF exported sucessfully!');
          $.fn.dataTable.ext.buttons.pdfHtml5.action.call(this, e, dt, button, config);
      },
      extend: 'pdf',
      className: 'btn-info btnspaces',
      text: 'Export to PDF',
      filename: 'Students',
      orientation: 'landscape',
      pageSize: 'LEGAL',
      customize: function (pdf) {
          var firstHeaderRow = [];
          var secondHeaderRow = [];
          //11
          $('#dtBasicExample').find(&quot;thead&gt;tr:nth-child(7)&gt;th&quot;).each(
              function (index, element) {
              var colSpan = element.getAttribute(&quot;colspan&quot;);
              var rowSpan = element.getAttribute(&quot;rowspan&quot;);
              firstHeaderRow.push({
                  text: element.innerHTML,
                  style: &quot;tableHeader&quot;,
                  colSpan: colSpan,
                  rowSpan: rowSpan
              });
              for (var i = 0; i &lt; colSpan - 1; i++) {
                  firstHeaderRow.push({});
              }
          });
          //10
          $('#dtBasicExample').find(&quot;thead&gt;tr:nth-child(8)&gt;th&quot;).each(
              function (index, element) {
              var colSpan = element.getAttribute(&quot;colspan&quot;);
              var rowSpan = element.getAttribute(&quot;rowspan&quot;);
              secondHeaderRow.push({
                  text: element.innerHTML,
                  style: &quot;tableHeader&quot;,
                  colSpan: colSpan,
                  rowSpan: rowSpan
              });
              for (var i = 0; i &lt; colSpan - 1; i++) {
                  secondHeaderRow.push({});
              }
          });
  
          pdf.content[1].table.headerRows = 3;
  
          var objLayout = {};
          objLayout['hLineWidth'] = function (i) {
              return .5;
          };
          objLayout['vLineWidth'] = function (i) {
              return .5;
          };
          objLayout['hLineColor'] = function (i) {
              return 'black';
          };
          objLayout['vLineColor'] = function (i) {
              return 'black';
          };
          objLayout['paddingTop'] = function (i) {
              return 5;
          };
          objLayout['paddingBottom'] = function (i) {
              return 5;
          };
          objLayout['paddingLeft'] = function (i) {
              return 9;
          };
          objLayout['paddingRight'] = function (i) {
              return 9;
          };
          pdf.content[1].table.body.unshift(secondHeaderRow);
          pdf.content[1].table.body.unshift(firstHeaderRow);
          pdf.content[1].layout = objLayout;
      }
  
  </code></pre>
  <p>I have no issue exporting this to excel, but when I try to export it to PDF, it is giving the error</p>
  <blockquote>
  <p>uncaught malformed table row a cell is undefined</p>
  </blockquote>
  <p>I tried to remove the colspan and manually add in <code>&lt;td&gt;&lt;/td&gt;</code> into the tables up to 16 cols, it works fine, but the date and grand total are in the last column instead of spanning throughout the 3 columns.</p>