使用angular2/typescript从html生成PDF文件

使用angular2/typescript从html生成PDF文件

问题描述:

我想使用我的HTML模板的一部分并将其转换为PDF文件,以使用户可以选择下载它. (例如,在他们单击按钮之后).

I want to take a part of my HTML template and convert it to PDF file to give the user an option to download it. (After they click a button for example).

我找到了一个名为jsPDF的库,如何在Angular2应用(RC4)中使用jsPDF?

I found a library called jsPDF, how would I use jsPDF in an Angular2 app (RC4)?

谢谢

如果要在生产环境中使用它,则绝对不希望依赖index.html中引用的Internet链接,就像@提出的那样. khalil_diouri.

If you want to use it in production, you definitely don't want to depend on an internet link being referenced in your index.html, like proposed by @khalil_diouri.

因此,要在Angular2/Typescript环境中正确使用它, 首先从npm安装它

So, to properly use it in an Angular2/Typescript environment, first install it from npm

npm install --save jspdf

如果使用的是SystemJS,请将其映射到配置文件中

If you are using SystemJS, map it in your config file

map: {
    "jspdf": "node_modules/jspdf/dist/jspdf.min.js"
}

安装定义包:(如果未安装)

Install definition package: (if not installed)

npm install typings --global

安装定义文件:

typings install dt~jspdf --global --save

最后,将其导入您的component.ts文件中

And, finally, import it in your component.ts file

import jsPDF from 'jspdf'
...
    let doc = new jsPDF();
    doc.text(20,20,'Hello world');
    doc.save('Test.pdf');
...