Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasper Reports: Font extension not working

I'm using multiple fonts in one of my reports that I export as a PDF, I'm using IREPORT to design my report.

In the preview section the report looks good but when I export it from my web app it looks like I'm using one font for the whole report.

I've searched this issue and I found two solutions for it:

  1. Export the font as .jar file from IREPORT and add it to my project [ Not a Clean way, Since I'm using Maven ].

  2. Use jasperreports_extension.properties [the one I'm having problem with!].

I followed this tutorial step by step but still no result no error no nothing.

Here is the code I'm using:

jasperreports_extension.properties:

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

fonts.xml:

<?xml version="1.0" encoding="UTF-8"?>

<fontFamilies>

    <fontFamily name="Arial">
        <normal><![CDATA[fonts/arial.ttf]]></normal>
        <pdfEmbedded><![CDATA[true]]></pdfEmbedded>
    </fontFamily>

    <fontFamily name="Algerian">
        <normal><![CDATA[fonts/Algerian.ttf]]></normal>
        <pdfEmbedded><![CDATA[true]]></pdfEmbedded>
    </fontFamily>

    <fontFamily name="Bell Mt">
        <normal><![CDATA[fonts/bell-mt.ttf]]></normal>
        <pdfEmbedded><![CDATA[true]]></pdfEmbedded>
    </fontFamily>

    <fontFamily name="Savoye Std">
        <normal><![CDATA[fonts/SavoyeStd.ttf]]></normal>
        <pdfEmbedded><![CDATA[true]]></pdfEmbedded>
    </fontFamily>

    <fontFamily name="Cambria">
        <normal><![CDATA[fonts/Cambria.ttf]]></normal>
        <normal><![CDATA[fonts/cambriab.ttf]]></normal>
        <normal><![CDATA[fonts/cambriai.ttf]]></normal>
        <normal><![CDATA[fonts/cambriaz.ttf]]></normal>
        <pdfEmbedded><![CDATA[true]]></pdfEmbedded>
    </fontFamily>

</fontFamilies>

Since I'm using spring I tried this also fonts.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans:beans 

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"

    xmlns:jee="http://www.springframework.org/schema/jee"

    xsi:schemaLocation="

        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.3.xsd

        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">

    <beans:bean id="Arial" class="net.sf.jasperreports.engine.fonts.SimpleFontFamily">

        <beans:property name="name" value="Arial"/>
        <beans:property name="normal" value="fonts/arial.ttf"/>       
        <beans:property name="pdfEncoding" value="Identity-H"/>
        <beans:property name="pdfEmbedded" value="true"/>

    </beans:bean>

    <beans:bean id="Algerian" class="net.sf.jasperreports.engine.fonts.SimpleFontFamily">

        <beans:property name="name" value="Algerian"/>
        <beans:property name="normal" value="fonts/Algerian.ttf"/>       
        <beans:property name="pdfEncoding" value="Identity-H"/>
        <beans:property name="pdfEmbedded" value="true"/>

    </beans:bean>

    <beans:bean id="BellMt" class="net.sf.jasperreports.engine.fonts.SimpleFontFamily">

        <beans:property name="name" value="Bell Mt"/>
        <beans:property name="normal" value="fonts/bell-mt.ttf"/>       
        <beans:property name="pdfEncoding" value="Identity-H"/>
        <beans:property name="pdfEmbedded" value="true"/>

    </beans:bean>

    <beans:bean id="SavoyeStd" class="net.sf.jasperreports.engine.fonts.SimpleFontFamily">

        <beans:property name="name" value="Savoye Std"/>
        <beans:property name="normal" value="fonts/SavoyeStd.ttf"/>       
        <beans:property name="pdfEncoding" value="Identity-H"/>
        <beans:property name="pdfEmbedded" value="true"/>

    </beans:bean>

    <beans:bean id="Cambria" class="net.sf.jasperreports.engine.fonts.SimpleFontFamily">

        <beans:property name="name" value="Cambria"/>
        <beans:property name="normal" value="fonts/Cambria.ttf"/>
        <beans:property name="bold" value="fonts/cambriab.ttf"/>
        <beans:property name="italic" value="fonts/cambriai.ttf"/>
        <beans:property name="boldItalic" value="fonts/cambriaz.ttf"/>       
        <beans:property name="pdfEncoding" value="Identity-H"/>
        <beans:property name="pdfEmbedded" value="true"/>

    </beans:bean>

</beans:beans>

And the font files in the font folder in the root package.

like image 978
simoi chigo Avatar asked Apr 14 '16 14:04

simoi chigo


People also ask

How do I change font size in Jasper report?

Use the <style/> element instead. fontName The font name, which can be the name of a physical font, a logical one or the name of a font family from the registered JasperReports font extensions. size The size of the font measured in points. It defaults to 10.


2 Answers

A big thank you to @Tunaki for helping me to solve this issue.

The problem was solved by putting the jasperreports_extension.properties and the fonts folder in the root of the project like this:

enter image description here

like image 139
simoi chigo Avatar answered Sep 25 '22 15:09

simoi chigo


Yeah, using simple xml font file worked for me. Here is fonts.xml file:

<?xml version="1.0" encoding="UTF-8"?>

<fontFamilies>

    <fontFamily name="Arial Unicode MS">
        <normal><![CDATA[fonts/ARIALUNI.TTF]]></normal>
        <pdfEncoding>Identity-H</pdfEncoding>
        <pdfEmbedded><![CDATA[false]]></pdfEmbedded>
    </fontFamily>

</fontFamilies>

Here is src\main\resources\jasperreports_extension.properties:

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

Here is structure:

enter image description here

In your code, nothing need to do, just print in normal case, UTF-8 will work!

like image 35
nobjta_9x_tq Avatar answered Sep 25 '22 15:09

nobjta_9x_tq