Search code examples
pdfpdf-generationxsl-foapache-fop

How to set Image as pdf page background


I'm having problems to use an image as background in table column. I need the image to be the background of each page in pdf.

The image is being displayed but it seems to be zoomed in. It sizes are width : 2487px, heigth : 3516px (width : 21.06cm, height : 29.77cm).

This is my code:

<xsl:template name="template-body">     
        <fo:block backgorund-color="yellow">                                
            <fo:table>
                <fo:table-column background-image="image.jpg"/>
                <fo:table-body>
                    <fo:table-row height="29.7cm">                          
                        <fo:table-cell>
                            <fo:block color="white">
                                SOME TEXT INSIDE THE IMAGE
                            </fo:block>
                        </fo:table-cell> 
                    </fo:table-row>
                </fo:table-body>                
            </fo:table>
        </fo:block>

    </xsl:template>

This code generates this:

enter image description here

But i need this:

enter image description here

I also tried to put the image as background of <fo:region-body> but i had the same problem. I think i need some way to specify the image width and height.

Any help? Thanks in advance.


Solution

  • Since you are using FOP, I believe there is no provision to scale a background-image to fit. This would be an extension to the XSL FO Specification.

    RenderX XEP supports this (http://www.renderx.com/reference.html#Background_Image).

    It is unclear if you actually want the image behind the table (and you have other content) or you actually want the image behind the whole page.

    You could put the image in an absolute positioned block-container and use content-width and content-height to scale, but this is not going to repeat for only the table. This would work for the page. If it is only the table, you are likely going to have to resize the actual image to fit correctly.