How to configure BIRT Report Engine to load fonts directly from the classpath?
Asked Answered
T

2

6

I am writing a Java application that uses BIRT to produce reports. I want to package custom fonts in a jar file and be able embed them in PDF reports.

I could extract fonts to the file system first and then point BIRT to the file system locations, but I wonder whether it is possible to configure BIRT to load fonts directly from the classpath?

Topography answered 11/4, 2014 at 20:9 Comment(0)
T
13

I consulted the source code of BIRT and found that it is impossible to configure BIRT to register embeddable fonts from the classpath. BIRT registers fonts by the paths specified in fontsConfig.xml. It uses iText's FontFactory. Surprisingly, FontFactory itself can register fonts from the classpath. But the developers of BIRT probably don't know about this feature, so BIRT don't register any font that is not on the file system, i.e. when File#exists() returns false.

Fortunately, FontFactory.register() is a static method, so there is a workaround: we can register fonts ourselves bypassing BIRT. We can do just the following before initializing BIRT:

FontFactory.register("/com/example/fonts/font1.ttf");
FontFactory.register("/com/example/fonts/font2.ttf");

I tried this, and fonts are correctly embedded in the PDF output.

Topography answered 14/4, 2014 at 10:59 Comment(1)
Can you please provide the example, how we assign the registered font to the Birt label.Rabbitry
T
3

Many thanks @dened.

Using your answer, I found custom fonts can also be loaded as resources by:

  • Copying the fonts into the resources folder (eg src/main/resources for a Maven project)
  • In the BIRT engine initialisation code, register the fonts without specifying the path.
    Just use the filenames eg:\

import com.lowagie.text.FontFactory;
...
FontFactory.register("gillsans.ttf");
FontFactory.register("GILLUBCD.TTF");

The FontFactory will search for and find these files in the resources folder. This works for BIRT runtime 4.4.2 with iText v2.1.7.

This seems to be a good way to load non-standard fonts into the BIRT runtime engine so that they work in generated PDFs. If this approach is used, the fonts don't need to be added to the system fonts folder or the JRE/lib/fonts folder, and the fontsConfig.xml files in BIRT's jars don't need to be edited... Everything is contained within the application.

Trichoid answered 9/11, 2020 at 3:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.