Introduction

JasperReports font extension is used to package fonts which are not available in the target platform (e.g. Some Microsoft fonts are not available in Linux platform).

Prerequisites

  1. JDK 1.7 or later
  2. Maven 3.X

Steps

The following steps are used to create a font-extension for the font ‘Arial’

1. Create a maven project in the following structure.

Root
 |--pom.xml
 |--src
     |--main
          |--resources
                 |--fonts
                 |    |--fontsfamily.xml
                 |--jasperreports_extension.properties

2. Project POM

JasperReport Font extension doesn’t need any dependency so the pom.xml can be minimal.

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
        xmlns="http://maven.apache.org/POM/4.0.0">
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.seenukarthi</groupId>
     <artifactId>jasper-fonts-arial</artifactId>
     <version>1.0.0</version>
     <name>Jasper Reports Font Extension for Arial</name>
     <packaging>jar</packaging>
     <dependencies>
     </dependencies>
</project>

3. Fonts config

  • Copy the required fonts to the Root\src\main\resources\fonts\ folder. (There may have multiple files for each font style, so copy all the files.).
  • Edit Root\src\main\resources\fonts\fontsfamily.xml and add the following content.
<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
    <fontFamily name="Arial">
        <!-- Normal Arial Font -->
        <normal><![CDATA[fonts/arial.ttf]]></normal>
        <!-- Bold Arial Font -->
        <bold><![CDATA[fonts/arialbd.ttf]]></bold>
        <!-- Italic Arial Font -->
        <italic><![CDATA[fonts/ariali.ttf]]></italic>
        <!-- Bolt italic Arial Font -->
        <boldItalic><![CDATA[fonts/arialbi.ttf]]></boldItalic>
        <!-- Is font is embedded in PDF -->
        <pdfEmbedded><![CDATA[true]]></pdfEmbedded>
    </fontFamily>
</fontFamilies>
  • Edit the Root\src\main\resources\jasperreports_extension.properties and add the following entries.
net.sf.jasperreports.extension.registry.factory.fonts=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.ireportfamily=fonts/fontsfamily.xml

Build

$ mvn clean install

Add this artifact as dependency to the project which needed the fonts.