PDF example

Dynamic PDF generation

In this example we will generate a PDF document containing text received in the HTTP request parameters.

To run the example, submit this form, after changing the provided text if you want to:

Title
Text

If your browser has problems with PDF coming out of a form submit use this link instead.

The sitemap

The FOPSerializer requires a document in the XSL-FO vocabulary for input, and converts this to PDF, handling fonts, page layout, etc.

Our sitemap will then apply an XSL transform to the output of the RequestGenerator to convert it to XSL-FO, using request parameter values provided by the generator.

Errors in the XSL-FO document can happen when implementing such a transform, and to make debugging easier we use a view in the sitemap, to access the intermediate document before it is processed by the FOPSerializer.

Here are the view and pipeline definitions:

<map:view id="view" name="xsl-fo" from-label="xsl-fo">
<map:serialize type="xml"/>
</map:view>
<map:match id="pdf" pattern="*/*.pdf">
<map:generate type="request"/>
<map:transform label="xsl-fo" src="{1}/{2}.xsl"/>
<map:serialize type="fo2pdf"/>
</map:match>

These definitions allow the intermediate XSL-FO document to be retrieved by adding the cocoon-view=xsl-fo parameter at the end of the URL, for example:
../pdf-example/pdf-example.pdf?title=....&cocoon-view=xsl-fo

XSL transformation

This transformation converts the RequestGenerator output in XSL-FO, inserting the value of the text and title request parameters in the output document:

<xsl:stylesheet id="main" version="1.0">
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="main" page-height="29.7cm" page-width="21cm" margin-top="1cm" margin-bottom="2cm" margin-left="2.5cm" margin-right="2.5cm">
<fo:region-body margin-top="3cm"/>
<fo:region-before extent="3cm"/>
<fo:region-after extent="1.5cm"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence id="page-sequence" master-reference="main">
<fo:flow flow-name="xsl-region-body">
<fo:block font-size="12pt" text-align="right"> Supersonic FOP example </fo:block>
<fo:block font-size="18pt">
<xsl:apply-templates select="//request:parameter[@name='title']"/>
</fo:block>
<fo:block font-size="12pt">
<xsl:apply-templates select="//request:parameter[@name='text']"/>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="request:parameter">
<xsl:value-of select="request:value"/>
</xsl:template>
</xsl:stylesheet>