Adding fonts to jasper based pdf reports

Recently I was confronted with the task of adding fonts to one of the jasper based projects. The challenge was to create pdf with cyrillic font support, for that matter any fonts that needs to be embedded in pdf has been a challenge in the past.

This post details the steps involved in successfully creating pdf embedded with the fonts of your choice.

Twitmmary
Download and extract  jasperreports-fonts-X.Y.Z.jar
Edit jasperreports_extension.properties to define new font families folder
Add custom fonts to font families folder
Edit fonts.xml to define the new font names and their variation.

Download and extract jasper font jar
Download and extract jasperreports-fonts-X.Y.Z.jar (optional jar) from http://sourceforge.net/projects/jasperreports/files/
X.Y.Z should be substituted with the relevant latest major/minor version number.

Edit jasperreports_extension.properties
At the root of the jasperreports-fonts-X.Y.Z.jar you should see jasperreports_extension.properties

On editing the file you should see
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

Add the following
net.sf.jasperreports.extension.simple.font.families.<<newfontfamilyname>>=net/sf/jasperreports/fonts/fonts.xml

<<newfontfamilyname>> is what you would like to call the fonts that you would like to add. For example lets call it “timesnewroman”

So your file would now look like

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

Add custom fonts to font families folder

Create a folder by name <<newfontfamilyname>> in our case timesnewroman  under net/sf/jasperreports/fonts

In our example by now we should have  net/sf/jasperreports/fonts/timesnewroman created.

Copy all the relevant fonts that you would like to add into this folder. A typical font folder will have
standard fonts
bold fonts
italic fonts
bold italic fonts

Lets assume that after  copying font files the resultant directory would look something similar to this.
net/sf/jasperreports/fonts/timesnewroman/Times_MS.ttf
net/sf/jasperreports/fonts/timesnewroman/Times_MS_Bold.ttf
net/sf/jasperreports/fonts/timesnewroman/Times_MS_Italic.ttf
net/sf/jasperreports/fonts/timesnewroman/Times_MS_Bold_Italic.ttf

We are almost there, the next step is to edit the fonts.xml

Edit fonts.xml to define the new font names and their variation.

Add the fonts downloaded to the fontFamily name in fonts.xml.

<fontFamily name="Times New Roman">
   <normal>net/sf/jasperreports/fonts/timesnewroman/Times_MS.ttf</normal>
   <bold>net/sf/jasperreports/fonts/timesnewroman/Times_MS_Bold.ttf</bold>
   <italic>net/sf/jasperreports/fonts/timesnewroman/Times_MS_Italic.ttf</italic>
   <boldItalic>net/sf/jasperreports/fonts/timesnewroman/Times_MS_Bold_Italic.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>
</fontFamily>

Remember to use fontName=”Times New Roman” in your jrxml

Now your java code should be able to generate pdf with the embedded font.

Note, If you are using IReport to test the fonts mentioned in your configuration then these fonts should be installed in your system.

About these ads
Comments
13 Responses to “Adding fonts to jasper based pdf reports”
  1. Aksar says:

    Dude…Thanks a lot, It worked…..
    I am using jasperreport 4.0.2. Its is also easy to create a .jar file similar to the ‘jasperreports-fonts-X.Y.Z.jar’ with your own directory structure for .TTF files and keep it in the class path.

  2. gary says:

    Also note that by using extensions, you SHOULD NOT use pdfFontName property in your jrxml file because it is deprecated now. Just use fontName and it will be OK.
    Another idea is that this jar file can be easily generated by iReport. No need to deal with the files and the package structure. Enter options – fonts pane and click Install Font to begin.

  3. felagund finrod says:

    Thx man, you saved my day.

  4. Roberto says:

    Thanks a lot, but you have forgot to say that the fonts you declare in the .jar file, must be installed in the system.
    I work with iReport 4.0.2
    Bye

  5. Raja says:

    how i add in Java code…

  6. Raja says:

    i am generating PDF in Java code , that time i not get actual Font. how i will get.

  7. syndarin says:

    Thanks a lot! I had a big problem with cyrillic font in JR and now it is solved.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: