如何使用javascript或jquery将总html页面转换为pdf [英] how to convert total html page to pdf using javascript or jquery

查看:78
本文介绍了如何使用javascript或jquery将总html页面转换为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将带有动态值的html页面转换为pdf,但我不能。我看到了一些类似 jspdf 的api,但这并不符合我的需要。任何人都可以推荐适合此目的的Javascript或jQuery库吗?

I tried to convert a html page with dynamic values in it's totality to pdf but i can't. I saw some api like jspdf but that doesn't suit my needs. Can anybody recommend a Javascript or jQuery library that fits this purpose?

推荐答案

这是一个在本地工作的小片段 - 您可以更改为适合您的主机设置:

Here's a little snippet which works locally - you can change to suitable your host set up:

URL url = new File("test.html").toURI().toURL();
WebClient webClient = new WebClient(); 
HtmlPage page = webClient.getPage(url);

OutputStream os = null;
try{
   os = new FileOutputStream("test.pdf");

   ITextRenderer renderer = new ITextRenderer();
   renderer.setDocument(page,url.toString());
   renderer.layout();
   renderer.createPDF(os);
} finally{
   if(os != null) os.close();
}

或者,这里是jsPDF的链接:http://code.google.com/p/jspdf

Alternatively, here's the link to jsPDF: http://code.google.com/p/jspdf

以下是使用jsPDF的有用示例:

Here's a useful example using jsPDF:

var doc = new jsPDF();
doc.text(20, 20, 'Hello world!');
doc.text(20, 30, 'This is client-side Javascript, pumping out a PDF.');
doc.addPage();
doc.text(20, 20, 'Do you like that?');

// Output as Data URI
doc.output('datauri');

更多信息可在此处找到:http://snapshotmedia.co.uk/blog/jspdf

More information can be found here: http://snapshotmedia.co.uk/blog/jspdf

这篇关于如何使用javascript或jquery将总html页面转换为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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