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

时间:2021-08-11 01:35:13

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?

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

1 个解决方案

#1


12  

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

#1


12  

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