Как напечатать четыре разных таблицы с помощью jspdf-autotable - PullRequest
0 голосов
/ 18 февраля 2020

Я хочу напечатать эти две таблицы в формате pdf, используя плагин jspdf autotable. Но сценарий, который я написал, печатает только вторую таблицу. Я думаю, что проблема заключается в написании сценария. Кто-нибудь подскажет, как распечатать эти две таблицы с помощью jspdf-autotable. но у меня возникла проблема: «Ошибка типа Uncaught: невозможно прочитать свойство 'finalY' из неопределенного», эта ошибка будет отображаться в консоли. Здесь я покажу 4 таблицы шириной и высотой, и я напечатал одну страницу в формате PDF. Я работал над кодом, но не работал должным образом.

 function generate() {
    var doc = new jsPDF('p', 'pt', 'A4');
    let finalY = doc.autoTable.previous.finalY;
    var res = doc.autoTableHtmlToJson(document.getElementById('tbl1'));
    doc.autoTable(res.columns, res.data);
    var res2 = doc.autoTableHtmlToJson(document.getElementById('tbl2'));
    doc.autoTable(res2.columns, res2.data, {
    startY: doc.lastAutoTable.finalY + 50
    });
    doc.save("test.pdf");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.3/jspdf.debug.js"></script>          
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/2.1.0/jspdf.plugin.autotable.js"></script>
    
<div class="col-md-3">
  <table id="customers" class="table table-striped">
     <colgroup>
        <col width="60%">
        <col width="60%">
     </colgroup>
     <tbody>
        <tr style="background:#e5d56b;">
           <th>8 Gauge / 4.0MM</th>
           <th colspan="3">HOLE SIZE </th>
        </tr>
        <tr>
           <td>Height</td>
           <td style="background-color:#82ca2e;">2</td>
           <td style="background-color:#82ca2e;">3</td>
           <td style="background-color:#82ca2e;">4</td>
        </tr>
        <tr>
           <td>3 Feet</td>
           <td style="background-color:#82ca2e;">65</td>
           <td style="background-color:#82ca2e;">44</td>
           <td style="background-color:#82ca2e;">30</td>
        </tr>
        <tr>
           <td>4 Feet</td>
           <td style="background-color:#82ca2e;">86</td>
           <td style="background-color:#82ca2e;">58</td>
           <td style="background-color:#82ca2e;">40</td>
        </tr>
        <tr>
           <td>5 Feet</td>
           <td style="background-color:#82ca2e;">108</td>
           <td style="background-color:#82ca2e;">73</td>
           <td style="background-color:#82ca2e;">50</td>
        </tr>
        <tr>
           <td>6 Feet</td>
           <td style="background-color:#82ca2e;">129</td>
           <td style="background-color:#82ca2e;">87</td>
           <td style="background-color:#82ca2e;">60</td>
        </tr>
     </tbody>
  </table>
</div>
<div class="col-md-3">
  <table id="customers1" class="table table-striped">
     <colgroup>
        <col width="60%">
        <col width="60%">
     </colgroup>
     <tbody>
        <tr style="background:#e5d56b;">
           <th>10 Gauge / 3.0MM</th>
           <th colspan="3">HOLE SIZE </th>
        </tr>
        <tr>
           <td>Height</td>
           <td style="background-color:#82ca2e;">2</td>
           <td style="background-color:#82ca2e;">3</td>
           <td style="background-color:#82ca2e;">4</td>
        </tr>
        <tr>
           <td>3 Feet</td>
           <td style="background-color:#82ca2e;">36</td>
           <td style="background-color:#82ca2e;">23</td>
           <td style="background-color:#82ca2e;">19 </td>
        </tr>
        <tr>
           <td>4 Feet</td>
           <td style="background-color:#82ca2e;">48</td>
           <td style="background-color:#82ca2e;">30</td>
           <td style="background-color:#82ca2e;">25</td>
        </tr>
        <tr>
           <td>5 Feet</td>
           <td style="background-color:#82ca2e;">60</td>
           <td style="background-color:#82ca2e;">38</td>
           <td style="background-color:#82ca2e;">31</td>
        </tr>
        <tr>
           <td>6 Feet</td>
           <td style="background-color:#82ca2e;">72</td>
           <td style="background-color:#82ca2e;">45</td>
           <td style="background-color:#82ca2e;">38 </td>
        </tr>
     </tbody>
  </table>
</div>
<button onclick="javascript:demoFromHTML();" style="float:right;" class="btn btn-primary">Download PDF</button>

1 Ответ

1 голос
/ 14 марта 2020

Прежде всего, вы можете изменить скрипт здесь, после проверки этого скрипта он будет работать

<script type="text/javascript">
   document.getElementById('downloadbtn').addEventListener('click',
     exportPDF);

   var specialElementHandlers = {
     // element with id of "bypass" - jQuery style selector
     '.no-export': function(element, renderer) {
       // true = "handled elsewhere, bypass text extraction"
       return true;
     }
   };

   function exportPDF() {

     var pdf = new jsPDF('p', 'pt', 'a4');
     var img = new Image;
      pdf.text('Recommended Consumer Price list Month : <?php echo date('F Y'); ?>', 150,31);
      pdf.text('Customer Care : 9010316786', 150,51);
      pdf.text('email us : info@karimullagroup.com', 150,71);
      pdf.text('Website : www.karimullagroup.com', 150,91);


   var source = document.getElementById('canvas').innerHTML;

   margins = {
          top: 80,
          bottom: 60,
          left: 50,
          width: 322
      };
      // all coords and widths are in jsPDF instance's declared units
      // 'inches' in this case
      pdf.fromHTML(
      source, // HTML string or DOM elem ref.
      margins.left, // x coord
      margins.top, { // y coord
          'width': margins.width, // max width of content on PDF
          'elementHandlers': specialElementHandlers
      },

      function (dispose) {
          // dispose: object with X, Y of the last line add to the PDF 
          //          this allow the insertion of new lines after html
          img.onload = function() {
          pdf.addImage(this, 40, 10, 90, 70);
          pdf.save('starfence.pdf');
      } },margins);
          img.crossOrigin = "";  
          img.src = "images/Starfence.png";
      }

</script>
...