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

守給你的承諾、 提交于 2019-12-01 03:22:20

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/23021711/how-to-configure-birt-report-engine-to-load-fonts-directly-from-the-classpath

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!