1

在我的 .xsl 文件中,我使用这样的外部图形
<fo:external-graphic width="90pt" height="29pt" src="url(xsl/logo.jpg)"/>

但是图像没有在生成的 PDF 中加载,我在控制台中收到此错误。
[ERROR] Error while creating area : Error with image URL: xsl\logo.jpg (The system cannotfind the path specified) and no base URL is specified

我该如何解决这个问题?我想设置基本 URL 就可以了。但是如何设置基本 URL?请帮忙。

4

4 回答 4

3

我从这个链接
http://groups.yahoo.com/group/XSL-FO/message/6116得到了一个解决方案

使用 Java 代码设置基本目录

ServletContext servletContext = getServletConfig().getServletContext();

String appPath = servletContext.getRealPath(""); //root of web app
org.apache.fop.configuration.Configuration.put("baseDir",appPath);

这对我有用。
如果您知道任何更好的解决方案,请发布。

于 2011-03-24T08:31:37.310 回答
3

我正在使用 Apache FOP 1.1 版本。

    fopFactory = FopFactory.newInstance();
    // for image base URL : images from Resource path of project
    String serverPath = request.getSession().getServletContext().getRealPath("/");
    fopFactory.setBaseURL(serverPath);
    // for fonts base URL :  .ttf from Resource path of project
    fopFactory.getFontManager().setFontBaseURL(serverPath);

我在项目的资源总监中添加了所有图像和所需的字体字体文件。它对我来说很好。谢谢

于 2013-10-07T11:17:50.687 回答
2

我遇到了同样的问题,这只适用于 fop 0.95 版。SetBaseUrl 在 1.0 版中被忽略

于 2011-10-18T19:00:33.743 回答
0

版本 1.0、1.1 的解决方案:在 fop 1.0 和 1.1 中,方法 setBaseURL() 无法正确处理本地文件,因此您可以使用方法 setURIResolveri 并编写接口 URIResolver 的实现。

1.添加使用import javax.xml.transform.URIResolver;

2.添加主类

 private static class LocalResolver implements URIResolver {
         private String BaseFolder; 
            @Override
            public Source resolve(String href, String base) throws TransformerException {
             File f = new File(BaseFolder + "\\" + href);
             if (f.exists())
             return new StreamSource(f);
                     else
                      throw new TransformerException("File " + f.getAbsolutePath() +" not found!");         
            }

         public LocalResolver(String BaseFolder) {
           this.BaseFolder = BaseFolder;   
         }

     }

Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

3.在调用transformer.transform(src, res)之前添加:

fop.getUserAgent().setURIResolver(new LocalResolver("C:\\Users\\photon\\Downloads\\fop-1.1-bin\\fop-1.1"));
于 2013-12-02T09:57:32.800 回答