如何使用 javascript 或 jquery 将整个 html 页面转换为 pdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12108806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert total html page to pdf using javascript or jquery
提问by laxman
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 jspdfbut that doesn't suit my needs. Can anybody recommend a Javascript or jQuery library that fits this purpose?
我试图将一个包含动态值的 html 页面全部转换为 pdf,但我不能。我看到了一些像jspdf这样的api但这不适合我的需求。任何人都可以推荐适合此目的的 Javascript 或 jQuery 库吗?
采纳答案by Tim Dodd
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();
}
Alternatively, here's the link to jsPDF: http://code.google.com/p/jspdf
或者,这里是 jsPDF 的链接:http: //code.google.com/p/jspdf
Here's a useful example using jsPDF:
这是一个使用 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');
More information can be found here: http://snapshotmedia.co.uk/blog/jspdf
更多信息可以在这里找到:http: //snapshotmedia.co.uk/blog/jspdf