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