glassfish 中的 fop 无法渲染外部资源

发布于 2024-11-14 09:29:46 字数 1191 浏览 2 评论 0原文

我正在通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莳間冲淡了誓言ζ 2024-11-21 09:29:46

您考虑使用不同的类加载器,然后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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文