rekowski.info David Rekowski's random stuff

Use embedded fonts in JasperReport PDFs

2013-01-11

How to add a custom fonts.jar file to a maven project in order to make the fonts available for embedding in PDF files created with JasperReports.

Keywords

java, jasper, jasperreports, pdf, jar, fonts, ttf

The problem

You can find a wealth of information on how to create a fonts.jar to be used with JasperReports but they all end with "make sure the jar is available in the classpath". In my case, I had some JUnit tests, which did not test a thing, but instead created PDFs using the project templates (I didn't want to have to step through all the application's processes in order to get an application state that allowed me to create the reports, since it involved separate server instances and data synchronization, which took way too long). The application used maven to build and run the tests, which meant I had to persuade maven to magically insert the fonts.jar to the test run classpath.

The quick and dirty solution

It worked to put the fonts.jar into the $JAVA_HOME/jre/lib/ext folder or the .ttf files into the $JAVA_HOME/jre/lib/fonts folder. Which is nice for testing, but doesn't help a bit if other developers need to work with that and you want to ship it in an application bundle.

So this article is all about how I convinced maven to take a local fonts.jar file, install it into the maven repository and then use it in the application at test time and runtime.

Step 1: Creating the fonts.jar file

Create a fonts.jar file, following the instructions provided by Mathias in 'Embedding fonts into PDF generated by JasperReports'. Come back when you did that, I'll have a cup of tea in the meantime.

Got your jar? Fine. Continue with...

Step 2: Making the fonts.jar available to maven

Ordinarily, you would have to set up a project with a pom.xml that provides the fonts.jar file. This is tedious and not necessary, since there is an App^H^H^H^H^H maven plugin for that: maven-install-plugin. Add the following section to your pom.xml and replace the placeholders with your values.

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-install-plugin</artifactId> <version>2.4</version> <executions> <execution> <phase>initialize</phase> <goals> <goal>install-file</goal> </goals> <configuration> <groupId>my.group.id</groupId> <artifactId>MyArtifactId</artifactId> <version>1.0</version> <packaging>jar</packaging> <file>/path/to/local/font.jar/in/project</file> </configuration> </execution> </executions> </plugin>

You can use JasperFonts as the artifactId. Note that you should increase the version, whenever you update the fonts.jar file.

You have to execute mvn initialize once in order to trigger the installation of the jar package into your maven repository.

Step 3: Adding the fonts packages as a dependency

In order for maven to include the fonts.jar in your build, you need to reference it as a dependency, like this:

<dependency> <groupId>my.group.id</groupId> <artifactId>MyArtifactId</artifactId> <version>1.0</version> <scope>compile</scope> </dependency>

If you think of it, it may be plain, basic maven knowledge, but it sure took me some time to get the idea.