Table of Contents
More Resources
Calculating the Total Number of Pages

Edit the templates in custom-xsl.xsl to calculate the total page number.
context

To calculate the total number of pages, we need to place a block with an Id on the last page of the document. We can then reference the page number of this block to get the total number of pages. This involves building the whole < fo:root> element inside a variable and then passing it through a postprocessing template that sticks the Id’d block at the end.

You need to edit the custom-xsl.xsl file (located in cfg/fo/xsl/).
  1. First, you need to build the < fo:root> inside a variable instead of straight into the output. You’ll copy the default root template and overwrite it. This is what the default from pdf2 looks like:

    Step Information
    < xsl:template match="/" name="rootTemplate"> 
      < xsl:call-template name="validateTopicRefs"/> 
      < fo:root xsl:use-attribute-sets="__fo__root"> 
        < xsl:call-template name="createLayoutMasters"/> 
        < xsl:call-template name="createMetadata"/> 
        < xsl:call-template name="createBookmarks"/> 
        < xsl:call-template name="createFrontMatter"/> 
        < xsl:if test="not($retain-bookmap-order)"> 
          < xsl:call-template name="createToc"/> 
        < /xsl:if> 
        < xsl:apply-templates/>       
        < xsl:if test="not($retain-bookmap-order)"> 
          < xsl:call-template name="createIndex"/> 
        < /xsl:if> 
      < /fo:root> 
    < /xsl:template> 
  2. Copy the default template to your custom-xsl.xsl file, wrap the < fo:root> in a variable, and then run each element through a postprocess template.

    Step Information
    < xsl:template match="/" name="rootTemplate"> 
      < xsl:call-template name="validateTopicRefs"/> 
      < xsl:variable name="fo"> 
        < fo:root xsl:use-attribute-sets="__fo__root"> 
          < xsl:call-template name="createLayoutMasters"/> 
          < xsl:call-template name="createMetadata"/> 
          < xsl:call-template name="createBookmarks"/> 
          < xsl:call-template name="createFrontMatter"/> 
          < xsl:if test="not($retain-bookmap-order)"> 
            < xsl:call-template name="createToc"/> 
          < /xsl:if> 
          < xsl:apply-templates/>       
          < xsl:if test="not($retain-bookmap-order)"> 
            < xsl:call-template name="createIndex"/> 
          < /xsl:if> 
        < /fo:root> 
      < /xsl:variable> 
      < xsl:apply-templates select="$fo/*" mode="postprocess"/> 
    < /xsl:template> 
  3. Next, you’ll add two required postprocess templates to the custom-xsl.xsl file. The first template matches the last < fo:flow> element and appends a block element to it:

    Step Information
    < xsl:template match="fo:flow[not(following::fo:flow)]" mode="postprocess"> 
      < xsl:copy> 
        < xsl:copy-of select="@*"/> 
        < xsl:apply-templates mode="postprocess"/> 
        < fo:block id="last-page-id" keep-with-previous.within-page="always"/> 
      < /xsl:copy> 
    < /xsl:template> 
  4. The second template matches everything else and passes the data through:

    Step Information
    < xsl:template match="*" mode="postprocess"> 
      < xsl:copy> 
        < xsl:copy-of select="@*"/> 
        < xsl:apply-templates mode="postprocess"/> 
      < /xsl:copy> 
    < /xsl:template> 
result

You added templates to calculate the total page number using the last page of the document. Now, see Updating the ‘Custom Page Number’ Variable to display the total number of pages.