Search code examples
fontsjasper-reports

Issue with loading font using JaspersReport


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));

Solution

  • 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.