Background Of FOP and related terms
What is FOP?
FOP is an acronym for Formatting Object Processor. Its a project developed by Apache.
FOP is a print formatter for XSL formatting objects.
It can be used to render an XML file containing XSL formatting objects into a page layout. The FOP command line application can be directly used to transform XML into PDF, PostScript, PCL and other formats.The library can be used in servlets and other Java applications.
What is XSL?
XSL is a W3C standard concerned with publishing XML documents. It consists of two parts: XSLT and XSL-FO. The acronym expands to eXtensible Stylesheet Language.
What is XSL-FO?
XSLFO (aka XSL-FO) is an XML vocabulary that is used to specify a pagination and other styling for page layout output. The acronym “FO” stands for Formatting Objects. XSLFO can be used in conjunction with XSLT to convert from any XML format into a paginated layout ready for printing or displaying.
XSLFO defines a set of elements in XML that describes the way pages are set up. The contents of the pages are filled from flows. There can be static flows that appear on every page (for headers and footers) and the main flow which fills the body of the page.
Synonyms: XSL FO, XSL (FO), XSL:FO, XSL-FO, Formatting Objects
What is XSLT?
XSLT describes the transformation of arbitrary XML input into other XML (like XSLFO), HTML or plain text. The “T” comes from Transformation. For historical reasons, a transformation is often also called a “style sheet”.
Synonyms: XSL transformation, XSL:T, XSL style sheet.
Problem Description:
In my web application we use FOP to generate a pdf document. This rendering is done from a DOM object and using a XSLT file.
When the application was made originally there were less number of user and no Asian user. But now the Chinese , Japanes and other Asian users got added and they wanted to generate the pdf in there langauge. This was not suppported by our application.. So I was suppose to make the required changes to support the Asian users.
Analysis:
Asian characters are double byte character and hance are not supported properly by the normal fonts like Arial etc. To support these character we need to have unicode fonts. As a part of my search result i got to know that the ArialUni.ttf font is the best supported font for the Asian languages and most of the other international languages.
If the font is not supporting a language then we will see a # in pdf which is the glypgh for unknown character.
Now we should take FOP as black box. We need to provide to the FOP the DOM object , template for pdf(i.e our XSLT file) and the desired font for render. If no font is specified then it maps to "any" which is nothing but the Arial font.
So to narrow down the scenario we need to do a configuration to provide the appropriate font family which will have support for international languages.
Solution:
Step 1: First of all download the FOP source. You can download the binary source also but it may have some problem related to class loading. So better download the latest FOP source. The source is available at http://xmlgraphics.apache.org/fop/0.95/index.html#download
It is is SVF form. I coudnt make out how to download the full project from here. So i made a google search and got a site from where i downlaoded the zip file of FOP .95 source.
So you can follow the same if are not able to download from the link.
Step 2: Extract the zip file in your directory and you have the full FOP project. Since you have downlaoded the Source you need to build the jar file on your own.
For this go to the cmd prompt and go to the fop directory. type ant all and press enter.
Doing this the project will genrate the fop jar and other related jars.
Take this FOP jar from the build folder of FOP project and put it in your application lib folder. I also copied the other jar files which got genrated as part of this command. Still i have to figure out if i can go ahead with out them.
Step 3: The success of step 2 is followed with step 3. Here we will see how to generate a Font -metric file.
As told earlier i will be using ArialUni.ttf font here. Download the font from net. The one i got was some 22mb size. We will have to use a TTFReader.java class of FOP to create a Font metric for the ArialUni font.
In the FOP Folder copy the jars from lib folder and the newly genrated jars from build folder and put them in one folder outside the project.
We will use an ant build script to generate the font metric file from TTFReader.java class

You will have to adjust the path accordingly and on running this command you will get a arialuni.xml which is nothing but our font-metric file.
Always Remeber for each font version you use it is required that you generate a new font metric file.This XML is specific to a particular font.
This close are Step 2. In Step 3 we will configure this metric file with your application.
Step 4: Now integrating the Web application.
Please ensure the following : 1) You have copied the fop.jar and other jars which got generted from FOP project. Also you need to update the jars you got from the FOP>lib folder in your web application. These are required by fop for their fucntioning.
2) Ensure these jars are now availble in the build path of project also.
3) Copy the fop.xconf file from FOP>config folder and the metric file generated. Copy them and keep them in the folder where you have also kept your web.xml file.
Its not a compulsion.
4) Edit the fop.xconf file as shown in the below image

5) Edit your xslt file to include the default font to be used for rendering. This can be easily done by giving the Font-family name in the fo:root tag of xslt file.
6) Ensure that the arialuni.ttf font is installed in your system.(Not sure if this step is really needed. need to identify this.)
7) The below java code will do the rest of the work:
FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent userAgent = fopFactory.newFOUserAgent();
File userConfigXml = new File("D:/HEAD/applications/proj/config/fop.xconf");
fopFactory.setUserConfig(userConfigXml);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
File copyrightFile = new File(fileUpLoadTempDir + copyrightPdfFileName); //this is the output file name
outputStream = new FileOutputStream(copyrightFile);
outputStream = new BufferedOutputStream(outputStream);
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, userAgent, outputStream);
Transformer transformer = transformerFactory.newTransformer(new StreamSource(xslSource)); //this is the xslt file passes to transformer
Source src = new DOMSource(copyrightDom);
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
Step 5 : Build and deploy your application.
Problem faced:
1) From where to get latest FOP: download the source and build the jar file.
2) Class not found: Check if the correct version of supporting jars are used and that the jars are present in the build path.
3) "arialuni,normal,700" not found. Susbtituting with"any, normal,400". This may come coz of many reason. Its a configuration mistake. Check that the triplet name provided is correct. In my case i kept the triplet name same as the Font-family name i.e Arial Unicode MS.
Future:
1) I need to work on the relative paths been used.
2) The Applicaition is running in unix environment so need to make configuration for the unix system. So have to find which font will work in UNIX supporting most international language.
3) clarify few dowubts like installation of font is nessary or not.
0 comments:
Post a Comment