Search code examples
imagepdfxsltrelative-pathxsl-fo

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


I am trying to create a pdf using xsl-fo where the background image is being used. I have set up the relative path as follows:

background-image = "url('../themes/images/logo.gif')"

When I give absolute path like background-image="C://Images/logo.gif", it worked. but when I use url to take it relatively in the server, it is not working.

Below is my scenario.

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 ( to get the 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>

Solution

  • As you say in the comments that you don't get any "Image not found" warning, I think there is nothing wrong with the image URL.

    Your stylesheet puts the image as background of an empty fo:block:

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

    An empty fo:block produces a single block area whose height is 0pt, so its background is not visible.

    If your image must be the background for the whole page, use an absolute positioned fo:block-container instead:

    <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>
    
    • the height attribute sets the height of the generated area, i.e. the area which will have the desired background (use the height you want)
    • you can also use the properties background-repeat and background-position-horizontal to control repetion and position of the image
    • the empty fo:block inside the container is needed if your FO processor is strict about validation (Apache FOP is, and it would give a validation exception if the block-container were empty)