Use XStream, XSLT and XSL:FO to generate PDF

I recently digged into XStream and liked it immediatly. But I had no use for it since now. As the developer of webworks XSLT support I built a Bean-to-XML mapping layer before bad currently I have not the time to mantain it actively. For my current project I needed a way to print some reports in a swing application. Remembering Apache FOP I thought of using my webwork xml framework to generate some XSL:FO. However, I wanted to give XStream a change and it was quite successful.

The basic setup was using XStream to serialize my print content (e.g. a InvoiceBean) to XML and mangle it with a nicely crafted XSLT stylesheet who’s output would be rendered to PDF by FOP. The were some minor hurdles.

Repeated references to the same instance which XStream converts into XPath references are hard to impossible to render with XSLT. This can easily changed by setting an options in xstream but will give you some trouble in case of circular references. The is an extension in xalan to evaluta XPath references in XSLT stylesheet at runtime but I did not manage to get them werk.

The second problem was a little tougher. Because XStream acts as a serializing framework it serializes class fields, not properties. This subtle difference comes into play in case of derived properties like

public String getFullName() {
    return getFirstName() + " " + getLastName();

However I managed to use XStream with a custom BeanReflectionProvider which uses the Introspector api to determine the fields to “serialize”.

At a glance this gives you a nice printing franmework in Java based on major standards.

{ Comments are closed! }