Java HTML->PDF 解决方案?
我目前正在用 Java 构建一个基于 PDF 的报告。我已经研究过 iText 和 BIRT,但似乎需要付出很大的努力来学习他们的 API,我试图找到一个解决方案,让我可以在 HTMl 中生成报告(我已经知道的东西)并将其输出为 PDF。谁能提供一些可能的解决方案? - 谢谢! ——邓肯·克雷布斯
I'm currently working on building a PDF based report in Java. I have looked at iText and BIRT but it seems like a lot of effort to learn their API's what I'm trying to find is a solution that will let me generate the report in HTMl (something I already know) and output that to PDF. Can anyone provide some possible solutions? - Thanks! - Duncan krebs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Flying Saucer 将 XHTML 转换为 PDF。太棒了。它并不快。如果 XHTML 语法中有轻微错误,它就会失败。 (例如
,而它应该是
)这是让我开始的链接。它似乎使用 iText,但是一旦你让它工作起来,只需更改 HTML,它就会更新。
http ://today.java.net/pub/a/today/2007/06/26/generate-pdfs-with-flying-saucer-and-itext.html
可能有更好的方法,我就是这样做的。
Flying Saucer converts XHTML to PDF. It is great. It is not fast. It fails if there is a slight error in your XHTML syntax. (such as
<br>
when it should be<br/>
)This is the link that got me started. It seems to use iText, but once you have the thing working, just change the HTML and it updates.
http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html
There may be a better way, this is how I did it.
如果您的源 HTML 使用 CSS 进行样式化并且不一定格式良好,请尝试 PD4ML 库(免费供非营利使用)。
If your source HTML is styled with CSS and not necessarily well-formed, try PD4ML library (free for non-profit use).
我可以推荐jodconverter它在无头模式下使用openoffice
1安装openoffice(对于linux“zypper安装libreoffice”)
2 WIN将其放在路径变量中,这样“soffice”可以从任何地方运行,对我来说它是“C:\ Program Files (x86)\LibreOffice 4\program"
3 LINUX 确保运行 java 进程的用户拥有其主目录,因为
openoffice 需要在那里存储配置,对我来说,tomcat 运行了该过程,但其主目录由 root 拥有
4 将 jodconverter-lib 添加到您的 java 项目
转换
i can recommend jodconverter it uses openoffice in headless mode
1 install openoffice (for linux "zypper install libreoffice")
2 WIN put it on the path-variable so "soffice" can be run from everywhere, for me it was "C:\Program Files (x86)\LibreOffice 4\program"
3 LINUX make sure the user which runs the java process owns his home directory, because
openoffice needs to store configs there, for me tomcat ran the process, but its home dir was owned by root
4 add jodconverter-lib to your java project
convert
使用 phantomjs,您可以非常轻松地将 HTML 转换为 PDF:
如果
文件
名称以 < 结尾code>.pdf 则网页将另存为 PDF。 Phantomjs 还支持 PNG、JPG 和 GIF 输出。这是一个非常简单的示例,更一般地说,屏幕截图过程是非常可定制的(设置视口大小、启用/禁用 JavaScript 等)。有关详细信息,请参阅 屏幕捕获 上的 PhantomJS 页面。
Using phantomjs, you can convert HTML to PDF very easily:
If the
file
name ends with.pdf
then the webpage will be saved as PDF. Phantomjs also supports PNG, JPG and GIF output.This is a very simple example, more generally the screenshot process is very customizable (set viewport size, enable/disable javascript, etc). Look at PhantomJS's page on screen capturing for more info.
JavaFx WebKit 浏览器可用于 html 到 pdf 的转换。
对于 Windows,请安装 pdf24 打印机驱动程序;对于 Linux,请使用 cups-pdf。
安装后使用 print 方法。
如果使用外部库适合您,您可以轻松使用 ui4j 将网页打印为 pdf。
JavaFx WebKit browser could be used for html to pdf conversion.
For windows install pdf24 printer driver and for Linux use cups-pdf.
After installation use print method of WebEngine.
If using external library is ok for you, you could easily use ui4j to print Web page to pdf.