xsl-fo

How to avoid unwanted empty page


The following FO code is created by an XSL transformation. The result shall be a long PDF containing multiple pages. The content of page 1 might spill over to the next page. There might be the need for deliberately adding another page (Page 2 of 2 in this example). When I run the sample through a FO interpreter (I tried Apache's FOP as well as Compart's DocBridgeMill), I get this PDF:

I want to get rid of the empty page.

<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:layout-master-set>
        <fo:simple-page-master 
            margin-top="5mm" margin-left="10mm"
            page-width="210mm" page-height="50mm" master-name="FirstPage">
            <fo:region-body />
        </fo:simple-page-master>
        <fo:simple-page-master 
            margin-top="10mm" margin-left="10mm"
            page-width="210mm" page-height="50mm" master-name="OtherPage">
            <fo:region-body />
        </fo:simple-page-master>
        <fo:page-sequence-master
            master-name="PageSequence">
            <fo:single-page-master-reference
                master-reference="FirstPage" />
            <fo:repeatable-page-master-reference
                master-reference="OtherPage" />
        </fo:page-sequence-master>
        <fo:page-sequence-master
            master-name="FollowingPages">
            <fo:repeatable-page-master-reference
                master-reference="OtherPage" />
        </fo:page-sequence-master>
    </fo:layout-master-set>
    <fo:page-sequence initial-page-number="1"
        master-reference="PageSequence">
        <fo:flow flow-name="xsl-region-body">
            <fo:block>
                Page
                <fo:page-number />
                of
                <fo:page-number-citation
                    ref-id="5DAF3593E443DF58C1258934005BD76B" />
            </fo:block>
            <fo:block id="5DAF3593E443DF58C1258934005BD76B" />
        </fo:flow>
    </fo:page-sequence>
    <fo:page-sequence initial-page-number="1"
        master-reference="PageSequence">
        <fo:flow flow-name="xsl-region-body">
            <fo:block>
                Page
                <fo:page-number />
                of
                <fo:page-number-citation
                    ref-id="0C36B169CFBCD3DFC12589360067818E" />
            </fo:block>
        </fo:flow>
    </fo:page-sequence>
    <fo:page-sequence master-reference="FollowingPages">
        <fo:flow flow-name="xsl-region-body">
            <fo:block>
                Page
                <fo:page-number />
                of
                <fo:page-number-citation
                    ref-id="0C36B169CFBCD3DFC12589360067818E" />
            </fo:block>
            <fo:block id="0C36B169CFBCD3DFC12589360067818E" />
        </fo:flow>
    </fo:page-sequence>
</fo:root>

Solution

  • XSL-FO is written with the assumption that odd-numbered pages are on the recto (left, for us) side of a two-page spread and that even-numbered pages are on the verso (back, or right) side.

    The initial-page-number="1" on the second fo:page-sequence causes the formatter to emit the blank page after the first page sequence so that the second page sequence starts on a recto page as requested (however inadvertently). See https://www.w3.org/TR/xsl11/#initial-page-number


    If you were using Antenna House Formatter (and added xmlns:axf="http://www.antennahouse.com/names/XSL/Extensions" to the XSL-FO), you could use the axf:origin-id extension to get the effect that you want. See https://www.antenna.co.jp/AHF/help/en/ahf-ext.html#axf.origin-id

        <fo:page-sequence initial-page-number="1" id="seq1"
            master-reference="PageSequence">
            <fo:flow flow-name="xsl-region-body">
                <fo:block>
                    Page
                    <fo:page-number />
                    of
                    <fo:page-number-citation-last
                        ref-id="seq1" />
                </fo:block>
            </fo:flow>
        </fo:page-sequence>
        <fo:page-sequence id="seq2"
            master-reference="PageSequence">
            <fo:flow flow-name="xsl-region-body">
                <fo:block>
                    Page
                    <fo:page-number axf:origin-id="seq2" />
                    of
                    <fo:page-number-citation axf:origin-id="seq2"
                        ref-id="seq-last" />
                </fo:block>
            </fo:flow>
        </fo:page-sequence>
        <fo:page-sequence master-reference="FollowingPages"
                  id="seq-last">
            <fo:flow flow-name="xsl-region-body">
                <fo:block>
                    Page
                    <fo:page-number axf:origin-id="seq2" />
                    of
                    <fo:page-number-citation-last axf:origin-id="seq2"
                        ref-id="seq-last" />
                </fo:block>
            </fo:flow>
        </fo:page-sequence>