使用 JavaScript 将 HTML(具有 Javascript)转换为 PDF [英] convert HTML ( having Javascript ) to PDF using JavaScript
问题描述
我想将 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?
推荐答案
我们也在寻找某种方法将带有复杂 javascript 的 html 文件转换为 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 来解析文件和 Flying Saucer 渲染为 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.
如果您想尝试一下,这里有一个将本地 html 文件转换为 pdf 的代码片段.
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屋!