使用angular2/typescript从html生成PDF文件 [英] Generate PDF file from html using angular2/typescript

查看:240
本文介绍了使用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');
...

这篇关于使用angular2/typescript从html生成PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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