Create report
in PDF format using XSLFO and Apache FOP
XSLFO
is XSL Formatting Objects and can be used for formatting XML data.Apache
FOP (Formatting Objects Processor) is a Java application that reads a
formatting objects tree and renders the resulting pages to a specified output
(PDF in our case)
Requires:
- XSLT document (.xsl) with the :fo directives embedded into it. This XSLT document can be used in a java program to output the XSL-FO document- XML data or Java Object (convert Java Object to XML)
- Use Apache FOP driver to run the FO document against that Xml data to produce the PDF by using an XSLT Processor
Two stages of process
- XSLT to XSL-FO- XSL-FO to PDF
That process is similar with the following command of Apache FOP
Windows:
fop -xml name.xml -xsl name2fo.xsl -pdf name.pdf
Required Jars
- xml-apis.jar
- jaxb-api-2.2.3.jar
- fop-1.0.jar
- avalon-framework-4.1.3.jar
- commons-io-2.0.1.jar
- commons-logging-1.0.3.jar
- xmlgraphics-commons-1.4.jar
XSLT Processor/Engine
XSLT processor aims for transforming XML documents into HTML, text, or other XML document types.
Apache FOP use JAXP as default factory.Avoid relying on the JAXP factory mechanism for selecting your transformation engine. Instead load the engine you want explicitly: it's much more reliable and much faster.For Saxon, replace the call on TransformerFactory.newInstance()
with new net.sf.saxon.TransformerFactoryImpl()
For Xalan use
new org.apache.xalan.processor.TransformerFactoryImpl()
Generate font metadata & for embedding fonts
Use Apache FOP to generate font metadate- Setup classpath to run Apache FOP:
cmd> set CLASSPATH=fop.jar;lib\avalon-framework-4.2.0.jar;lib\commons-logging-1.0.4.jar;lib\commons-io-1.3.1.jar; lib\xmlgraphics-commons-1.4.jar
- Create the metrics file:
cmd> java org.apache.fop.fonts.apps.TTFReader FontName.TTF FONTNAME.xml
Custom configuration & embed fonts in jar
Create conf.xml<font metrics-url="/fonts/FontName.xml" kerning="yes" embed-url="/fonts/FontName.ttf">
<font-triplet name="FontName" style="normal" weight="normal" />
font-triplet name="FontName" style="italic" weight="normal" />
</font>
<font metrics-url="/fonts/FontName.xml" kerning="yes" embed-url="/fonts/FontName.ttf">
<font-triplet name="FontName" style="italic" weight="normal" />
</font>
Reference:
http://xmlgraphics.apache.org/fop/http://www.e-zest.net/blog/integrating-apache-fop-with-java-project-to-generate-pdf-files/