This component is intended to easyly generate PDF documents by specifying
XML templates. The contents of the XML templates can dynamically be created by
using expressions which are evaluated by the
Expression Interpreter Using this
component you can dynamically build tables, define charts, define conditions or
simply fill tag contents.
The printing module is build around the DocumentHandler, which implements a
SAX Event Handler. It builds the output by using document tags, which are
served by the DocumentTagFactory. There is a huge number of default tags
registered with this factory. Look at the
tag documentation (generated
using the print module itself) to see what's available.
The most convenient way to use it is to call the DocumentPrinter component.
You can call it on command line to create your documents:
export CLASSPATH=lib/ujac.jar:lib/iText.jar:lib/commons-logging.jar java org.ujac.print.DocumentPrinter [options] <template> <outputfile> [<properties file>] |
The usage shown above is only for testing purposes. For productive usage you need to
integrate it into your own code.
Here's a short example:
... // defining the document properties, this map is used for dynamical content evaluation. Map documentProperties = new HashMap(); ... // instantiating the document printer FileInputStream templateStream = new FileInputStream("your-template-file.xml"); DocumentPrinter documentPrinter = new DocumentPrinter(templateStream, documentProperties); // in case you'd like to use a XML parser different from the default implementation // you can specify it here (apache xerces in this case). documentPrinter.setXmlReaderClass("org.apache.xerces.parsers.SAXParser"); // defining the ResourceLoader: This is necessary if you like to // dynamically load resources like images during template processing. documentPrinter.setResourceLoader(new FileResourceLoader("../")); // generating the document output FileOutputStream pdfStream = new FileOutputStream("your-output-file.pdf"); documentPrinter.printDocument(pdfStream); ... |
In case you're going to render the results of a XML transformation you'll like to
directly process the results of your transformation to save time and memory.
There's a component that helps you with this: The DocumentTranformHelper.
Here's a short usage example:
... File inXML = ...; File xslTemplate = ...; File outPDF = ...; Transformer trx = TransformerFactory.newInstance().newTransformer(new StreamSource(xslTemplate)); Source src = new StreamSource(new BufferedInputStream(new FileInputStream(inXML))); // instantiating and configuring the DocumentTransformHelper DocumentTransformHelper dfs = new DocumentTransformHelper(); Result out = dfs.prepare(new BufferedOutputStream(new FileOutputStream(outPDF)), false); // executing the transformation trx.transform(src, out); // finishing the document output dfs.finish(); ... |
The documentProperties map has to contain keys that have to be Strings, with no white spaces and no special characters like '$', '.' and '{' or '}'. The values, bound to these keys have to be or to extend a type, defined at the Expression Interpreter type handler documentation. If the type of your object doesn't match a specific type from that list, your instance is treated like a Java Bean, which means, that it allows the access of the object's properties by its getter methods.
The print module generates the output using the popular
iText library.
Click here to browse the API documentation of the current version.