如何在angular2中将HTML转换为pdf? [英] How to convert HTML to pdf in angular2?

查看:187
本文介绍了如何在angular2中将HTML转换为pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将Dynamic动态生成的html表转换为pdf,并能够将其打印出来.我需要在angular2和Typescript中完成.

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.

推荐答案

JSPDF适用于angular2.您需要从dt〜下载定义. 将库导入为:

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`);
})

在您的system.js中,在地图部分中添加以下行:

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

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

这篇关于如何在angular2中将HTML转换为pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