本文介绍了Apache的FOP的Java小程序 - 无图像preloader发现数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个成熟的商业产品看问题。

I'm looking at an issue in a mature commercial product.

在简单地说,我们正在使用Apache POI库的一部分在Word .doc或.docx文件读取,并将其转化为XSL-FO,使我们可以做令牌替换。然后,我们使用FOP - 嵌入Java程序 - 为FO数据转换成用于打印PDF文件。美中不足的是,这一切都被在客户端完成在Internet Explorer内部运行Java小程序里面。

In a nutshell, we are using part of the Apache POI library to read in a Word .DOC or .DOCX file, and convert it into XSL-FO so that we can do token replacements. We then use FOP – embedded into the Java program - to convert the FO data into a PDF for printing. The catch is, all this is being done on the client inside a Java applet running inside Internet Explorer.

本来我们使用FOP 0.93,这工作得相当好。但是,它无法生成PDF时,并会一切映射到时间,其中的一个客户不喜欢利用DOC文件中的字体。从理论上讲它可以作出加入某种字体规格数据的工作,但这需要为每个字体很可能会遇到一个比较复杂的定义,我们不能predict什么客户端是易于使用外MS芯字体的设置。

Originally we were using FOP 0.93, which worked reasonably well. However, it was not able to utilise the fonts inside the DOC file when generating the PDF and would map everything to Times, which one of the customers did not like. In theory it could be made to work by adding some kind of font metrics data, but that would require a relatively complex definition for every font it was likely to encounter and we can’t predict what the client is liable to use outside of the MS core fonts set.

要解决这个问题,FOP升级到1.0,增加了对操作系统的自动探测字体的支持。这个工作,但我们注意到,图像处理已经停止了工作和信纸已经不见了。
这似乎已经发生的是,里面的FOP图像加载器在某一点0.93和0.95,这样,而不是使用吉米和JAI它现在使用的ImageIO之间的改写。早期的实施工作得很好,但新的code不喜欢被运行作为applet。

To fix this, FOP was upgraded to 1.0, which added support for autodetecting the fonts from the operating system. This worked, but we noticed that the image processing had stopped working and the letterheads had disappeared.What appears to have happened is that the image loader inside FOP was rewritten at some point between 0.93 and 0.95 so that instead of using Jimi and JAI it now uses ImageIO. The earlier implementation worked fine, but the new code doesn’t like being run as an applet.

图片被嵌入在FO数据URI所以我们得到了如下的错误:
二零一四年九月三十零日17:00:10,607 ERROR [org.apache.fop.apps.FOUserAgent]图片不可用。 URI:
数据:图像/ JPEG; BASE64,iVBORw0KGgoAAAANSUhEUgAAALQAAABSCAIAAABysmn6AAA ...
... GGG ==。原因:org.apache.xmlgraphics.image.loader.ImageException:不支持的文件格式。发现数据没有图像preloader:图像/ JPEG; BASE64,iVBORw0KGgoAAAANSUhEUgAAALQAAABSCAIAAABysmn6AAAA ...

Images are embedded in URIs in the FO data so we get an error like this:2014-09-30 17:00:10,607 ERROR [org.apache.fop.apps.FOUserAgent] Image not available. URI:data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAALQAAABSCAIAAABysmn6AAA......ggg==. Reason: org.apache.xmlgraphics.image.loader.ImageException: The file format is not supported. No ImagePreloader found for data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAALQAAABSCAIAAABysmn6AAAA...

当通过测试工具运行时,产生正确的输出,但是当在浏览器内的小应用程序运行,我们得到上面的错误,让我怀疑浏览器的小应用程序的安全性在某种程度上干扰了ImageIO的插件加载器。

When run through a test harness, the correct output is generated, but when run as an applet inside the browser we get the above error which makes me suspect that the browser applet security is jamming the ImageIO plugin loader somehow.

在FOP转换的胆量,即它是引发错误的位是这样的:

The guts of the FOP transformation, i.e. the bit which is triggering the error is this:

// Step 4: Setup JAXP using identity transformer
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(); // identity transformer

transformer.transform(src, res);

这都被一个的PrivilegedAction块中,因为在FOP 1.0,它需要文件运行

... I / O访问来管理字体缓存。

...which is all being run inside a PrivilegedAction block since in FOP 1.0 it needed file I/O access to manage the font cache.

运行独立FOP linux下的0.93和1.0的方案,并使用strace的表明,它是写出来的临时文件中的图像数据,但两者0.93和1.0做类似的事情,所以它不应该是自身,特别是因为它应该有权限创建已临时文件。

Running the standalone FOP 0.93 and 1.0 programs under linux and using strace shows that it is writing out temporary files for the image data, but both 0.93 and 1.0 do similar things, so it shouldn’t be that by itself, especially since it should have permission to create temp files already.

我试过不同版本的JRE,因为有些建立了一个几年前显然有安全问题与ImageIO的图书馆,但无济于事。

I've tried different versions of the JRE since some builds a few years back apparently had security issues with the ImageIO library, but to no avail.

任何想法?

谢谢,

推荐答案

在情况下,任何人有类似的东西,这竟然是由该项目正在兴建中的Maven的方式所引起的。

In case anyone else has something similar, this turned out to be caused by the way the project was being built in Maven.

Fop的1.0及以上使用XML的图形库共享,方便图像渲染。正如在问题中提到,这使用它是使用JAR内的下列文件配置的插件注册表:

Fop 1.0 and above use the xml-graphics-commons library to facilitate the image rendering. As mentioned in the question, this uses a plugin registry which is configured using the following files inside the JAR:

META-INF /服务/ org.apache.xmlgraphics.image.loader.spi.ImageConverter

META-INF /服务/ org.apache.xmlgraphics.image.loader.spi.ImageLoaderFactory

META-INF /服务/ org.apache.xmlgraphics.image.loader.spi.Image preloader

META-INF/services/org.apache.xmlgraphics.image.loader.spi.ImageConverter
META-INF/services/org.apache.xmlgraphics.image.loader.spi.ImageLoaderFactory
META-INF/services/org.apache.xmlgraphics.image.loader.spi.ImagePreloader

...每个这些包含将被支持的图像德codeRS的列表。

...each of these contains a list of the image decoders which will be supported.

问题是,XML的图形常见的附带违约明智列表中的这些文件,而FOP还有一组矛盾的默认值,这对于一些奇怪的原因,禁用所有的图像去codeRS,那一人服用的优先级。

The problem is that xml-graphics-common ships these files with a sensible list of defaults, while FOP also has a conflicting set of defaults, which for some weird reason disable all of the image decoders, and that one was taking priority.

要解决这个问题,我确信,我的Maven pom.xml文件中导入XML的图形常见之前 FOP,以便其默认设置了precedence,而在这一点上窜出的一切生命。

To solve the problem, I made sure that my maven pom.xml file imported xml-graphics-common before FOP, so that its defaults took precedence, and at that point everything sprang to life.

我仍然不知道为什么code被作为一个独立的测试程序正常工作,但我怀疑它是类路径正在被处理给它的插件模式下运行不同的方式。

I am still not sure why the code was working correctly as a standalone test program, but I suspect it was the way the classpath was being handled being different to it running in plugin mode.

这篇关于Apache的FOP的Java小程序 - 无图像preloader发现数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:43