使用JaspersReport加载字体的问题 [英] Issue with loading font using JaspersReport
问题描述
我用iReports导出了这个字体,这个 / b / b / b / b / b / b / b / b / b / b / b / p / p>
/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.L ato = fonts / fontsfamily1480698239543.xml
和 fontsfamily1480698239543.xml $ c
$ b
<?xml version =1.0encoding =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 =Coloredx =119y =93width =161height =15uuid =724f085b-cb64-4d11-ac64-dc23e6f04553/>
< textElement markup =none>
< / textElement>
< text><![CDATA [Whatever]]>< / text>
< / staticText>
< staticText>
< reportElement x =198y =200width =154height =24uuid =95b310d8-19bf-4485-840f-f3f5076b225e/>
< textElement markup =none>
< / textElement>
< text><![CDATA [Address:]]>< / text>
< / staticText>
当我尝试生成文件时,出现以下错误:
net.sf.jasperreports.engine.JRRuntimeException:无法加载以下字体:
pdfFontName:Lato
pdfEncoding:Cp1250
isPdfEmbedded:true]带有根本原因
net.sf.jasperreports.engine.JRRuntimeException:无法加载以下字体:
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 =198y =200width =154height =24uuid =95b310d8-19bf-4485-840f-f3f5076b225e/>
< textElement markup =none>
< font size =14fontName =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));
原来问题是我没有加载在我的类路径中的字体。感谢@AlexD,我设法使用maven添加它。
I have an issue with loading my own font.
I've exported the font using iReports and this tutorial.
This resulted in having the files:
/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
Where 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
And 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>
In my report.jrxml I use the following for text:
<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>
When I am trying to generate the file, I get the following error:
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]
What am I doing wrong?
Update
I've applied changes with accordance to comment by AlexK and dada67, but this still didn't changed the font that is generated in the PDF. However, I can see the font in iReports application. I now use:
<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>
Which doesn't throw any error.
Here is how I generate the pdf in java:
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));
Turns out that the issue was that I was not loading the font in my classpath. Thanks to @AlexD, I've managed to add it using maven.
这篇关于使用JaspersReport加载字体的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!