How to configure BIRT Report Engine to download fonts directly from the class path?

I am writing a Java application that uses BIRT to create reports. I want to pack custom fonts in a jar file and embed them in PDF reports.

I could first extract the fonts to the file system and then point BIRT to the location of the file system, but I wonder if it is possible to configure BIRT to load fonts directly from the class path?

+4
source share
1 answer

I turned to the BIRT source code and found that it was not possible to configure BIRT to register inline fonts from the class path. BIRT registers fonts in the paths specified in the fontsConfig.xml file. He uses iText FontFactory. Surprisingly, it FontFactorycan register fonts from the class path itself. But the developers of BIRT probably don’t know about this function, so BIRT does not register a font that is not in the file system, i.e. When File#exists()returns false.

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

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

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

+8
source

All Articles