如何使用javascript或jquery将整个html页面转换为pdf

how to convert total html page to pdf using javascript or jquery

本文关键字:html 转换 pdf javascript 何使用 jquery      更新时间:2023-09-26

我试图将一个包含动态值的html页面转换为pdf,但我做不到。我看到了一些api,比如jspdf,但这不适合我的需求。有人能推荐一个适合这个目的的Javascript或jQuery库吗?

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

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

下面是一个使用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