XSL-FO外部图像未显示

3

我在将外部图片显示在PDF文件中遇到了一些问题。尝试使用url()方法的所有路径都无法生效。是否还有其他需要注意的地方?

<fo:table-cell>
    <fo:block>
        <fo:external-graphic src="url(ss/naam.png)"></fo:external-graphic>
    </fo:block>
</fo:table-cell>

1
如果你加上单引号,它能正常工作吗?例如:src="url('ss/naam.png')". - mzjn
很奇怪,但我们只需上传图像并传递URL来绕过它。 - Kevin Peters
@KevinPeters感谢您的回答,您能否再详细解释一下您提到的解决方案是如何实现的呢?关于上传图片怎么处理呢?您是指从Web服务器上传吗? - MigRome
6个回答

6

我正在使用 Apache FOP 1.1版本。

首先,您需要按照以下方式告诉 Apache FOP 基本路径在哪里。请按原样复制代码。

    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);

在您的XSL模板文件中:
<fo:table-cell>
    <fo:block>
        <fo:external-graphic src="url(resources/naam.png)"></fo:external-graphic>
    </fo:block>
</fo:table-cell>

这里的naam.png将位于资源目录中

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


即使这样做了,图像仍然不会出现,Web应用程序中也不会抛出任何异常。当它是一个简单的Java应用程序时,图像才会显示出来。 - Chetan
能够立即生效的是 src="images/fop.jpg",在这里您可以提供完整的文件路径,请查看 https://xmlgraphics.apache.org/fop/fo/align2.fo。 - Chetan
在哪里调用 "request.getSession()" 时出现了错误? - Adrian Jimenez

4
为了添加一个绝对路径的<fo:external-graphic>,我发现唯一可行的方法是将URL写成url(file:/C:/myPath/image.jpg)。参考来源
<fo:external-graphic src="url(file:/C:/myPath/image.jpg)"/>

0
如果您正在使用变量,这可能会有所帮助:
</fo:table-cell>
                                    <fo:table-cell number-columns-spanned="5"
                                        border="0.1pt solid gray"  
                                        padding="5pt">
                                         <fo:block>
                                            <fo:external-graphic 
                                            display-align="center"
                                            content-width="338px"
                                            content-height="290px"
                                            src="url(file:/{$screenshot-path})">
                                            </fo:external-graphic>
                                        </fo:block>
                                    </fo:table-cell>

0
一个特定于处理器的注意事项。XSL-FO标准规定uri-specification必须像url(...)这样,其中内容可以选择性地放在引号中,但至少在Apache FOP中,这种方式不起作用。在Apache FOP中有效的是相对于FO文件的简单路径。
<external-graphic src="../res/pic-1.png" />

这是他们在他们的样本中使用的东西。

0
<fo:external-graphic src="ss/naam.png"/>

这种方式对我有效。


-1

例如,它在我的 Apache FOP xls-fo 模板中运行。

<fo:external-graphic src='url("src/main/resources/ima/logo.depo.gif")' />

4
这只在IDE本地运行时有效,但在部署到独立应用容器中时无效。一旦应用程序构建完成,src/main/文件夹将不存在。 - Juanal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接