Introduction to Cocoon, XML XSL - Viewing your file as a pdf (Page 9 of 9 )
The last thing we do in this tutorial is convert the xml to a pdf file. Creating a pdf file is great when your users might want to keep or print a copy of the data. Since pdfs are not editable they are a great way to provide users with a document that they will not be able to accidentally (or purposely) modify later.
For pdf generation we will use FOP from Apache. FOP takes in an xml document, an FO stylesheet and outputs a pdf. FOP is actually a separate project from cocoon but the FOP jar is included in the cocoon distribution so you do not need to add anything new to the project. There are a lot of similarities with html or wml stylesheets but you will notice that the formatting is much more specific. You need to create a file called address-pdf.xsl and open it in a text browser. Begin the file with the usual xsl:stylesheet PI but you also need to say that you will be using the xml namespaces for xml and fo:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
Use an xsl template to match the contact item and add a processing-instruction for the type text/xslfo.
<xsl:template match="contact">
<xsl:processing-instruction name="cocoon-format">
type="text/xslfo"</xsl:processing-instruction>
For any fo file you first need an fo:root element. The fo:root element is essentially the same as the root xsl:template. It defines page layouts, page sequences and host of optional text formatting instructions.
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
Every fo:root must have one fo:layout-master-set. The fo:layout-master-set defines the layout specification including page margins.
<fo:layout-master-set>
<fo:simple-page-master
page-master-name="single"
margin-top="2cm"
margin-bottom="2cm"
margin-left="2.5cm"
margin-right="2.5cm">
<fo:region-body
margin-bottom="3cm"/>
<fo:region-after extent="1.5cm"/>
</fo:simple-page-master>
</fo:layout-master-set>
Next add an fo:page-sequence to define how to create the pages within the document. Your entire document can be within one fo:page-sequence or you can use different page-sequence for sections.
<fo:page-sequence>
<fo:sequence-specification>
<fo:sequence-specifier-alternating
page-master-first="single"
page-master-odd="single"
page-master-even="single"/>
</fo:sequence-specification>
Next you need to add fo:flow to output the text. In our file we are applying the templates from the xml result tree.
<fo:flow>
<xsl:apply-templates/>
</fo:flow>
</fo:page-sequence>
Close
the fo:root and xsl:template
</fo:root>
</xsl:template>
Finally we create xsl:templates for the elements of our contact item. In each template we have an fo:block which is used to define font, font size, alignment, etc.
<xsl:template match="name">
<fo:block font-size="12pt" text-align="justified">
<xsl:apply-templates/>
</fo:block>
</xsl:template>
<xsl:template
match="address">
<fo:block font-size="12pt" text-align="justified">
<xsl:apply-templates/>
</fo:block>
</xsl:template>
<xsl:template
match="phone">
<fo:block font-size="12pt" text-align-last="justified">
<xsl:apply-templates/>
</fo:block>
</xsl:template>
<xsl:template
match="e-mail">
<fo:block font-size="12pt" space-before.optimum="12pt"
text-align="justified">
<xsl:apply-templates/>
</fo:block>
</xsl:template>
</xsl:stylesheet>
You will notice that the xsl syntax is similar to previous xml files but with fo formatting instruction. Now all that needs to be done is to tell your xml file to use address-pdf.xsl. For simplicity open homer.xml and replace:
<?xml-stylesheet href="address-html.xsl" type="text/xsl"?>
with:
<?xml-stylesheet href="address-pdf.xsl" type="text/xsl"?>
It is possible to have both the pdf and html file generated at the same time and have the pdf downloaded from a link in the html page but it is not a straight forward as you might think. I will save that for another day. All that is left for you now is to point your browser to http://localhost:8080/address/homer.xml. If you have an Acrobat plugin the pdf should load automatically. If you do not have a plugin you should be prompted to download the file. Once downloaded open Acrobat to view the file.
That covers this tutorial. If everything has gone well you have a good idea of how powerful xml can be. Hopefully it can help you manage your data and make your life a little easier
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |