Adding Custom Fonts to Jasper Reports


Jasper Reports support by default limited fonts in the iReport Designer and the Jasper Reports font jar file. If you want to add other fonts in the iReport designer and to the Jasper Reports Jar file. Here are the steps to add fonts to the Jasper Reports PDF report, in this tutorial I am going to use Calibri font.
The main font settings available in JasperReports are:
  • fontName – the font name, which can be the name of a physical font or a logical one.
  • size – the size of the font measured in points.
  • isBold – flag specifying if a bold font is required.
  • isItalic – flag specifying if an italic font is required.
  • isUnderline – flag specifying if the underline text decoration is required.
  • isStrikeThrough – flag specifying if the strikethrough text decoration is required.
  • pdfFontName – the name of an equivalent PDF font required by the iText library when exporting documents to PDF format.
  • pdfEncoding – the equivalent PDF character encoding, also required by the iText library.
  • isPdfEmbedded – flag that specifies whether the font should be embedded into the document itself.
Installing Font in iReport Designer:
  1. iReports select “Font”
  2. Right side click on “Install Font”
  3. Browse to CALIBRI.TTF and click on “Next” button
  4. Then select CALIBRIB, CALIBRII, CALIBRIZ in Bold, Italic and Bold Italic and Click “Finish” button.
Now the Calibri font is available in iReport Designer. But when we generate the report from the java application, the font is not embed to the PDF report, as the font is not part of the Jasper font jar.
Adding Font as part of jasperreports-fonts-X.Y.Z.jar, here the x, y and z are version of the Jasper Reports.
  1. Download and extract jasper font jar
    • Download and extract jasperreports-fonts-X.Y.Z.jar ( I am using jasperreports-fonts-4.5.1).
  2. Edit jasperreports_extension.properties
    At the root of the jasperreports-fonts-4.5.1.jar you should see jasperreports_extension.properties
    Edit this file you will see the following:
    net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperreports.engine.fonts.Simp…
    net.sf.jasperreports.extension.simple.font.families.dejavu=net/sf/jasperreports/fonts/fonts.xml
    Add the following line at the bottom:
    net.sf.jasperreports.extension.simple.font.families.calibri=net/sf/jasperreports/fonts/fonts.xml
  3. Create a Calibri folder in “net\sf\jasperreports\fonts\”
    Here it is “net\sf\jasperreports\fonts\calibri”. Copy the relevant fonts that you would like add into this folder.
    • standard fonts – CALIBRI
    • bold fonts – CALIBRIB
    • italic fonts – CALIBRII
    • bold italic fonts – CALIBRIZ
    After copying these files, the directory would look like this,
    net/sf/jasperreports/fonts/calibri/CALIBRI.ttf
    net/sf/jasperreports/fonts/calibri/CALIBRIB.ttf
    net/sf/jasperreports/fonts/calibri/CALIBRII.ttf
    net/sf/jasperreports/fonts/calibri/CALIBRIZ.ttf
  4. Now edit the fonts.xml , in the “net/sf/jasperreports/fonts/” directory.
    And add the following Font Family in this file.


<fontFamily name="Calibri">

   <normal>net/sf/jasperreports/fonts/calibri/CALIBRI.ttf</normal>

   <bold>net/sf/jasperreports/fonts/calibri/CALIBRIB.ttf</bold>

   <italic>net/sf/jasperreports/fonts/calibri/CALIBRII.ttf</italic>

   <boldItalic>net/sf/jasperreports/fonts/calibri/CALIBRIZ.ttf</boldItalic>

   <pdfEncoding>Identity-H</pdfEncoding>

   <pdfEmbedded>true</pdfEmbedded>

   <exportFonts>

       <export key="net.sf.jasperreports.html">Calibri</export>

       <export key="net.sf.jasperreports.xhtml">Calibri</export>

   </exportFonts>

</fontFamily>

Prepare the jar file and copy it into the WEB-INF\lib folder, now the reports generated with Java application will embed with Calibri font as part of the PDF.

No comments

Powered by Blogger.