Use custom fonts in JasperReports PDF exporter

I was using custom font in a customer report embedded in the web application. The font (available in the operating system) was displayed correctly in HTML, RTF and Excel export. However, in the generated PDF text elements were displayed using Arial font.

The solution is to wrap required fonts as JasperReports Font Extensions. Font TTF files can be included in the same project generating Jasper reports or, a more elegant solution, in a separate JAR file.

I found an interesting blog post Fonts in JasperServer about providing custom fonts. The author is configuring the font extensions using Spring. However, you can do it in a simpler way.

First, install fonts in iReport and let create it the required configuration. In iReport, go to Tools -> Options, click on iReport group and go to Fonts tab. Here are some screenshots from the installation wizard:

iReport creates font extensions configuration and copies the recently installed font into the iReport installation directory/ireport/fonts directory (C:\Program Files (x86)\iReport-3.7.2\ireport\fonts in my case). You should find the following files there:

  • jasperreports_extension.properties – it configures net.sf.jasperreports.extensions.ExtensionsRegistryFactory as net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory and points to the font family configuration:
  • net.sf.jasperreports.extension.registry.factory.fonts=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
    net.sf.jasperreports.extension.simple.font.families.ireport=irfonts.xml
    
  • irfonts.xml – font family configuration:
  • 
    <fontfamilies>
       <fontfamily name="Calibri">
           <normal><!--[CDATA[calibri.ttf]]--></normal>
           <bold><!--[CDATA[calibrib.ttf]]--></bold>
           <italic><!--[CDATA[calibrii.ttf]]--></italic>
           <bolditalic><!--[CDATA[calibriz.ttf]]--></bolditalic>
           <pdfembedded><!--[CDATA[true]]--></pdfembedded>
       </fontfamily>
    </fontfamilies>
    
  • one or more TTF font files

All files found there that need to be packed in the JAR; let’s use Maven to create it:

  1. Create the Maven project directory (e.g. jasperreports-font-extensions) with src/main/resources subdirectory.
  2. Copy all files from iReport installation directory/ireport/fonts to Maven font extensions project directory/src/main/resources.
  3. Add pom.xml:
  4. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelversion>4.0.0</modelversion>
        <groupid>net.gryszko</groupid>
        <artifactid>jasperreports-font-extensions</artifactid>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>
    </project>
    
  5. Build the JAR and install it in the local Maven repository:
  6. mvn install

The final step is to reference the newly created JAR as dependency in your reports project:

<project>
    ...
    <dependencies>
        <dependency>
            <groupid>net.gryszko</groupid>
            <artifactid>jasperreports-font-extensions</artifactid>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        ...
    </dependencies>
    ...
</project>

After that, the fonts contained it JAR will be available to JasperReports when generating a PDF report.

Update:
Just before publishing my post, Matt Dahlmann, author of the published an update of his previous article mentioned in this post. Check Jaspersoft v3 Font Extensions for the more actual version.

Advertisements

8 thoughts on “Use custom fonts in JasperReports PDF exporter

  1. Good article. I don’t know maven well, so those details are interesting for me. Thanks for the mention of my article. Rather than link to my article of a year ago, it might be better to link to the one published just four days before yours. It parallels your idea of using iReport’s new abilities to create the font extensions, which is indeed much easier than doing things manually. But then it focuses on JasperServer rather than on JasperReports. http://mdahlman.wordpress.com/2010/05/28/jaspersoft-v3-7-font-extensions/

  2. In case anybody is facing a more mundane question of e.g. bold text not displaying properly in generated pdfs paste this into your pom:

    net.sf.jasperreports
    jasperreports-fonts
    4.0.0

    I have decided to post it here because I read on many forums that people are struggling with this trifle and I got a bit frustrated over it too. Obviously it is only vaguely related to a more advanced topic you are describing in your post but I have landed here looking for a solution to my problem and your post gave me a hint. Thanks.

  3. I created the jar with the fonts but when I went up the application on a linux server, can not generate a report. It gives the following error:

    12:47:07,248 ERROR [stderr] (http–0.0.0.0-8443-2) Caused by: net.sf.jasperreports.engine.JRRuntimeException: java.io.IOException: Problem reading font data.
    12:47:07,249 ERROR [stderr] (http–0.0.0.0-8443-2) at net.sf.jasperreports.engine.fonts.SimpleFontFace.(SimpleFontFace.java:109)
    12:47:07,250 ERROR [stderr] (http–0.0.0.0-8443-2) at net.sf.jasperreports.engine.fonts.SimpleFontFace.(SimpleFontFace.java:129)
    12:47:07,250 ERROR [stderr] (http–0.0.0.0-8443-2) at net.sf.jasperreports.engine.fonts.SimpleFontFace.getInstance(SimpleFontFace.java:68)
    12:47:07,251 ERROR [stderr] (http–0.0.0.0-8443-2) at net.sf.jasperreports.engine.fonts.SimpleFontFamily.setNormal(SimpleFontFamily.java:99)
    12:47:07,251 ERROR [stderr] (http–0.0.0.0-8443-2) at net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.parseFontFamily(SimpleFontExtensionHelper.java:261)
    12:47:07,252 ERROR [stderr] (http–0.0.0.0-8443-2) at net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.parseFontFamilies(SimpleFontExtensionHelper.java:232)
    12:47:07,253 ERROR [stderr] (http–0.0.0.0-8443-2) at net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.loadFontFamilies(SimpleFontExtensionHelper.java:193)
    12:47:07,256 ERROR [stderr] (http–0.0.0.0-8443-2) at net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.loadFontFamilies(SimpleFontExtensionHelper.java:162)
    12:47:07,256 ERROR [stderr] (http–0.0.0.0-8443-2) at net.sf.jasperreports.engine.fonts.FontExtensionsRegistry.getExtensions(FontExtensionsRegistry.java:56)
    12:47:07,257 ERROR [stderr] (http–0.0.0.0-8443-2) at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.getExtensions(DefaultExtensionsRegistry.java:110)
    12:47:07,261 ERROR [stderr] (http–0.0.0.0-8443-2) at net.sf.jasperreports.engine.util.JRStyledTextParser.(JRStyledTextParser.java:83)
    12:47:07,261 ERROR [stderr] (http–0.0.0.0-8443-2) … 36 more

  4. Absolutely great post. I wasted hours with half-baked and deprecated explanations, until I came here.

    Thanks!

Comments are closed.