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

查看:563
本文介绍了使用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.

我遇到的问题是使用变压器.当执行转换时(示例的第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:

(Location of error unknown)org.apache.fop.fo.ValidationException: First element must be the fo:root formatting object. Found (Namespace URI: "", Local Name: "html") instead. Please make sure you're producing a valid XSL-FO document.

(Location of error unknown)org.apache.fop.fo.ValidationException: First element must be the fo:root formatting object. Found (Namespace URI: "", Local Name: "html") instead. Please make sure you're producing a valid XSL-FO document.

由于我对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天全站免登陆