Convert HTML to PDF in Angular 6

后端 未结 6 1688
执念已碎
执念已碎 2021-02-05 04:50

I have a component (Angular 6) which is an aggregation of several components. This produces a long HTML (I am using Bootstrap 4). Now I want to convert this HTML to PDF. I have

6条回答
  •  执念已碎
    2021-02-05 05:37

    Firstly install the following: 1) npm install jspdf 2) npm install html2canvas

    Later import and use the following code

     public captureScreen() {
          const data = document.getElementById('invoice');
          html2canvas(data).then(canvas => {
          const imgWidth = 208;
          const pageHeight = 295;
          const imgHeight = canvas.height * imgWidth / canvas.width;
          const heightLeft = imgHeight;
          const contentDataURL = canvas.toDataURL('image/png');
          const pdf = new jspdf('p', 'mm', 'a4'); 
          const position = 0;
          pdf.addImage(contentDataURL, 'PNG', 0, position, imgWidth, imgHeight);
          pdf.save('invoice.pdf'); 
          });
          }
    

提交回复
热议问题