glassfish 中的 fop 无法渲染外部资源
我正在通过 fop 1.0 从 java 库生成 PDF 文件。单元测试运行良好,PDF 按预期渲染,包括外部图形:
<fo:external-graphic content-width="20mm" src="url('images/image.png')" />
如果我在 glassfish 3.1 中的 Java EE 应用程序中渲染它,我总是会收到以下错误:
Image not found. URI: images/image.png. (No context info available)
我仔细检查了图像是否可用。它在 .ear 文件中的 .jar 文件中可用,因此应该可由 ClasspathUriResolver 使用。这是我如何设置 fop-factory 的代码片段:
FopFactory fopFactory = FopFactory.newInstance();
URIResolver uriResolver = new ClasspathUriResolver();
fopFactory.setURIResolver(uriResolver);
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
...
我还将 URI 解析器分配给 TransformerFactory 和 Transformer,但没有成功。如果有人能帮助我那就太好了。
-- Wintermute
顺便说一句:ClasspathUriResolver() 看起来像这样
public class ClasspathUriResolver implements URIResolver {
@Override
public Source resolve(String href, String base) throws TransformerException {
Source source = null;
InputStream inputStream = ClassLoader.getSystemResourceAsStream(href);
if (inputStream != null) {
source = new StreamSource(inputStream);
}
return source;
}
}
I am generating a PDF file via fop 1.0 out of a java library. The unit tests are running fine and the PDF is rendered as expected, including an external graphic:
<fo:external-graphic content-width="20mm" src="url('images/image.png')" />
If I render this within a Java EE application in glassfish 3.1, I always get the following error:
Image not found. URI: images/image.png. (No context info available)
I double-checked whether the image is available. It is available within the .jar file in the .ear file and should therfore be available by the ClasspathUriResolver. This is a code-snipplet of how I setup the fop-factory:
FopFactory fopFactory = FopFactory.newInstance();
URIResolver uriResolver = new ClasspathUriResolver();
fopFactory.setURIResolver(uriResolver);
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
...
I also assigned the URI resolver to the TransformerFactory and the Transformer with no success. Would be great if someone can help me out.
-- Wintermute
Btw: the ClasspathUriResolver() looks like this
public class ClasspathUriResolver implements URIResolver {
@Override
public Source resolve(String href, String base) throws TransformerException {
Source source = null;
InputStream inputStream = ClassLoader.getSystemResourceAsStream(href);
if (inputStream != null) {
source = new StreamSource(inputStream);
}
return source;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您考虑使用不同的类加载器,然后
ClassLoader.getSystemResourceAsStream(href);
尝试
InputStream inputStream = getClass().getResourceAsStream(href);
或其他东西否则,也许吧。那么它有效吗?
You consider a different class loader then
ClassLoader.getSystemResourceAsStream(href);
Try
InputStream inputStream = getClass().getResourceAsStream(href);
or something else, maybe.Does it work, then?