java JasperReports:如何添加不在应用程序类路径中的字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3151562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JasperReports: How to add font not in the application classpath
提问by acostache
I am trying to use a font, that is not installed on my local OS, with a JasperReports. The jasper report uses in this way:
我正在尝试使用一种字体,该字体未安装在我的本地操作系统上,带有JasperReports. jasper 报告是这样使用的:
<textField>
<reportElement x="0" y="0" width="137" height="20"/>
<textElement>
<font fontName="Corbel" size="12"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{something}]]></textFieldExpression>
</textField>
The font named Corbelwas exported as a font extension (using iReport) and is contained in file (Corbel.jar), in folder, on my system.
I add this extension to the classpath of the application, using a code as follows:
命名的字体Corbel作为字体扩展名(使用 iReport)导出,并包含在我系统上的文件夹中的文件 (Corbel.jar) 中。我将此扩展添加到应用程序的类路径中,使用如下代码:
ClassLoader cl = new URLClassLoader(new URL[] {new URL("file:///D:/path/to/Corbel_jar_folder/")});
param = new HashMap();
param.put(JRParameter.REPORT_CLASS_LOADER, cl);
jasperReport = JasperCompileManager.compileReport("d:/path/to/Report_with_Corbel_font.jrxml");
jasperPrint = JasperFillManager.fillReport(jasperReport, param, new JREmptyDataSource());
After the report is filled, I export it using a JRPdfExporter. However, in the result pdf file, the element does not have the Corbelfont applied. I have not included the pdf exporting, because I think that the problem is somewhere with the filling.
I have searched and read numerous posts and questions related to using/including fonts (i.e.font extensions) in JasperReports; still I am not aware of where the mistake or the problem resides.
Any help would be greatly appreciated.
报告填写完毕后,我使用JRPdfExporter. 但是,在结果 pdf 文件中,元素没有Corbel应用字体。我没有包括pdf导出,因为我认为问题出在填充的某个地方。我已经搜索并阅读了许多与在 中使用/包括字体(即字体扩展)相关的帖子和问题JasperReports;我仍然不知道错误或问题出在哪里。任何帮助将不胜感激。
Thank you in advance! (sorry for the bad code indentation and I hope I have included enough details)
先感谢您!(抱歉代码缩进不好,我希望我已经包含了足够的细节)
回答by acostache
The problem was that the loading of font extensions jars is done from the thread context classloader and from the JRParameter.REPORT_CLASS_LOADER, nor the JRExporterParameter.CLASS_LOADER.
问题是,字体扩展罐的装载量是线程上下文的classloader和从完成的JRParameter.REPORT_CLASS_LOADER,也不是JRExporterParameter.CLASS_LOADER。
Therefore, in my case, the current (initial) thread classloader had to be saved, the we had to do something like Thread.currentThread().setContextClassLoader(cl), where clwas the context of the JasperReportsbased application and then the thread context classloader was reverted to the original one.
因此,在我的情况下,必须保存当前(初始)线程类加载器,我们必须执行类似的操作Thread.currentThread().setContextClassLoader(cl),其中基于应用程序cl的上下文,JasperReports然后线程上下文类加载器恢复为原始的。
The question has been answered and details are available here.
问题已得到解答,详情请点击此处。
I hope this answer will help others facing similar (font) issues.
我希望这个答案能帮助其他面临类似(字体)问题的人。
回答by Dhanushka Lahiru
Jasper Reports: Adding Custom Fonts
Jasper Reports:添加自定义字体
- Get the font files you want to use ( TTF, EOT, SVG or WOFF (OTF converted to TTF works as well))
Add the fonts to Jaspersoft Studio (or iReport) using the UI. (Window->Preferences->Jaspersoft Studio->Fonts)
You need to give details as in the image. Specially PDF Font Name field is misleading. We need to give the path here for this font in font-extention.jar which we create in next step.
Now you can preview the font in your report.
Export the fonts to a fonts-extention.jar file for use in your application. Select and Export required fonts. (Window->Preferences->Jaspersoft Studio->Fonts->Select and Export)
Take care of integrating the jar into your build. If you are using maven for your project you can integrate as follows.
- 获取您要使用的字体文件(TTF、EOT、SVG 或 WOFF(OTF 转换为 TTF 也可以))
使用 UI 将字体添加到 Jaspersoft Studio(或 iReport)。(窗口->首选项->Jaspersoft Studio->字体)
您需要提供图片中的详细信息。特别是 PDF 字体名称字段具有误导性。我们需要在下一步创建的 font-extention.jar 中为此字体提供路径。
现在您可以预览报告中的字体。
将字体导出到 fonts-extention.jar 文件以在您的应用程序中使用。选择并导出所需的字体。(窗口->首选项->Jaspersoft Studio->字体->选择和导出)
注意将 jar 集成到您的构建中。如果您在项目中使用 maven,您可以按如下方式进行集成。
<dependency>
<groupId>local.jasperFont</groupId>
<artifactId>local.jasperFont</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/resources/reports/fonts-extension.jar</systemPath>
</dependency>
And add the fonts-extension.jar in project resources/reports directory.
并在项目资源/报告目录中添加fonts-extension.jar。
回答by vsingh
We could use Google Noto fonts with Jasper with these steps
我们可以通过以下步骤在 Jasper 中使用 Google Noto 字体
Packaging the NotoSans family into a jar file as instructed in Jasper documentation.
Uploaded this jar to our maven repo
- Define dependency in pom
按照 Jasper 文档中的说明将 NotoSans 系列打包到 jar 文件中。
将这个 jar 上传到我们的 maven repo
- 在 pom 中定义依赖
<dependency> <groupId>com.crunchtime.shared.fonts</groupId> <artifactId>notosans</artifactId> <version>1.0</version> </dependency>
<dependency> <groupId>com.crunchtime.shared.fonts</groupId> <artifactId>notosans</artifactId> <version>1.0</version> </dependency>
- And then use in our. I think this is the hardest part but this is how we did it
- 然后在我们的. 我认为这是最难的部分,但这就是我们做到的
private static final String DEFAULT_PDF_STYLE = "defaultPdf";
private static final String DEFAULT_PDF_STYLE = "defaultPdf";
private static final String DEFAULT_FONT_FAMILY = "NotoSans";
private static final String DEFAULT_FONT_FAMILY = "NotoSans";
private static final String CJK_FONT = "fonts/NotoSansCJKtc-Regular.otf";
private static final String CJK_FONT = "fonts/NotoSansCJKtc-Regular.otf";
if (language-other-than-English) {
defaultStyle.setPdfFontName(CJK_FONT);
} else {
defaultStyle.setFontName(DEFAULT_FONT_FAMILY);
}
We had been seeing this null pointer exception but the key was to set explicitly set the font for the pdf
我们一直看到这个空指针异常,但关键是明确设置 pdf 的字体


