适用于 Java 的 PDF 生成库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3986105/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 07:57:13  来源:igfitidea点击:

PDF Generation Library for Java

javapdf-generationjasper-reportsitextapache-fop

提问by Stefan Haberl

I know this has been asked before, but I'm still undecided on which PDF generation framework to use for my current project.

我知道以前有人问过这个问题,但我仍然不确定要为我当前的项目使用哪个 PDF 生成框架。

My requirements

我的要求

  • on-the-fly generation of PDF documents (mainly order forms, invoices)
  • Java based
  • easy to layout
  • should be open source
  • easy to change layout
  • 即时生成 PDF 文档(主要是订单、发票)
  • 基于Java
  • 易于布局
  • 应该是开源的
  • 易于更改布局

A lot of people seem to use iText, but I have some concerns (apart from the changed licence) regarding separation of concerns: In an HTML context there's good MVC support, where I usually stick to Spring MVC and FreeMarker to separate logic and layout. I'm a little bit worried that with iText you end up mixing code and layout a lot.

很多人似乎在使用iText,但我对关注点分离有一些担忧(除了更改的许可证):在 HTML 上下文中有很好的 MVC 支持,我通常坚持使用 Spring MVC 和 FreeMarker 来分离逻辑和布局。我有点担心 iText 最终会大量混合代码和布局。

I am aware, that Apache FOPcould be a solution here, but then again I find XSLT tedious to work with and I read that FOP can be slow when it comes to huge throuput of many documents?

我知道,Apache FOP在这里可能是一个解决方案,但是我又发现 XSLT 使用起来很乏味,而且我读到 FOP 在处理大量文档时可能会很慢?

I also considered JasperReports, but from my understanding this is more suited for reports containing tabular datasets rather than single documents such as invoices which require a lot of layout formatting?

我也考虑过JasperReports,但根据我的理解,这更适合包含表格数据集的报告,而不是单个文档,例如需要大量布局格式的发票?

Any thoughts on this?

对此有何想法?

采纳答案by Janek Bogucki

Give JasperReports a try. Use iReportto create the .jrxml files. JapserReports can handle complex layouts. For those parts of the report based on different queries have a look at using subreports embedded into the main report.

试试 JasperReports。使用iReport创建 .jrxml 文件。JapserReports 可以处理复杂的布局。对于基于不同查询的报告的那些部分,请查看使用嵌入到主报告中的子报告。

Just like @Adrian Smith's solution this approach will separate the report layout editing from the data sourcing.

就像@Adrian Smith 的解决方案一样,这种方法会将报表布局编辑与数据来源分开。

回答by Adrian Smith

I have implemented a good solution where my software creates a format-independent "pure" XML file, then I give my boss the XSD and he puts it into Altova StyleVisionwhere he can WYSIWYG design reports based on data he plucks out from the XSD. That software produces an XSLT. So my program:

我已经实现了一个很好的解决方案,我的软件创建了一个与格式无关的“纯”XML 文件,然后我给我的老板 XSD,他将它放入Altova StyleVision,在那里他可以根据他从 XSD 中提取的数据设计所见即所得的报告。该软件生成 XSLT。所以我的程序:

  • Produces the format-independent "pure" XML
  • Transforms it with the XSLT, the output of which is XML-FO
  • Use Apache FOPto convert the XML-FO into PDF
  • 生成与格式无关的“纯”XML
  • 使用 XSLT 对其进行转换,其输出为 XML-FO
  • 使用Apache FOP将 XML-FO 转换为 PDF

This is a really great solution, means no more do I (as a programmer) have to change my code each time my boss wants to change a color in the report, my job is simply to produce "pure" XML.

这是一个非常好的解决方案,这意味着我(作为程序员)不再需要在每次我的老板想要更改报告中的颜色时更改我的代码,我的工作只是生成“纯”XML。

Update:I should also point out that I give my boss access to our SVN repository with Tortoise SVN which is sufficiently easy to use that he can use it without error. So he can check the XSLT files straight into SVN and run the build/deploy without even having to interrupt me from my work. Obviously that workflow only works with people who are sufficiently exact that they don't make mistakes etc., but it works out well for us in that case.

更新:我还应该指出,我让我的老板使用 Tortoise SVN 访问我们的 SVN 存储库,它非常易于使用,他可以毫无错误地使用它。因此,他可以将 XSLT 文件直接检查到 SVN 中并运行构建/部署,甚至不必打断我的工作。显然,该工作流程仅适用于足够精确以至于不会犯错误等的人,但在这种情况下对我们来说效果很好。

回答by Shervin Asgari

I have been using JODConverterfor a while and I really like it.

我已经使用JODConverter一段时间了,我真的很喜欢它。

What we do is use JODReportsto generate dynamic OpenOffice.org documents (which internally uses FreeMarker). Then we convert these documents to PDF documents using JODConverter.

我们所做的是使用JODReports生成动态 OpenOffice.org 文档(内部使用 FreeMarker)。然后我们使用 JODConverter 将这些文档转换为 PDF 文档。

It sounds like a lot of work, but it really isn't.

听起来工作量很大,但实际上并非如此。

回答by Curd

One possibility is

一种可能性是

  • to create your documents in PostScript format and then
  • convert it to pdf using ghostscript (ps2pdf)
  • 以 PostScript 格式创建文档,然后
  • 使用 ghostscript (ps2pdf) 将其转换为 pdf

回答by Paul Jowett

Docmosisallows you to create templates in Word or OpenOffice writer - separating concerns nicely and layout is then in the most familiar tools.

Docmosis允许您在 Word 或 OpenOffice编写器中创建模板 - 很好地分离关注点,然后在最熟悉的工具中进行布局。

回答by Prasanna L M

Based on my experience, I would suggest you to consider following Java PDF Librariesfor creating PDF reports,

根据我的经验,我建议您考虑使用Java PDF 库来创建 PDF 报告,

  1. DynamicReports
  2. Apache PDF Box
  3. iText PDF
  4. PDF Clown
  1. 动态报告
  2. Apache PDF 框
  3. iText PDF
  4. PDF小丑

For your requirement, I think DynamicReportswould be the right choice. I have been using Dynamic Reports from last 3 years for all my PDF Reporting requirements. With a very less amount of code, you can easily create a truly dynamic PDF. Dynamicreports is a wrapper around Jasper Report. So, it internally makes use of Jasper report.

对于您的要求,我认为DynamicReports将是正确的选择。过去 3 年我一直在使用动态报告来满足我所有的 PDF 报告要求。只需很少的代码,您就可以轻松创建真正动态的 PDF。Dynamicreports 是 Jasper Report 的包装器。因此,它在内部使用 Jasper 报告。