简体   繁体   中英

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.

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:

在此处输入图片说明

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:

在此处输入图片说明

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

I had similar issue where IReport designer preview able to show the font with bold but not in downloaded report from Spring boot.

I added the following maven dependency and solved my issue. https://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports-fonts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM