First, let’s have a look what XSL-FO actually is: XSL-FO is an XML-based language for type setting, i.e. for describing textual documents. It is quite similar to HTML in specifying margins and font properties. In a sense it is comparable to Latex as well because it is usually compiled into PDF format using a command line processor (though of course Latex is much more powerful especially because of its vast amount of styles and commands available).
If you want to know more about XSL-FO I refer you to the tutorial offer by w3schools. That’s at least where I learned how to use it. Additionally, you should have a look at the XSL-FO processor offered by the Apache Foundation. It provides an easy to use command line tool, which can be used to convert XSL-FO documents into PDF format. Moreover, it not only provides direct XSL-FO input, but also normal XML plus an XSL stylesheet. The XSL stylesheet is used to transform the original XML document into XSL-FO format. That’s also the final setup, I chose to use. The following figure depicts the components graphically:
As input I provide an XML document which contains the data of the business document and the XSL stylesheet which takes care about the layout. I’ll demonstrate this setup using an example bill. First let’s have a look at the XML data which has to be provided for generating a PDF bill:
In the XML document we specify some meta-information such as the date and the service period. Then we give information about the recipient and the items. Notice, that this XML format is not standardized. It suites very well my purpose, but can be changed according to personal preference.
This document now has to be transformed into a valid XSL-FO document. Therefore, I have developed one XSL stylesheet, which defines some text paragraphs and transforms the bill’sitems into a table layout. The stylesheet is depicted in the following figure:
As you can easily see, the template defines a header with Rechnungsdatum and other meta-information.
Then it prints a greeting line according to the sex of the recipient followed by a text paragraph.
After that we start layouting the table and so on.
Important to notice is that we import another stylesheet in the beginning (Letter.xsl) and define a parameter (the recipient data structure defined in the XML document).
This will cause the general letter layout to be wrapped around the bill-specific contents such as the date and the items table.
The letter stylesheet is presented in the following figure:
In the letter styleheet we define the A4 page layout, the header and footer contents, page margins and much more. We also use the recipient parameter to automatically write the address header to the PDF file. This way, each letter generated by this system will have the same basic layout, just contents change to the specific letter type.
Finally, I can say, that developing and using this system was quite comfortable. I experienced problems when trying to integrate graphical material into the XSL-FO documents. Apache’s open source XSL-FO processor seems to have some problems regarding this issue. Thus, it might also be interesting to generate Latex code from the XML documents. The system setup would basically be the same, just that you can use the power and flexibility of Latex.
I hope you learned a good deal about PDF generation and use these experiences when implementing your own infrastructures. Have a sunny day!



