How to convert HTML to pdf in angular2?

杀马特。学长 韩版系。学妹 提交于 2019-12-04 13:34:35

问题


I need to convert Dynamic generated html table in to pdf and able to print it too. I need it to be done in angular2 and Typescript.


回答1:


JSPDF works for angular 2. You need to download the definitions from dt~. Import the library as:

import * as jsPDF from "jspdf";
.
.
.

let doc = new jsPDF();

// Add a title to your PDF
doc.setFontSize(30); 
doc.text(12, 10, "Your Title");

// Create your table here (The dynamic table needs to be converted to canvas).
let element = <HTMLScriptElement>document.getElementsByClassName("pvtTable")[0];
html2canvas(element)
.then((canvas: any) => {
    doc.addImage(canvas.toDataURL("image/jpeg"), "JPEG", 0, 50, doc.internal.pageSize.width, element.offsetHeight / 5 );
    doc.save(`Report-${Date.now()}.pdf`);
})

In your system.js, in the map section add this line:

"jspdf": "<myLibs>/jspdf.js",


来源:https://stackoverflow.com/questions/42900319/how-to-convert-html-to-pdf-in-angular2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!