【发布时间】: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