JasperReport Font Extensions

From iDempiere en
Jump to navigation Jump to search

This page is about using fonts in iDempiere reports using the JasperReports report engine. I did this with my own replacement Japser plugin (JasperReportsFreiBier) but some things written here may also count for the old standard plugin. (If you are interested in the old standard plugin you should be aware that there is an example font plugin in the iDempiere source trunk.)


What is the problem?

If you create your report with iReport it gives you the possibility to use all fonts that are installed on your local system. But that does not mean that these fonts are available on your server or on a swing client's machine. It does not even guarantee that the created pdf document can be shown on these other machines.

For the first part there is a way to create a so called "font extension" for JapserReports. Our goal is to create an iDempiere plugin to be added to an iDempiere installation which includes the needed font. Another goal is to have a standard plugin with a useful collection of fonts so most users would not need to do it themself.


find a good font

There are some places on the internet to find good fonts. I first use a search engine and then find one of the specialized font search engines. You can also use a page where all debian fonts are shown: http://fonts.debian.net. Personally I prefer fonts that are available in debian linux. But in some cases I found a font through one of the font search pages, went to the font's home page and only then found out that it has a debian package.

At the end we just need some *.ttf files. So it is possible to just download these. For a perfect result it would be good if we have different font files for bold and italic versions (if not then they are calculated from the base font).

For my example I found a font via http://www.fontspace.com, found the home page at http://mplus-fonts.sourceforge.jp/mplus-outline-fonts/design/index-en.html and found the debian package fonts-mplus.


create a font extension

I first install the debian package. Then I look where the font files are.

 tbayen@laptop:~$ sudo aptitude install fonts-mplus
 ...
 tbayen@laptop:~$ dpkg -L fonts-mplus
 /.
 /usr
 /usr/share
 /usr/share/fonts
 /usr/share/fonts/truetype
 /usr/share/fonts/truetype/mplus
 /usr/share/fonts/truetype/mplus/mplus-1mn-regular.ttf
 /usr/share/fonts/truetype/mplus/mplus-2m-regular.ttf
 /usr/share/fonts/truetype/mplus/mplus-2c-regular.ttf
 /usr/share/fonts/truetype/mplus/mplus-2m-medium.ttf
 /usr/share/fonts/truetype/mplus/mplus-2p-thin.ttf
 /usr/share/fonts/truetype/mplus/mplus-2c-light.ttf
 ...

After that I open JasperStudio (This should also work with the iReport application). I open the preferences and open the Tab for "Jaspersoft Studio" -> "Fonts". There I press the "add" button.

In the font definition window I first set the name of the Font to "MPlus M1". Then I go to every tab ("Normal", "Bold", etc.) and create an entry for the font files I have. Every tab needs only one filename entry. In my case I set the TrueType filename on the tabs "Normal" and "Bold". I let the italic tabs alone because I have no italic font files. On every used font file tab I check "Embed this font in PDF document". That makes the resulting PDF file a bit bigger but it guarantees that the file looks the same everywhere. (btw: Only the used characters are embedded, not the whole font. Don't be afraid!) You can end this window by using the "Finish" button. (Some pages are available through "Next" but we I didn't need them for my case.)

Back in the Preferences Window you can select the new created font and press "Export". With that you create a JapserReports font extension file (ending with *.jar).


using the selected fonts in JasperReports studio or iReport

You can use a font for every text field in JasperReports Studio in the field preferences. The combobox to choose the font contains (in my case) a very lot of fonts. Most of them are not prefered to use. The fonts that are installed as font extensions are at the very top of the list. You should always use one of them.

using Font Styles

You can create a style to to a collection of some output configurations that can be applies to many different fields. You can create a style for using a font and use that style instead. That allows a later change of the used font with one single setting. See the JasperReports documentation if you have more questions.

using Font Style Templates

You can even use Font Style Templates. That is a file with font (and other style) definitions that can be included in your report. This allows to use one set of font and style definitions to define a whole set of reports.

The actual JasperReports version in iDempiere 2.0 does not allow Style Templates (they are rather new). If you use JasperReportsFreibier that is no problem.


creating an iDempiere plugin

You can use my standard plugin de.bayen.freibier.report.fontsfragment as an example point to extend or you can create an own plugin for that. Create a new plugin project and declare it as a Fragment. A Fragment is an extension to one single parent plugin - the so-called "host bundle". Our host bundle is de.bayen.freibier.report.libraries. More is not needed to set up the plugin.

Then I created a directory "lib" and copied all my font extension jar-files into it. In the manifest definition editor I added them all to the "binary build" set (in the "build" tab).

That's it. Create the bundle file with Menu "Export..." -> "Deployable Plug-ins and Fragments", set some name values and the new plugin is ready to test with your report.


useful links