javascript - 如何使用 jspdf-autotable 打印四个不同的表格

标签 javascript jquery jspdf jspdf-autotable

我想使用 jspdf autotable 插件将这两个表打印为 pdf。但我编写的脚本仅打印第二个表。我认为问题出在剧本的编写上。有人会指导我如何使用 jspdf-autotable 打印这两个表格吗?但我遇到了一个问题“未捕获类型错误:无法读取未定义的属性'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>

最佳答案

首先你可以更改脚本 一旦检查这个脚本它就会工作

<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>

关于javascript - 如何使用 jspdf-autotable 打印四个不同的表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60275555/

相关文章:

javascript - 无法获得正确的 knockout 绑定(bind)上下文

javascript - Safari 上的 Polymer 2.0

javascript - 是否可以让链接在网页源中不可见/隐藏,但在单击按钮时处于事件状态?

javascript - 无法关闭 jquery masonry + waypoint 中的动画

php - while 语句中的 jquery

php - html2canvas 和 jsPDF : send generated pdf as email attachment

javascript - 如何将 php 变量传递给 Wordpress AJAX 调用?

javascript - 全局 Javascript 对象如何保存状态?

javascript - PDF达到一定大小后无法添加内容

javascript - 在jspdf中添加自定义字体