背景图像不显示在具有相对路径 URL 的 xsl fo 中

Background-image not displays in xsl fo with Relative Path URL

我正在尝试使用 xsl-fo 创建一个 pdf,其中使用了背景图像。我设置的相对路径如下:

背景图片="url('../themes/images/logo.gif')"

当我给出像 background-image="C://Images/logo.gif" 这样的绝对路径时,它起作用了。 但是当我在服务器中使用url相对取它时,它不起作用。

下面是我的场景。

XML:

<?xml version="1.0" encoding="iso-8859-1"?>
 <form-data>
       <field>
          <name>txtFirstName</name>
          <value>ABC</value>
       </field>
       <field>
          <name>txtLastName</name>
          <value>XYZ</value>
       </field>
</form-data>    

XSL-FO(获取 Pdf)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" version="1.0">
<xsl:template match="/">
  <fo:root>
     <fo:layout-master-set>
        <fo:simple-page-master master-name="SPM_Name" page-height="29.7cm" page-width="21cm" margin-top="1.2cm" margin-bottom="1.2cm" margin-left="1.75cm" margin-right="1.75cm">
           <fo:region-body margin-top="0.5cm" />
           <fo:region-before extent="0.5cm" />
           <fo:region-after extent="1cm" />
        </fo:simple-page-master>
        <fo:page-sequence-master master-name="PSM_Name">
           <fo:single-page-master-reference master-reference="SPM_Name" />
        </fo:page-sequence-master>
     </fo:layout-master-set>
     <fo:page-sequence master-reference="PSM_Name" initial-page-number="1">
        <fo:static-content flow-name="xsl-region-before">
           <fo:block text-align="end" font-size="10pt" font-family="serif" line-height="14pt">
              Page
              <fo:page-number />
           </fo:block>
        </fo:static-content>
        <fo:flow flow-name="xsl-region-body">
           <fo:block background-image="url('http://localhost:9081/resources/themes/images/Logo1.gif')" background-position="right" background-color="transparent" >
           </fo:block>
           <fo:block font-size="20pt" text-align="center" font-family="sans-serif" line-height="20pt" space-after.optimum="15pt" padding-top="18pt">Income Tax Form</fo:block>
           <fo:table table-layout="fixed" width="100%" border-collapse="separate">
              <fo:table-column column-width="50%" />
              <fo:table-column column-width="50%" />
              <fo:table-header text-align="center">
                 <fo:table-row>
                    <fo:table-cell padding="6pt" border="0.5pt solid black" wrap-option="wrap" keep-together.within-column="always">
                       <fo:block font-weight="solid">Questions</fo:block>
                    </fo:table-cell>
                    <fo:table-cell padding="6pt" border="0.5pt solid black" wrap-option="wrap" keep-together.within-column="always">
                       <fo:block font-weight="solid">Form Inputs</fo:block>
                    </fo:table-cell>
                 </fo:table-row>
              </fo:table-header>
              <fo:table-body text-align="center">
                 <fo:table-row>
                    <fo:table-cell padding="6pt" border="0.5pt solid black" wrap-option="wrap" keep-together.within-column="always">
                       <fo:block>Your First Name</fo:block>
                    </fo:table-cell>
                    <fo:table-cell padding="6pt" border="0.5pt solid black" wrap-option="wrap" keep-together.within-column="always">
                       <fo:block>
                          <xsl:value-of select="//field/value[../name/text() = 'txtFirstName']" />
                       </fo:block>
                    </fo:table-cell>
                 </fo:table-row>
                 <fo:table-row>
                    <fo:table-cell padding="6pt" border="0.5pt solid black" wrap-option="wrap" keep-together.within-column="always">
                       <fo:block>Your Last Name</fo:block>
                    </fo:table-cell>
                    <fo:table-cell padding="6pt" border="0.5pt solid black" wrap-option="wrap" keep-together.within-column="always">
                       <fo:block>
                          <xsl:value-of select="//field/value[../name/text() = 'txtLastName']" />
                       </fo:block>
                    </fo:table-cell>
                 </fo:table-row>
              </fo:table-body>
           </fo:table>
        </fo:flow>
     </fo:page-sequence>
   </fo:root>
</xsl:template>
</xsl:stylesheet>

正如您在评论中所说,您没有收到任何 "Image not found" 警告,我认为图像 URL.

没有任何问题

您的样式表将图像作为 fo:block:

的背景
<fo:block background-image="url('http://localhost:9081/resources/themes/images/Logo1.gif')" 
    background-position="right" background-color="transparent" >
</fo:block>

一个空的fo:block产生一个高度为0pt的单块区域,所以它的背景是不可见的。

如果您的图像必须是整个页面的背景,请改用绝对定位 fo:block-container

<fo:block-container position="absolute" height="25cm" 
    background-image="url('http://localhost:9081/resources/themes/images/Logo1.gif')" 
    background-position="right" background-color="transparent" >
    <fo:block/>
</fo:block-container>
  • height 属性设置生成区域的高度,即具有所需背景的区域(使用您想要的高度)
  • 您还可以使用属性 background-repeat and background-position-horizontal 来控制图像的重复和位置
  • 如果您的 FO 处理器对验证严格,则需要容器内的空 fo:block(Apache FOP 是,如果 block-container 为空,它会给出验证异常)