【问题标题】:Issue with loading font using JaspersReport使用 JaspersReport 加载字体的问题
【发布时间】:2016-12-02 17:34:45
【问题描述】:

我在加载自己的字体时遇到问题。

我已经使用 iReports 和 tutorial 导出了字体。

这导致了文件:

/resources/jasperreports_extension.properties
/resources/fonts/fontsfamily1480698239543.xml
/resources/fonts/Lato-Bold.ttf
/resources/fonts/Lato-BoldItalic.ttf
/resources/fonts/Lato-Italic.ttf
/resources/fonts/Lato-Regular.ttf

在哪里jasperreports_extension.properties:

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

还有fontsfamily1480698239543.xml:

<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
   <fontFamily name="Lato">
       <normal><![CDATA[fonts/Lato-Regular.ttf]]></normal>
       <bold><![CDATA[fonts/Lato-Bold.ttf]]></bold>
       <italic><![CDATA[fonts/Lato-Italic.ttf]]></italic>
       <boldItalic><![CDATA[fonts/Lato-BoldItalic.ttf]]></boldItalic>
       <pdfEncoding><![CDATA[Cp1250]]></pdfEncoding>
       <pdfEmbedded><![CDATA[true]]></pdfEmbedded>
   </fontFamily>
</fontFamilies>

在我的 report.jrxml 中,我使用以下文本:

        <staticText>
            <reportElement style="Colored" x="119" y="93" width="161" height="15" uuid="724f085b-cb64-4d11-ac64-dc23e6f04553"/>
            <textElement markup="none">
                <font fontName="Lato" size="10" pdfEncoding="Cp1250" isPdfEmbedded="true"/>
            </textElement>
            <text><![CDATA[Whatever]]></text>
        </staticText>

        <staticText>
            <reportElement x="198" y="200" width="154" height="24" uuid="95b310d8-19bf-4485-840f-f3f5076b225e"/>
            <textElement markup="none">
                <font size="14" isItalic="false" isUnderline="false" pdfFontName="Lato" pdfEncoding="Cp1250" isPdfEmbedded="true"/>
            </textElement>
            <text><![CDATA[Address:]]></text>
        </staticText>

当我尝试生成文件时,出现以下错误:

net.sf.jasperreports.engine.JRRuntimeException: Could not load the following font: 
pdfFontName: Lato
pdfEncoding: Cp1250
isPdfEmbedded : true] with root cause
net.sf.jasperreports.engine.JRRuntimeException: Could not load the following font: 
pdfFontName: Lato
pdfEncoding: Cp1250
isPdfEmbedded : true
    at net.sf.jasperreports.engine.export.JRPdfExporter.getFont(JRPdfExporter.java:2176) ~[jasperreports-6.1.0.jar:6.1.0]

我做错了什么?

更新

我已根据 AlexK 和 dada67 的评论应用了更改,但这仍然没有更改 PDF 中生成的字体。但是,我可以在 iReports 应用程序中看到字体。我现在使用:

        <staticText>
            <reportElement x="198" y="200" width="154" height="24" uuid="95b310d8-19bf-4485-840f-f3f5076b225e"/>
            <textElement markup="none">
                <font size="14" fontName="Lato"/>
            </textElement>
            <text><![CDATA[Address:]]></text>
        </staticText>

不会引发任何错误。

这是我在 java 中生成 pdf 的方法:

ClassLoader classLoader = getClass().getClassLoader();
JasperReport jasperReportSpec = JasperCompileManager.compileReport(classLoader.getResourceAsStream("reporting/Example.jrxml"));
JasperPrint jasperPrintSpec = JasperFillManager.fillReport(jasperReportSpec, parameters, new JREmptyDataSource());
files.put("generatedFile", JasperExportManager.exportReportToPdf(jasperPrintSpec));

【问题讨论】:

  • 你有没有把jar和字体放在cp上?
  • @AlexK,我只是把jar的内容移到了resources/fonts/*
  • 您是否尝试使用其他字体?例如来自jasperreports-fonts-x.jar?
  • 如果您对 Java 没问题,您可以尝试使用附加的 源代码 进行调试。在这种情况下,我认为您将在 10 分钟内解决问题
  • 我一上电脑就会这样做。我应该只将 jar 添加到类路径吗?

标签: fonts jasper-reports


【解决方案1】:

当你使用字体扩展时,你应该只设置 fontName 属性。

从文本元素中删除 pdfFontName、pdfEncoding 和 isPdfEmbedded 属性,以便 PDF 导出器使用字体扩展名中定义的字体。

【讨论】:

  • 这没有帮助。我已将字体添加到类路径中,仅在 标记中将 fontName 设置为“Lato”,但我仍然没有预期的字体,即使它在 iReports 中可见
  • 是否还有“Could not load the following font”错误?
  • 您在看哪种导出格式(PDF/HTML/viewer/other)?
  • 我只看PDF
  • Lato 字体是否列在 PDF 查看器中(在属性/字体或类似内容下)?
【解决方案2】:

原来问题在于我没有在类路径中加载字体。感谢@AlexD,我已经设法使用 maven 添加它。

【讨论】:

    猜你喜欢
    • 2015-12-07
    • 1970-01-01
    • 2021-03-16
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多