Search code examples
xmlxsl-foapache-fop

Inserting a cover page into XSL:FO template


I have worked with these documents where I need to edit the existing code in order to map fields from a database. The XSL document already has the template and regions in place. My goal is to add a cover page before any of the margins of the template occur.

The code looks like this:

<fo:layout-master-set>
    <fo:simple-page-master master-name="Letter Page" page-width="8.3in" page-height="11.7in" 
        margin-top="0px" margin-bottom="0px" margin-right="0px" margin-left="0px">
        <fo:region-body region-name="xsl-region-body" 
            padding-top="0px" padding-bottom="0px" padding-left="0px" padding-right="0px" 
            margin-top="4.250cm" margin-bottom="2.750cm" margin-left="1.8cm" margin-right="1.8cm" 
            border-style="none" border-width="0px" border-color="" background="" background-repeat="no-repeat" background-position-horizontal="0px" background-position-vertical="0px" 
            extent="0px" column-gap="0px" column-count="1" reference-orientation="0"/>
        <fo:region-before region-name="xsl-region-before" 
            display-align="after" extent="3.8cm" 
            padding-top="0px" padding-bottom="0px" padding-left="0px" padding-right="0px" 
            border-style="none" border-width="0px" border-color="" background="" background-repeat="no-repeat" background-position-horizontal="0px" background-position-vertical="0px" reference-orientation="0"/>
        <fo:region-after region-name="xsl-region-after" 
            display-align="before" extent="2.750cm" 
            padding-top="0px" padding-bottom="0px" padding-left="0px" padding-right="0px" 
            border-style="none" border-width="0px" border-color="" background="" background-repeat="no-repeat" background-position-horizontal="0px" background-position-vertical="0px" reference-orientation="0"/>
        <fo:region-start region-name="xsl-region-start" 
            extent="1.8cm" 
            padding-top="0px" padding-bottom="0px" padding-left="0px" padding-right="0px" 
            border-style="none" border-width="0px" border-color="" background="" background-repeat="no-repeat" background-position-horizontal="0px" background-position-vertical="0px" reference-orientation="0"/>
        <fo:region-end region-name="xsl-region-end" 
            extent="1.8cm" 
            padding-top="0px" padding-bottom="0px" padding-left="0px" padding-right="0px" 
            border-style="none" border-width="0px" border-color="" background="" background-repeat="no-repeat" background-position-horizontal="0px" background-position-vertical="0px" reference-orientation="0"/>
    </fo:simple-page-master>
</fo:layout-master-set>

Then pages starts as

<fo:page-sequence master-reference="Letter Page">  
    <fo:static-content flow-name="xsl-region-before" font-size="8pt" font-family="Arial">

Followed by any other regions etc. I looked up how to insert some cover pages but I keep getting errors when I try and all I am using is notepad++ and rendering the pdf each time I test so it is becoming a very time consuming process. I've decided to just throw an image as the cover page to save myself some time. I am aware that this is an FOP engine 1.0.


Solution

  • Here's a simple example to help you through the process:

    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <fo:layout-master-set>
            <fo:simple-page-master page-width="8.5in" page-height="11in" master-name="Cover">
                <fo:region-body margin-top="0pt" background-color="red"/>
                <fo:region-before extent="0pt"/>
                <fo:region-after extent="0pt"/>
            </fo:simple-page-master>
            <fo:simple-page-master margin-top="1in" margin-left="1in"
                margin-bottom="18pt" margin-right="18pt"
                page-width="8.5in" page-height="11in" master-name="Pages">
                <fo:region-body margin-top="0pt"/>
                <fo:region-before extent="0pt"/>
                <fo:region-after extent="0pt"/>
            </fo:simple-page-master>
        </fo:layout-master-set>
        <fo:page-sequence master-reference="Cover">
            <fo:flow flow-name="xsl-region-body">
                <fo:block text-align="center">
                    I am a Cover Page, format me however you wish!
                </fo:block>
            </fo:flow>
        </fo:page-sequence>
        <fo:page-sequence master-reference="Pages">
            <fo:flow flow-name="xsl-region-body">
                <fo:block>
                    I am the rest of the text in the "Pages" page-sequence.
                </fo:block>
            </fo:flow>
        </fo:page-sequence>
    </fo:root>
    

    The "Cover" page sequence can have whatever on it you wish (I put some text and made it red), the "Pages" page sequence has the remainder of the content.

    This yields a two page document like this:

    enter image description here