如何将 div 中的所有内容导出为 Angular 中的 pdf 2

How to export all inside a div as a pdf in Angular 2

我有一个带有 id="div1" 的 div,我需要将它转换成 pdf。所以我尝试在 angular 2.JSpdf 中使用 JSPf 正在工作很好,当我尝试做 doc.autoTable(col, rows);.但是当我想导出它时做 pdf 它不起作用

<div class="container" id="div1">
<table>
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
  
</table>
        <button id="create" (click)="convert()">Create file</button> 
    </div>


 convert(){
      const elementToPrint = document.getElementById('div1');
      console.log('eelementToPrint',elementToPrint); //The html element to become a pdf
      const pdf = new jsPDF('p', 'pt', 'a4');
      pdf.addHTML(elementToPrint, () => {
          doc.save('web.pdf');
      });
}

您指的是 doc.save()

中不存在的变量

您应该参考说明 addHTML 已弃用的文档,您应该使用 html2pdf 库。

http://rawgit.com/MrRio/jsPDF/master/docs/global.html#addHTML

https://github.com/spipu/html2pdf

首先,

pdf.save('web.pdf');

此外,如果您决定使用此功能,请记住该功能不支持 css。