用 Java 将源 HTML 转为 PDF(2014 年)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23117664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open Source HTML to PDF in Java (2014)
提问by matty-d
I've been searching high and low for an up to date solution to this age old problem.
我一直在寻找解决这个古老问题的最新解决方案。
Long story short I want to take css + html -> pdf and do it in java.
长话短说,我想采用 css + html -> pdf 并在 java 中进行。
I don't want to use an API as the data is sensitive. Googling provides me with countless sites/services that offer to do this but I'm looking for a stand alone tool and looking for one that will work nicely from my java server. I've found this awesome looking command line toolbut it's a command line tool and spawning processes off a web server starts to get sketchy IMO (but I'm always willing to hear otherwise). Additionally flying saucerseems to be a standard choice, but I've heard mixed reviews.
我不想使用 API,因为数据很敏感。谷歌搜索为我提供了无数的站点/服务来实现这一点,但我正在寻找一种独立的工具,并正在寻找一种可以在我的 Java 服务器上很好地工作的工具。我发现了这个看起来很棒的命令行工具,但它是一个命令行工具,并且从 Web 服务器生成进程开始变得粗略 IMO(但我总是愿意听到其他情况)。此外,飞碟似乎是一个标准选择,但我听到了褒贬不一的评论。
Hereis a 5 year old questionon the subject, but I figure things have changed! Especially with all the work being done in the area of front end unit testing with dom manipulation I figure there might be some less than conventional solutions and I'm willing to hear them all!
这是一个关于这个主题的5 年前的问题,但我认为事情已经改变了!尤其是在使用 dom 操作进行前端单元测试领域的所有工作中,我认为可能存在一些比传统解决方案少的解决方案,我愿意全部听听!
Any help would be greatly appreciated.
任何帮助将不胜感激。
回答by radkovo
You might try a combination CSSBoxthat converts HTML+CSS to SVG and then use for example Batik for creating your PDF as proposed for example here. FlyingSaucer could also do the job.
您可以尝试将 HTML+CSS 转换为 SVG的组合CSSBox,然后使用例如蜡染来创建您的 PDF,例如此处的建议。FlyingSaucer 也可以完成这项工作。
The choice depends on your further requirements. E.g. are you processing "street HTML" or well-formed documents? What about the pages in the resulting PDF? What about interactive elements in the HTML pages?
选择取决于您的进一步要求。例如,您是在处理“街头 HTML”还是格式良好的文档?生成的 PDF 中的页面怎么样?HTML 页面中的交互元素呢?
I mean the only way is to try at least some options practically and then you may ask more specific questions about some particular problems.
我的意思是唯一的方法是至少在实践中尝试一些选项,然后您可能会针对某些特定问题提出更具体的问题。