xsltxsl-foapache-fop

How to organise images in a grid in a PDF generated with Apache-FOP?


I am generating PDF-s with Apache-fop. I have the following template snippet:

<xsl:template match="images">
    <xsl:apply-templates select="image"/>
</xsl:template>

<xsl:template match="image">
    <fo:block space-after="14px">
        <fo:external-graphic content-height="200pt" height="auto" width="auto"
                             src="url('data:{type};base64,{data}')"/>
    </fo:block>
</xsl:template>

And the XML data:

<images>
    <image>
        <type>image/png</type>
        <data>iVBORw0KGgoAAAANS...</data>
    </image>
    <image>
        <type>image/png</type>
        <data>iVBORw0KGgoAAAANS...</data>
    </image>
    /// ... more images
</images>

How can I change this so the images get in a two column grid, 3x2 image each page? (Size of the images have to be assigned accordingly)


Solution

  • EDITED ANSWER I would output a line break after each 2 pictures or a page break after each 6 pictures using the operator modulo mod as in:

    <xsl:template match="images">
        <fo:block><xsl:apply-templates select="image"/></fo:block>
    </xsl:template>
    
    <xsl:template match="image">
        <fo:external-graphic 
          content-height="200pt" height="auto" width="auto" 
          padding-after="14px" 
          src="url('data:{type};base64,{data}')"/>
        <xsl:if condition="position() mod 2 = 0">
            <fo:block/>
        </xsl:if>
        <xsl:if condition="position() mod 6 = 0">
            <fo:block page-break-before="always"/>
        </xsl:if>
    </xsl:template>
    

    If there is no room for 3 pictures in a row, the line will break automatically after 2 pictures. In that case, it will not be necessary to break the line manually. The same can be said for the page break.