问题描述
我正在编写一个使用BIRT生成报告的Java应用程序。我想在jar文件中打包自定义字体,并能将它们嵌入到PDF报告中。
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.
我可以首先将字体提取到文件系统,然后将BIRT指向文件系统位置,但我想知道是否可以配置BIRT直接从类路径加载字体?
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?
推荐答案
我咨询了源代码BIRT的代码,发现不可能配置BIRT来从类路径注册可嵌入字体。 BIRT通过fontsConfig.xml中指定的路径注册字体。它使用iText的 FontFactory
。令人惊讶的是, FontFactory
本身可以从类路径中注册字体。但是BIRT的开发人员可能不知道这个功能,所以BIRT不会注册任何不在文件系统上的字体,即 File#exists()
返回 false
。
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
.
幸运的是, FontFactory.register()
是一个静态方法,所以有一个解决方法:我们可以绕过BIRT自己注册字体。在初始化BIRT之前我们可以执行以下操作:
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");
我试过这个,字体正确嵌入PDF输出中。
I tried this, and fonts are correctly embedded in the PDF output.
这篇关于如何配置BIRT Report Engine直接从类路径加载字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!