Monday, March 12, 2012

fonts problem in jasper report for pdf

when we export jaspert report to pdf format, we face problem with the font of the file. As, jasper report deal with its own true type font only for pdf. For this reason, we need to use the steps of font extension. This can be done following the steps below:

Ingredients: Jar file of jasperreports-fonts-v.v.v.(v stand for Version), true type fonts

1. Download the jasperreports-fonts-v.v.v(i.e jasperreports-fonts-4.0.1) jar file to modify according your requirements.
2.Right click on the jar file and select Extract to jasperreports-fonts-v.v.v.
3.At the root directory of the extracted folder, there is a properties file named: jasperreports_extension.properties

The file contains:
net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.dejavu=net/sf/jasperreports/fonts/fonts.xml

For the practise, here we will try to add the Times New Roman fonts to this jar. So, now we will have to add the following lines at the above file.
net.sf.jasperreports.extension.simple.font.families.timesnewroman=net/sf/jasperreports/fonts/fonts.xml

After adding we will get the file as:

net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.dejavu=net/sf/jasperreports/fonts/fonts.xml
net.sf.jasperreports.extension.simple.font.families.timesnewroman=net/sf/jasperreports/fonts/fonts.xml

4.Now, create the specific location to save the font collection. for this purpose, we create a directory in the location of ~/jasperreports-fonts-v.v.v\net\sf\jasperreports\fonts\. The name of the file will be timesnewroman for our case.

5. Now we need to add the collections of true type fonts at the above newly created location. Generally four files will be added as the representative of four categories: normal, bold, italic, bold italic.

6. Now its the time to modifying the fonts.xml file. We need to add the definition of the font to tis xml file. For our case, the definition will be as:

<fontFamily name="Times New Roman">
<normal>net/sf/jasperreports/fonts/timesnewroman/times.ttf</normal>
<bold>net/sf/jasperreports/fonts/timesnewroman/timesbd.ttf</bold>
<italic>net/sf/jasperreports/fonts/timesnewroman/timesi.ttf</italic>
<boldItalic>net/sf/jasperreports/fonts/timesnewroman/timesbi.ttf</boldItalic>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
<exportFonts>
<export key="net.sf.jasperreports.html">'Times New Roman', Arial, Helvetica, sans-serif</export>
<export key="net.sf.jasperreports.xhtml">'Times New Roman', Arial, Helvetica, sans-serif</export>
</exportFonts>
<!--
<locales>
<locale>en_US</locale>
<locale>de_DE</locale>
</locales>
-->
</fontFamily>

7. after completion of customization, again we will make the jar from the raw files. Entering into the extracted folder, select all the files. right clcik and select the option Add to Archive. select the type to ZIP. At the end of the name, change the extension to .jar.

8. After that use this jar where you want.



--
For converting code to show on blog, i have used http://centricle.com/tools/html-entities/

1 comment:

Prabhu said...

hi i have some issues regarding this..
my project uses japanese fonts and its been hard to find those fonts online, if i google the font only adobe seems to be selling it.
HeiseiMin-W3 is the font name.. my doubt is i need to buy this font i want to use it ?