Internationalization – Registering Custom Fonts for Apache FOP

April 28, 2008

Author: Fazal Gupta

This is a small article which explains the steps to register custom fonts with Apache FOP engine. Registering custom fonts with Apache FOP becomes important in the context of Internationalization, especially if one wants to display CJK languages in the PDF being generated. The context of this article is when fop is being used in other application for generating PDF for some XML data.

Step 1:
Install the required fonts to your OS. E.g. for Windows the fonts should be installed using the Control Panel or by copying the fonts to Windows Directory\Fonts.
If one is looking for Chinese/Korean Language, MS GOTHIC is a good font carrying a wide range of symbols.
The font can be in a standalone ttf (TrueType Font) file or a ttc (TrueType Collection)

Step 2:
Generate a font XML file that helps in registering the font with FOP engine. Fop.jar provides a TTFReader program to do this.
Example way to run the program
TTC Font: Java –classpath .\fop.jar org.apache.fop.fonts.apps.TTFReader –ttcname “DejaVu Sans” c:\windows\fonts\DejaVuSans.ttc dejavusans.xml
TTF Font: Java –classpath .\fop.jar org.apache.fop.fonts.apps.TTFReader c:\windows\fonts\DejaVuSans.ttf dejavusans.xml

Step 3:
Next we define a configuration file which JAVA program can read and update the FOP Configuration to use the new fonts along with the standard fonts available. The file is available in fop.jar named userConfig.xml.

Example structure of the configuration file

<font metrics-file=”file:///C:/ fonts/msgothic.xml” embed-file=”file:///C: /fonts/msgothic.ttc” kerning=”yes”>

<font-triplet name=”MSGothic” style=”italic” weight=”normal”/>

<font-triplet name=”MSGothic” style=”italic” weight=”bold”/>

<font-triplet name=”MSGothic” style=”normal” weight=”normal”/>

<font-triplet name=”MSGothic” style=”normal” weight=”bold”/>

</font>

The font name attribute in font-triplet tag should be taken from the font xml file.
If one uses fop program in a stand alone manner, one can use the userConfig.xml file to register the custom fonts there directly. The documentation of the XML file in fop.jar lists the usage of the custom file as follows
java org.apache.fop.apps.Fop -c userconfig.xml -fo fo-file -pdf pdf-file
If one directly modified the userConfig.xml one need not give the config file location.
But if one is using fop.jar in another application one may need to define an external xml file and make fop engine use the custom XML file as one may not invoke the fop program directly but use its API in turn. The custom XML file structure can be constructed by looking at userConfig.xml.

To achieve this, after generating the XML file, you can write the following piece of JAVA code to load the custom XML file and run the Apache engine to get the pdf byte stream
File userConfigFile = new File(configXmlLocation);
new org.apache.fop.apps.Options(userConfigFile);
ByteArrayInputStream xsl = new ByteArrayInputStream(inputByteArray);
ByteArrayOutputStream pdf = new ByteArrayOutputStream();
org.apache.fop.apps.Driver driver = new org.apache.fop.apps.Driver();
driver.setInputSource(new InputSource(xsl));
driver.setOutputStream(pdf);
driver.setRenderer(org.apache.fop.apps.Driver.RENDER_PDF);
driver.run();

That’s it. One can now write the xsl-fo file to use the new fonts mentioned and give to FOP to see the text in their desired fonts.

Entry Filed under: Internationalization. Tags: , .

1 Comment Add your own

  • 1. Subadhra  |  June 23, 2009 at 12:27 pm

    Thank you very much for this article. Really explained the scenario I was facing. I wasnt understanding why when so many countries were rendering PDF through FOP in my application, greece alone was using the basedir defined in fop.xml. This article explained it all. Thanks once again!

    Reply

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


New Blog

We Have moved to http://blog.webscale.co.in

Blog Stats

Top Posts

Feeds