Jasper Report Custom Font

Japser report is amazing reporting tool for reporting which has been used in several enterprise application. Jasper report provides flexibile functionalities for report developements. One of the features that is available in the Jasper report is custom font feature. One of the problem when developing a report is font compatability between platoms. By using custom font, the font that is available on a platform such a Windows could be used in another platform like Linux. In this post, we will exploring detail for how to create custom library for the font from ttf file and using the font library to generate report with particular font.

Table Contents

  1. Create Custom Font Library
  2. Create Report with Custom Font Library
  3. Generating Report with Custom Font Library From Java Program
  4. Reference

For my environment set up, I have set up as following

  • JDK 1.11
  • Jasper Studio 6.20.0
pom.xml
  <properties>
<java-version>1.11</java-version>
<jasper-version>6.20.0</jasper-version>
</properties>

<dependencies>

<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>${jasper-version}</version>
</dependency>


<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>

</dependencies>

1. Create Custom Font Library

Jasper Report Studio provides a ease of use for create custom font library from ttf file. In order to create custom font library, we first need to download font that we want to use in the report. In this sample, I will use Noto Sans Font from Google.

After download finished, we just extract the zip file. This example, we will create custom font library from NotoSans-MediumItalic.ttf

Next, open Jasper Report Studio and create custom font library by using downloaded ttf files as below

  1. Go to Windows >> Preference
  2. Jasper Studio >> Fonts >> Add

  3. Enter Family name >> At truetype (.ttf) browse and select .ttf (i.e.Anonymous.ttf) font file.
  4. At PDF Encoding, select for Identity-H (Unicode with horizontal writing) > Finish.

2. Create Report with Custom Font Library

Next, we'll just use created custom font to create report. In my report, I used JDBC datasource to create data source provide to the report

For the report, I have create as below. In the example, I have used two font types, Noto Sans and Monospace

