Most folks should refer to my updated article on Fonts in JasperServer 3.7 instead of the article below. But I’m leaving the old article here for cases where someone needs to run the older version of JasperServer.
JasperServer 3.5.1 released
JasperServer Professional 3.5.1 from Jaspersoft will be released shortly. One of the more anticipated features for me will be the inclusion of a localized Chinese GUI. I got a sneak peek at it this morning. The menus and user interface are completely in Chinese (if I choose the appropriate locale). Hooray! But I ran one of the sample reports, and this is what I saw:
What’s wrong with this picture? The text that stands by itself is fine, but the text that gets rendered as part of the chart image doesn’t display correctly at all. How can this be? Everything is Java-based, so I shouldn’t have localization issues. The problem is that the report specifies Verdana as its font for nearly everything, but Verdana doesn’t include support for Chinese characters. (Interestingly, the text that displays well was also specified as Verdana. But the browser realizes the specified font doesn’t have the correct characters, so it automatically substitutes another font. It’s a useful feature, but it made understanding the underlying problem more difficult.) The solution is to select appropriate fonts.
Fixing the fonts
Fortunately it’s not any sort of JasperServer bug; it’s just an issue with the sample reports. Now that I see what’s causing the problem, it should be easy to fix:
- Update the report to use an appropriate font
- Deploy the font to JasperServer
Updating the report should be easy. Open iReport, connect to JasperServer, get the reports, set the font. But the devil is in the details. The sample report with the problem is/supermart/salesByMonth/SalesByMonthReport. I open it, find the chart, find the property “Legend font” and see that the current font is “Verdana”. I change this to “Arial Unicode MS”. I save the report back to JasperServer and see what it looks like now. No change. This isn’t be surprising because the server doesn’t have the font in its classpath anywhere.
I’ve never deployed a font to JasperServer before. So it took a little poking around in the documentation and the samples to see what was needed. The key concept is the need for JasperReports Font Extensions. The JasperReports Ultimate Guide has a good explanation and tutorial, so I was off to a good start. Three files are needed:
- The actual font file (This might be multiple files… but in my case it was just one.)
All of the files need to be in the Java classpath, so this is a good place to put them given that I’m using Apache Tomcat: …\Tomcat 5.5\webapps\jasperserver-pro-3.5\WEB-INF\classes
The name of this file is important. JasperReports knows to load the file based on its name. Its contents are simple. I copied it unmodified from the documented example:
This file can take any name; it just needs to match what is specified above. The contents are only minimally updated from the sample provided:
<?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="myFontFamily" class="net.sf.jasperreports.engine.fonts.SimpleFontFamily"> <property name="name" value="Arial Unicode MS"/> <property name="normal" value="fonts/ARIALUNI.TTF"/> <property name="pdfEncoding" value="Identity-H"/> <property name="pdfEmbedded" value="true"/> </bean> </beans>
I chose the font Arial Unicode MS because it includes Chinese characters, and it ships with MS Office. There are other downloadable fonts available for people that don’t have access to this one.
The font file(s)
In fonts.xml the font file is specified as fonts/ARIALUNI.TTF. I did this so I could put all of my fonts into a subdirectory under the classes directory. I’m using default locations for everything, so this was the copy command:
copy C:\WINDOWS\Fonts\ARIALUNI.TTF "C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\jasperserver-pro-3.5\WEB-INF\classes\fonts"
Enjoy the corrected reports
After restarting JasperServer, the report now looks like this:
The process is simple, but it’s not entirely intuitive. And you need to get things exactly right. Hopefully this will be an aid to anyone encountering localization issues with the charts (JFreeChart) in JasperServer.
I described the step-by-step process only for the chart legend. But there are a few more properties that need to be set for anyone that wants the complete demo dashboard to display correctly.
In the report /supermart/salesByMonth/SalesByMonthReport you need to set both the “Legend font” and the “Category Axis Tick Label Font”. There are two charts (one on top of the other), so be sure to set both properties for both charts.
In the report /supermart/revenueAndProfit/SalesGaugesReport it’s the “Value Font” that needs to be changed. This property should be set for all four dial gauges.