Currently I'm finalizing a reporting solution based on JasperReports. The solution includes a JasperServer instance running on Linux. After deploying some initial reports to JasperServer using iReports I noticed that the TrueType fonts used when designing the reports (on Windows) did not render correctly. In fact, they did not render at all ... JasperReports decided to use a default font instead.

From the JasperServer log it appeared that the font used in the report could not be found: "JRFontNotFoundException: Font 'Courier New' is not available to the JVM"

Using the following steps I fixed my font-issue (note: I'm a Linux kind-a guy, please translate the following steps to your own environment):

First, we'll need to install the Microsoft TrueType fonts. Excellent reference here: http://corefonts.sourceforge.net. In short, the following steps apply:

  • sudo yum install rpmbuild cabextract
  • wget http://corefonts.sourceforge.net/msttcorefonts-2.0-1.spec
  • rpmbuild -bb msttcorefonts-2.0-1.spec
  • Find out where file msttcorefonts-2.0-1.noarch.rpm is located and do rpm -ivh msttcorefonts-2.0-1.noarch.rpm

Now the TrueType fonts are installed (use the fc-list command to display a list of fonts installed), we need to configure JasperServer so they can be used:

  • Open a SSH session to the JasperServer machine and goto directory $CATALINA_HOME/shared/classes. I use this directory because its contents is by default on the classpath of Apache Tomcat (and that's exactly where we need the following files) and its non-intrusive with regard to the JasperServer installation;
  • Create a file called jasperreports_extension.properties with the following contents (copied from the jasperreports demo directory):
CODE:
  1. net.sf.jasperreports.extension.registry.factory.fonts=net.sf.jasperreports.extensions.SpringExtensionsRegistryFactory
  2. net.sf.jasperreports.extension.fonts.spring.beans.resource=fonts.xml


The contents of this file has been copied from the JasperServer documentation. Two things are important: the name of file (JasperServer expects it to be named like this and will automagically load it) and the reference to fonts.xml

  • Create a file called fonts.xml with the following contents:
XML:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  5.        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
  6.  <bean id="CourierNewFontFamily" class="net.sf.jasperreports.engine.fonts.SimpleFontFamily">
  7.    <property name="name" value="Courier New"/>
  8.    <property name="normal" value="/usr/share/fonts/msttcorefonts/cour.ttf"/>
  9.    <property name="bold" value="/usr/share/fonts/msttcorefonts/courbd.ttf"/>
  10.    <property name="italic" value="/usr/share/fonts/msttcorefonts/couri.ttf"/>
  11.    <property name="boldItalic" value="/usr/share/fonts/msttcorefonts/courbi.ttf"/>
  12.    <property name="pdfEncoding" value="Identity-H"/>
  13.    <property name="pdfEmbedded" value="true"/>
  14.  </bean>
  15. </beans>



A few remarks:

  • Note that the name property should reflect (exactly) the name of the font as used in your JasperReport. I used the name Courier New, because that's what my exception said.
  • Properties normal, bold, etcetera, refer to the location of the font. By doing a sudo find / -name *.ttf on our LInux machine, we found out the location of the TTF font.
  • When using exotic fonts, the pdfEmbedded property can be used to embed the font into the PDF (actually, the property name itself gave that away). Your PDF will be somewhat larger in size, but the result will be readable.

For a full reference of the possible properties, just check the JavaDoc for SimpleFontFamily. Just remember that each FontFamily gets its own bean.


Now both files are in place, all we need to do now is restart JasperServer (read: Apache Tomcat). Once Tomcat has been restarted, run your report again and the fonts you were previously missing will be there.