sample01.jrxml

  
<?xml version="1.0" encoding="UTF-8"?> <!-- Created with Jaspersoft Studio version 6.20.0.final using JasperReports Library version 6.20.0-2bc7ab61c56f459e8176eb05c7705e145cd400ad --> <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="sample01" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="66cb32e1-2f32-4a96-96af-b97925d9d4cf"> <property name="com.jaspersoft.studio.data.defaultdataadapter" value="PgDataAdapter"/> <property name="com.jaspersoft.studio.data.sql.tables" value=""/> <queryString> <![CDATA[select * from itemtbl]]> </queryString> <field name="itemid" class="java.lang.Integer"> <property name="com.jaspersoft.studio.field.name" value="itemid"/> <property name="com.jaspersoft.studio.field.label" value="itemid"/> <property name="com.jaspersoft.studio.field.tree.path" value="itemtbl"/> </field> <field name="itemname" class="java.lang.String"> <property name="com.jaspersoft.studio.field.name" value="itemname"/> <property name="com.jaspersoft.studio.field.label" value="itemname"/> <property name="com.jaspersoft.studio.field.tree.path" value="itemtbl"/> </field> <field name="itemdesc" class="java.lang.String"> <property name="com.jaspersoft.studio.field.name" value="itemdesc"/> <property name="com.jaspersoft.studio.field.label" value="itemdesc"/> <property name="com.jaspersoft.studio.field.tree.path" value="itemtbl"/> </field> <variable name="itemid1" class="java.lang.Integer" calculation="Count"> <variableExpression><![CDATA[$F{itemid}]]></variableExpression> </variable> <background> <band splitType="Stretch"/> </background> <title> <band height="54"> <staticText> <reportElement x="285" y="10" width="240" height="30" uuid="ced703d3-f273-4c66-9243-2239672fad4b"/> <textElement textAlignment="Center" verticalAlignment="Middle" markup="styled"> <font fontName="NotoSans" size="18" isBold="true"/> </textElement> <text><![CDATA[Report Data]]></text> </staticText> <textField pattern="MMMMM dd, yyyy"> <reportElement x="530" y="12" width="263" height="30" uuid="ecb52066-9c39-40da-896e-4b816ea0b9fb"/> <textElement textAlignment="Center" verticalAlignment="Middle"> <font fontName="Monospaces_Anonymus" size="18"/> </textElement> <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression> </textField> </band> </title> <columnHeader> <band height="40"> <staticText> <reportElement x="94" y="6" width="100" height="30" uuid="ceca0b97-2b4d-436d-bc7e-ed7dea703fa0"> <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="3ab0372b-8fa2-4e04-bc2b-be64fa3d1369"/> </reportElement> <textElement textAlignment="Center" verticalAlignment="Middle"> <font fontName="NotoSans" size="12" isBold="true"/> </textElement> <text><![CDATA[itemid]]></text> </staticText> <staticText> <reportElement x="194" y="6" width="100" height="30" uuid="2b2902fc-cea8-41bb-8db1-966d22536d0e"> <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="956b978a-76f9-460d-b7cc-81e380c95c38"/> </reportElement> <textElement textAlignment="Center" verticalAlignment="Middle"> <font fontName="NotoSans" size="12" isBold="true"/> </textElement> <text><![CDATA[itemname]]></text> </staticText> <staticText> <reportElement x="294" y="5" width="100" height="30" uuid="e31e879f-03d7-484b-8f22-483ac1ae4ccc"> <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="93fd13dc-1119-46f1-a426-f39a3e041fe3"/> </reportElement> <textElement textAlignment="Center" verticalAlignment="Middle"> <font fontName="NotoSans" size="12" isBold="true"/> </textElement> <text><![CDATA[itemdesc]]></text> </staticText> </band> </columnHeader> <detail> <band height="39" splitType="Stretch"> <textField> <reportElement x="94" y="2" width="100" height="30" uuid="44a70bd8-421f-4c28-99aa-03e394c87ad3"> <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="3ab0372b-8fa2-4e04-bc2b-be64fa3d1369"/> </reportElement> <textElement textAlignment="Center" verticalAlignment="Middle"/> <textFieldExpression><![CDATA[$F{itemid}]]></textFieldExpression> </textField> <textField> <reportElement x="194" y="2" width="100" height="30" uuid="332e0067-1b03-478b-a310-b098359b54bd"> <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="956b978a-76f9-460d-b7cc-81e380c95c38"/> </reportElement> <textElement textAlignment="Center" verticalAlignment="Middle"/> <textFieldExpression><![CDATA[$F{itemname}]]></textFieldExpression> </textField> <textField> <reportElement x="294" y="2" width="100" height="30" uuid="0c6e64e1-b037-42dc-815d-2fb3637b180a"> <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="93fd13dc-1119-46f1-a426-f39a3e041fe3"/> </reportElement> <textElement textAlignment="Center" verticalAlignment="Middle"/> <textFieldExpression><![CDATA[$F{itemdesc}]]></textFieldExpression> </textField> </band> </detail> <summary> <band height="56"> <textField> <reportElement x="161" y="10" width="140" height="30" uuid="d398a978-6ee0-4540-bd8e-d4cdaa8c7938"/> <textElement textAlignment="Center" verticalAlignment="Middle"/> <textFieldExpression><![CDATA[$V{itemid1}]]></textFieldExpression> </textField> <staticText> <reportElement x="60" y="10" width="100" height="30" uuid="812234f3-9cde-48fa-bdd0-20cfacd02d8e"/> <textElement textAlignment="Center" verticalAlignment="Middle"> <font size="14" isBold="true"/> </textElement> <text><![CDATA[Total]]></text> </staticText> </band> </summary> </jasperReport>

Once previews, you should see as following

Finally, you could compile report (.jrxml) to jasper report file (.jasper) file

3. Export Custom Font library

In Case we want to use custom font for report from java program. We need to export custom font library. Jasper Report Studio comes with ease of use functionality to create custom font library. To create custom font library, you just need to export custom font from Jasper report to a java library file by following steps.

  1. Windows > > preference > > Jasper Studio > > Font
  2. Select Font >> Export

Once you exported all required font library, you shall have following files

4. Create Report with Custom Font From Java Program

After we have completed exporting of all needed font library files. We could use those file to create report from Java program by including those file in your class path. We will use our created report with custom font library to create PDF report. The codes shall be as following

Sample Report Java Program
package pro.tikkwiki.blog.sample.main;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import pro.tikkwiki.blog.sample.connector.PgConnector;

public class Main {

	public static void main(String[] args) {
		
		try {
			
			String reportName = "src/main/resources/sample01";
			Map<String, Object> parameters = new HashMap<String, Object>();
			
			Connection conn = PgConnector.getConnection();
			JasperPrint print = JasperFillManager.fillReport(reportName + ".jasper",parameters,conn);
			// exports report to pdf
			JasperExportManager.exportReportToPdfFile(print, "sample01.pdf");
			
			System.out.println("Completed !!!!!!!!!");
	
		} catch (Exception e) {
			throw new RuntimeException("It's not possible to generate the pdf report.", e);
		} 
	}

}

Once we run the report, we shall have result as below

5.Reference

  1. Custom Font with the Font Extension

No comments:

Post a Comment

Feature Recently

Running Wildfly Application Server in Domain Mode

  Wildfly application server provides two modes of how to run application one wildfly application server. It is very simple if you run your ...

Most Views