简体   繁体   English

如何在angular2中将HTML转换为pdf?

[英]How to convert HTML to pdf in angular2?

I need to convert Dynamic generated html table in to pdf and able to print it too. 我需要将动态生成的html表转换为pdf并能够打印。 I need it to be done in angular2 and Typescript. 我需要在angular2和Typescript中完成。

JSPDF works for angular 2. You need to download the definitions from dt~. JSPDF适用于angular2。您需要从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: 在system.js的map部分中,添加以下行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM