在 fat jar 中编译应用程序时生成 jasper 报告的问题

Issue with generating jasper report when the app was compiled in fat jar

我已成功将 jasper 报告添加到我的 java 应用程序,并通过字体扩展添加了字体。当我 运行 我的应用程序在 intelij idea 中时,一切正常,报告使用选定的字体生成。但是,当我将所有内容编译成一个统一的(单个、fat 等)jar 文件,将其部署到 Wildfly 并尝试生成报告时,我收到以下错误:

2020-04-30 13:44:20,834 ERROR [net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper] (DefaultDispatcher-worker-1) Error loading font family: net.sf.jasperreports.engine.fonts.InvalidFontException: Error loading font "net/sf/jasperreports/fonts/icons/icons.ttf".
    at deployment.develop.war//net.sf.jasperreports.engine.fonts.SimpleFontFace.loadFont(SimpleFontFace.java:206)
    at deployment.develop.war//net.sf.jasperreports.engine.fonts.SimpleFontFace.setTtf(SimpleFontFace.java:167)
    at deployment.develop.war//net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.parseFontFace(SimpleFontExtensionHelper.java:390)
    at deployment.develop.war//net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.parseFontFamily(SimpleFontExtensionHelper.java:311)
    at deployment.develop.war//net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.parseFontExtensions(SimpleFontExtensionHelper.java:259)
    at deployment.develop.war//net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.loadFontExtensions(SimpleFontExtensionHelper.java:230)
    at deployment.develop.war//net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.loadFontExtensions(SimpleFontExtensionHelper.java:187)
    at deployment.develop.war//net.sf.jasperreports.engine.fonts.FontExtensionsRegistry.ensureFontExtensions(FontExtensionsRegistry.java:93)
    at deployment.develop.war//net.sf.jasperreports.engine.fonts.FontExtensionsRegistry.getExtensions(FontExtensionsRegistry.java:57)
    at deployment.develop.war//net.sf.jasperreports.extensions.DefaultExtensionsRegistry.getExtensions(DefaultExtensionsRegistry.java:134)
    at deployment.develop.war//net.sf.jasperreports.engine.util.JRStyledTextParser.<clinit>(JRStyledTextParser.java:86)
    at deployment.develop.war//net.sf.jasperreports.engine.fill.JRBaseFiller.<init>(JRBaseFiller.java:116)
    at deployment.develop.war//net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:79)
    at deployment.develop.war//net.sf.jasperreports.engine.fill.JRFiller.createBandReportFiller(JRFiller.java:251)
    at deployment.develop.war//net.sf.jasperreports.engine.fill.JRFiller.createReportFiller(JRFiller.java:272)
    at deployment.develop.war//net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:156)
    at deployment.develop.war//net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:145)
    at deployment.develop.war//net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:758)
    at deployment.develop.war//net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:739)
    at deployment.develop.war//net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:1061)
    at deployment.develop.war//ru.myproject.testspring.services.reports.ReportProvider.generateReportInternal(ReportProvider.kt:27)
    at deployment.develop.war//ru.myproject.testspring.services.reports.ReportProvider.generateReportInternal(ReportProvider.kt:10)
    at deployment.develop.war//ru.myproject.testspring.services.reports.AbstractReportProvider$generateReport.invokeSuspend(AbstractReportProvider.kt:52)
    at deployment.develop.war//kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at deployment.develop.war//kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
    at deployment.develop.war//kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
    at deployment.develop.war//kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738)
    at deployment.develop.war//kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
    at deployment.develop.war//kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
Caused by: java.io.IOException: Problem reading font data.
    at java.desktop/java.awt.Font.createFont0(Font.java:1177)
    at java.desktop/java.awt.Font.createFont(Font.java:1046)
    at deployment.develop.war//net.sf.jasperreports.engine.fonts.SimpleFontFace.loadFont(SimpleFontFace.java:198)
    ... 28 more
2020-04-30 13:44:20,826 ERROR [net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper] (DefaultDispatcher-worker-1) Error loading font family: net.sf.jasperreports.engine.fonts.InvalidFontException: Error loading font "fonts/TimesFont/times.ttf".
    at deployment.develop.war//net.sf.jasperreports.engine.fonts.SimpleFontFace.loadFont(SimpleFontFace.java:206)
    at deployment.develop.war//net.sf.jasperreports.engine.fonts.SimpleFontFace.setTtf(SimpleFontFace.java:167)
    at deployment.develop.war//net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.parseFontFace(SimpleFontExtensionHelper.java:379)
    at deployment.develop.war//net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.parseFontFamily(SimpleFontExtensionHelper.java:311)
    at deployment.develop.war//net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.parseFontExtensions(SimpleFontExtensionHelper.java:259)
    at deployment.develop.war//net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.loadFontExtensions(SimpleFontExtensionHelper.java:230)
    at deployment.develop.war//net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.loadFontExtensions(SimpleFontExtensionHelper.java:187)
    at deployment.develop.war//net.sf.jasperreports.engine.fonts.FontExtensionsRegistry.ensureFontExtensions(FontExtensionsRegistry.java:93)
    at deployment.develop.war//net.sf.jasperreports.engine.fonts.FontExtensionsRegistry.getExtensions(FontExtensionsRegistry.java:57)
    at deployment.develop.war//net.sf.jasperreports.extensions.DefaultExtensionsRegistry.getExtensions(DefaultExtensionsRegistry.java:134)
    at deployment.develop.war//net.sf.jasperreports.engine.util.JRStyledTextParser.<clinit>(JRStyledTextParser.java:86)
    at deployment.develop.war//net.sf.jasperreports.engine.fill.JRBaseFiller.<init>(JRBaseFiller.java:116)
    at deployment.develop.war//net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:79)
    at deployment.develop.war//net.sf.jasperreports.engine.fill.JRFiller.createBandReportFiller(JRFiller.java:251)
    at deployment.develop.war//net.sf.jasperreports.engine.fill.JRFiller.createReportFiller(JRFiller.java:272)
    at deployment.develop.war//net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:156)
    at deployment.develop.war//net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:145)
    at deployment.develop.war//net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:758)
    at deployment.develop.war//net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:739)
    at deployment.develop.war//net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:1061)
    at deployment.develop.war//ru.myproject.testspring.services.reports.ReportProvider.generateReportInternal(ReportProvider.kt:27)
    at deployment.develop.war//ru.myproject.testspring.services.reports.ReportProvider.generateReportInternal(ReportProvider.kt:10)
    at deployment.develop.war//ru.myproject.testspring.services.reports.AbstractReportProvider$generateReport.invokeSuspend(AbstractReportProvider.kt:52)
    at deployment.develop.war//kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at deployment.develop.war//kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
    at deployment.develop.war//kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
    at deployment.develop.war//kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738)
    at deployment.develop.war//kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
    at deployment.develop.war//kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
Caused by: java.io.IOException: Problem reading font data.
    at java.desktop/java.awt.Font.createFont0(Font.java:1177)
    at java.desktop/java.awt.Font.createFont(Font.java:1046)
    at deployment.develop.war//net.sf.jasperreports.engine.fonts.SimpleFontFace.loadFont(SimpleFontFace.java:198)
    ... 28 more

有我的字体扩展代码

jasperreports_extension.properties

net.sf.jasperreports.extension.registry.factory.fonts=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.ireportfamily1586778266566=fonts/fontsfamily1586778266566.xml

fontsfamily1586778266566.xml:

<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
    <fontFamily name="TimesFont">
        <normal><![CDATA[fonts/TimesFont/times.ttf]]></normal>
        <bold><![CDATA[fonts/TimesFont/timesbd.ttf]]></bold>
        <italic><![CDATA[fonts/TimesFont/timesi.ttf]]></italic>
        <boldItalic><![CDATA[fonts/TimesFont/timesbi.ttf]]></boldItalic>
        <pdfEncoding><![CDATA[Cp1251]]></pdfEncoding>
        <pdfEmbedded><![CDATA[true]]></pdfEmbedded>
        <exportFonts/>
    </fontFamily>
    <fontSet name="ReportFontSet">
        <family familyName="TimesFont" primary="true"/>
    </fontSet>
</fontFamilies>

我在 .war 文件 (WEB-INF\lib) 的 lib 文件夹中看到了我的字体扩展 jar。

我的环境:

jasperreports:6.12.2
Wildfly 19

P.S。 很奇怪,我从日志中收到以下错误:

Error loading font family: net.sf.jasperreports.engine.fonts.InvalidFontException: Error loading font "net/sf/jasperreports/fonts/icons/icons.ttf"

因为 icon.ttf 位于 jasperreports:6.12.2.jar

中的 jasper 默认字体文件夹中

我找到了这个问题的解决方案。我将 docker 容器与 WildFly 一起使用,在 docker 中我有 Linux,它没有为字体配置。我使用了以下命令:

输入:

docker exec -it -u 0 wildfly /bin/bash 其中 wildfly 是容器的名称

对于字体(它是解决方案)

sudo apt-get install fontconfig