使用 ApacheFOP 从 Java 中的 XML 生成 PDF [英] Generating PDF from XML in Java using ApacheFOP

查看:37
本文介绍了使用 ApacheFOP 从 Java 中的 XML 生成 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Java 对象生成动态 PDF 报告.我找不到很多这样的例子,所以我一直在关注这个例子:

I am trying to generate on-the-fly PDF reports from Java objects. I have not been able to find many examples of this, so I have been following this example:

http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/examples/embedding/java/embedding/ExampleObj2PDF.java?view=markup

这段代码和我自己的代码之间唯一真正的区别是我使用 JSON 转换和 XMLSerializer 生成了 src(示例中的第 81 行).

The only real difference between this code and my own is that I generate src (line 81 in the example) using JSON conversion and an XMLSerializer.

我遇到的问题是使用 Transformer.当我进行转换时(示例的第 87 行),我收到错误消息:

The problem I run into is using the Transformer. When I do the transform (line 87 of the example), I receive the error message:

(错误位置未知)org.apache.fop.fo.ValidationException:第一个元素必须是 fo:root 格式对象.找到(命名空间URI: "", Local Name: "html") 代替.请确保你是生成有效的 XSL-FO 文档.

由于我无法了解 Transformer,并且无法找到那种文档来告诉我它应该如何工作,所以我真的不知道转换在哪里失败.

Since I don't have visibility into the Transformer, and haven't been able to find the kind of documentation to tell me how it is supposed to work, I don't really know where the transformation is breaking down.

非常感谢任何有关此类转换的经验,或了解我所缺少的任何内容.

Any experience with this kind of conversion, or understand of whatever it is I'm missing is greatly appreciated.

推荐答案

Apache FOP 的输入是 XSL:FO.错误消息说输入不是有效的 XSL:FO 文档,因为第一个(根)元素应该是fo:root",但找到了html".FOP 不读取 HTML 输入.我建议以较小的步骤分解代码并保存中间文档以帮助您识别问题.步骤是:

The input of Apache FOP is XSL:FO. The error message is saying that the input is not a valid XSL:FO document because the first (root) element is expected to be "fo:root" but "html" was found. FOP does not read HTML input. I suggest to break down the code in smaller steps and save the intermediate documents to help you identify the problem. The steps are:

  1. JSON 对象被序列化为 XML
  2. 使用 XSLT 将 XML 转换为 XSL:FO
  3. XSL:FO 使用 FOP 转换为 PDF.

这篇关于使用 ApacheFOP 从 Java 中的 XML 生成 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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