remi
remi

Reputation: 3988

Create multi-page pdf with table of content with fop

Assuming an input XML document such as this one:

<AuthorList>
  <Author>
    <name>John Steinbeck</name>
    <biography>John Ernst Steinbeck, Jr. (February 27, 1902 – December 20, 1968) was an American author of twenty-seven books, including sixteen novels, six non-fiction books
    </biography>
    <picture>
      http://upload.wikimedia.org/wikipedia/commons/e/e7/John_Steinbeck_1962.jpg
    </picture>
  </Author>
  <Author>
    <name>William Faulkner</name>
    <biography>William Cuthbert Faulkner (September 25, 1897 – July 6, 1962) was an American writer and Nobel Prize laureate from Oxford, Mississippi.
    </biography>
    <picture>
      http://upload.wikimedia.org/wikipedia/commons/f/f3/William_Faulkner_1949.jpg
    </picture>
  </Author>
</AuthorList>

I want to create a PDF with the first page(s) showing a "kind of" table of content, e.g. just a table with author names and an internal link to their full page. The rest of the PDF should be detailed sheets about each author, containing bio, picture, etc. 1 page per author.

I have already written a piece of code like this:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
        xmlns:fo="http://www.w3.org/1999/XSL/Format"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" version="1.0" indent="yes" />
  <xsl:template match="AuthorList">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <fo:layout-master-set>
    <!-- Layout for the table of content -->
    <fo:simple-page-master master-name="table" page-height="29.7cm" page-width="21cm" margin="1cm">
      <fo:region-body column-count="1" background-color="transparent" />
      <fo:region-after extent="2cm" />
    </fo:simple-page-master>
    <!-- Layout for individual sheets -->
    <fo:simple-page-master master-name="sheet" page-height="21cm" page-width="29.7cm" margin="2cm">
      <fo:region-body column-count="1" background-color="transparent" />
    </fo:simple-page-master>
      </fo:layout-master-set>


      <!-- First part of the PDF output: table of content -->
      <fo:page-sequence master-reference="table">
    <fo:flow flow-name="xsl-region-body">
      <fo:block>
        Must put here the table of content
      </fo:block>
    </fo:flow>
      </fo:page-sequence>

      <!-- Second part of the PDF output: sheets for authors -->
      <xsl:apply-templates select="Author"/>

    </fo:root>
  </xsl:template>


  <xsl:template match="Author">
    <fo:page-sequence master-reference="sheet">
      <fo:flow flow-name="xsl-region-body">
    <fo:block font-family="sans-serif" font-size="22pt" text-align="center">
      <xsl:apply-templates select="name" />
    </fo:block>

    <fo:block font-family="sans-serif" font-size="12pt" text-align="justify">
      <xsl:apply-templates select="biography" />
    </fo:block>
      </fo:flow>
    </fo:page-sequence>

  </xsl:template>

</xsl:stylesheet>

I'm now facing some problems:

  1. How can I create the table in the beginning of the PDF? I thought I could have an xsl:template matching author to add rows to the table, but since I already have one that is being used to produce the detailed sheets for the author, how can I differentiate the two of them?
  2. How can I introduce hyperlinks from elements in the table to the corresponding page for each author?
  3. I am also interested in suggestions for improvement, e.g. I put each author sheet in a page sequence, but probably I dont need that, although I do want each author page to be displayed in it's own page

I'm using apache-fop to produce the PDF.

Note that I have full hand at the way the original XML is produced so I could change the structure if that helps.

Upvotes: 0

Views: 2084

Answers (1)

remi
remi

Reputation: 3988

Sometimes taking time to write a proper question gives you more precise idea on what to look for. I just learnt that that <xsl-template> has a mode argument that can be specified, so I can write

<xsl:template match="Author" mode="toc">
  <!-- write some code to put in the table of content -->  
</xsl:template>


<xsl:template match="Author" mode="sheet">
    <fo:page-sequence master-reference="sheet">
      <fo:flow flow-name="xsl-region-body">
    <fo:block font-family="sans-serif" font-size="22pt" text-align="center">
      <xsl:apply-templates select="name" />
    </fo:block>

    <fo:block font-family="sans-serif" font-size="12pt" text-align="justify">
      <xsl:apply-templates select="biography" />
    </fo:block>
      </fo:flow>
    </fo:page-sequence>

  </xsl:template>

And my calling code becomes

    <!-- First part of the PDF output: table of content -->
      <fo:page-sequence master-reference="table">
    <fo:flow flow-name="xsl-region-body">
      <fo:block>
         <xsl:apply-templates select="Author" mode="toc"/>
      </fo:block>
    </fo:flow>
      </fo:page-sequence>

      <!-- Second part of the PDF output: sheets for authors -->
      <xsl:apply-templates select="Author" mode="sheet"/>

As for my second question, I need to create a link with the function <fo:basic-link internal-destination="{generate-id(.)}"> (generate-id as the name says generates an id instead of using an hard-coded one). Also, I create the link target using the id parameter in the <fo-block>, such as <fo:block id="{generate-id(.)}"> </fo:block>

Upvotes: 1

Related Questions