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

时间:2015-01-30 11:40:16

标签: image pdf xslt relative-path xsl-fo

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

background-image =" 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>

1 个答案:

答案 0 :(得分:1)

正如你在评论中所说,你没有得到任何“未找到图像”的警告,我认为图片网址没有任何问题。

您的样式表将图片作为 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-repeatbackground-position-horizontal来控制图像的重复和位置
  • 如果您的FO处理器严格要求验证,则需要容器内的空fo:block(Apache FOP是,如果block-container为空,它将提供验证异常)