| 31 May 2010 |
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 cabextractwget http://corefonts.sourceforge.net/msttcorefonts-2.0-1.specrpmbuild -bb msttcorefonts-2.0-1.spec- Find out where file
msttcorefonts-2.0-1.noarch.rpmis located and dorpm -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.propertieswith the following contents (copied from the jasperreports demo directory):
-
net.sf.jasperreports.extension.registry.factory.fonts=net.sf.jasperreports.extensions.SpringExtensionsRegistryFactory
-
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.xmlwith the following contents:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<beans xmlns="http://www.springframework.org/schema/beans"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation="http://www.springframework.org/schema/beans
-
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
-
<bean id="CourierNewFontFamily" class="net.sf.jasperreports.engine.fonts.SimpleFontFamily">
-
<property name="name" value="Courier New"/>
-
<property name="normal" value="/usr/share/fonts/msttcorefonts/cour.ttf"/>
-
<property name="bold" value="/usr/share/fonts/msttcorefonts/courbd.ttf"/>
-
<property name="italic" value="/usr/share/fonts/msttcorefonts/couri.ttf"/>
-
<property name="boldItalic" value="/usr/share/fonts/msttcorefonts/courbi.ttf"/>
-
<property name="pdfEncoding" value="Identity-H"/>
-
<property name="pdfEmbedded" value="true"/>
-
</bean>
-
</beans>
A few remarks:
- Note that the
nameproperty should reflect (exactly) the name of the font as used in your JasperReport. I used the nameCourier New, because that's what my exception said. - Properties
normal,bold, etcetera, refer to the location of the font. By doing asudo find / -name *.ttfon our LInux machine, we found out the location of the TTF font. - When using exotic fonts, the
pdfEmbeddedproperty 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.


1 comment to 'Solving the JasperServer TrueType Font Issue'
23 July 2010
Thanks!