使用JavaScript将HTML(使用Javascript)转换为PDF [英] convert HTML ( having Javascript ) to PDF using JavaScript

查看:117
本文介绍了使用JavaScript将HTML(使用Javascript)转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将HTML(包含JavaScript)转换为PDF。我该怎么做?

I want to convert HTML (containing JavaScript ) to a PDF. How can I do that?

我只想显示网页中显示的内容。我正在显示由JavaScript库生成的甘特图。

I just want to show what is being shown in web page. I am displaying a gantt chart that is generated by a JavaScript library.

现在我想将该HTML网页保存为PDF,如何做到这一点?

Now I want to save that HTML web page as a PDF, how to do that?

推荐答案

我们也在寻找一些方法将html文件与复杂的javascript转换为pdf。
我们文件中的javasript包含 document.write 和DOM操作。

We are also looking for some way to convert html files with complex javascript to pdf. The javasript in our files contains document.write and DOM manipulation.

我们尝试使用 HtmlUnit 解析文件和飞碟渲染成PDF格式,但结果不够理想。它的工作原理,但在我们的情况下,pdf不够接近用户想要的。

We have tried using a combination of HtmlUnit to parse the files and Flying Saucer to render to pdf but the results are not satisfactory enough. It works, but in our case the pdf is not close enough to what the user wants.

如果你想试试这个,下面是一个代码片段,

If you want to try this out, here is a code snippet to convert a local html file to pdf.

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();
}

这篇关于使用JavaScript将HTML(使用Javascript)转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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