7/06/2014

"Could not load the following font" solved: Jasper Reports + Grails adventures

Jasper + Grails/Java


Recently, I've been struggling with making this couple work together. JasperReports might be very useful when it comes to rendering reports out of your data, it can read your database and transform it into tables and charts. It sounds very nice in theory, but when your client wants to not only see the data itself, but also he wants to see it looking good then you're in trouble. Mine did, and I had some hard time setting it all up.

At the time I was working on it, there was a Jasper plugin for Grails version 1.6 which used some ancient version of JasperReports library and hence it was not compatible with my report I created in JasperSoft Studio 5.5.0. I had to build 1.7-SNAPSHOT version of the plugin to fix this. Luckily, couple days later plugin got updated, and I could use the officially released one (currently, version 1.8 is out).

The battle rages on

Nevertheless, even though I had it all theoretically compatible, I still couldn't render any of my reports. The problem now were... the fonts. My client wanted the reports to look nice, there were designs for it and they even wanted it to be printable as an advertising materials. It meant that reports should share look-and-feel with the corresponding website, including graphics and fonts. And this is where the real battle begun...

Font not found, font not available

My website, and my reports created in JasperSoft Studio have been using Ubuntu font family and it all worked fine there, but when trying to render the same report through Grails web application, it said that:

net.sf.jasperreports.engine.util.JRFontNotFoundException:
Font 'Ubuntu'is not available to the JVM.
See the Javadoc for more details.


First I tried to add the font to my JVM in very brutal low-level way - by adding to JavaHome/lib/fontconfig.properties but it didn't help much. After a while of stack-overflowing I found that I have to create a special .jar file with definition of my fonts for the reports, and that easiest way to do it is using iReports, so I did. It helped a little because now I was able to render my reports in all the other file formats, however only .pptx were actually usable, the rest didn't look as I expected. Pdfs though still didn't cooperate but the error message now has changed a bit.

Could not load the following font

I stuck with new error message like this:

Class 
    net.sf.jasperreports.engine.JRRuntimeException 
Message 
    Could not load the following font : pdfFontName : Ubuntu pdfEncoding : UTF-8 isPdfEmbedded : false

I couldn't explain it because the same report was being rendered fine in iReports and JasperSoft Studio, and also it worked fine in Grails when rendered to .pptx format, but not the bloody PDF. After some time of debugging guts of jasper's rendering engine and changing random things in order to find any clue, I started to wonder where does it get the pdfEncoding : UTF-8 property from and that was a milestone. It turned out that jasper uses three system properties:

net.sf.jasperreports.default.pdf.font.name
net.sf.jasperreports.default.pdf.encoding
net.sf.jasperreports.default.pdf.embedded

I started digging through JasperSoft Studio's options and I found that it sets some values to them, and that was a silver bullet.

Helvetica to the rescue

I don't know why, but this default.pdf.font.name by default was set to Helvetica. Even though I don't have a font like this anywhere in my OS (Windows 8), neither do I have it anywhere on my VPS (Ubuntu), it made the whole thing work.

These four lines of code placed in my BootStrap.groovy worked like a charm and now I was finally happy to see my reports in a pdf file.


No comments:

Post a Comment