【问题标题】:fop 2 + xsl = Image not foundfop 2 + xsl = 未找到图像
【发布时间】:2022-08-05 13:39:56
【问题描述】:

从一台服务器移动到另一台服务器时,使用 fop 2 + xsl 生成 pdf 时不再出现图片

试图理解别人的代码 使用 fop 2 + xsl 生成 pdf 时,我进入日志

[FOUserAgent] Image not found. URI: test/myimg2/logo.png (No context info available)

相对路径在 xsl 中指定,图像的完整路径将是 /myserv/mydir/test/myimg2/logo.png

我读到在 fop 2 版本中,您可以使用 FopFactoryBuilder 设置基本 url

FopFactoryBuilder builder = new FopFactoryBuilder(new File(\"/myserv/mydir/\").toURI(), resolver);

但是,在我的代码中,以下构造

FopConfParser parser = new FopConfParser(new File(fopBaseDir, \"userconfig.xml\"));
FopFactoryBuilder builder = parser.getFopFactoryBuilder();

fopBaseDir - fop library directory
fonts are specified in userconfig.xml

builder 已经使用 userconfig.xml 创建

你能告诉我如何指定图像的基本 URL 吗? 这可以在 userconfig.xml 中完成吗?

我不使用 servlet,JSF

我调试了代码,看到创建fop对象后,BaseUri通向userconfig.xml文件

fop -> foUserAgent -> ResourceResolver -> BaseUri = file:/myserv/etc/tomcat/fop/userconfig.xml

这是正常的吗?或者 BaseUri 不是用于搜索图像的基本 url?

    标签: java xml xslt apache-fop


    【解决方案1】:

    我注意到你已经用xslt 标记了这个问题;这表明您可能愿意使用 XSLT 来预处理您的 XSL-FO 文档并修复图像资源 URL,可能通过使用与 fo:external-graphic/@src 匹配并在图像基础 URI 前添加的模板,或者只是添加一个 @ 987654323@ 属性,例如

    <xsl:stylesheet version="1.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:fo="http://www.w3.org/1999/XSL/Format">
    
        <!-- supplement external-graphic's @src with a base URI -->
        <xsl:template match="fo:external-graphic/@src">
          <xsl:copy/>
          <xsl:attribute name="xml:base">file:/myserv/mydir/</xsl:attribute>
        </xsl:template>
        
       <!-- copy everything else unchanged -->
       <xsl:template match="node()|@*">
           <xsl:copy>
             <xsl:apply-templates select="node()|@*"/>
           </xsl:copy>
       </xsl:template>
       
    </xsl:stylesheet>
    

    您甚至可以在 XSL-FO 文档的根元素上设置 /myserv/mydir/xml:base 属性,但要注意这也会影响文档中的其他相关超链接。

    【讨论】:

    • 感谢您的回复。如果我不能以某种方式将基本 URL 推送到 FOP,那么您将不得不自己写入 xslt 文件
    • 您也可以将 imagedir 作为参数传入
    【解决方案2】:

    使用配置文件时的工作选项之一是在那里指定基本 url 设置

    <base>...</base>
    <font-base>...</font-base>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-22
      • 2021-01-16
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多