3
votes

I wanted to show different fonts(Gujarati-Indian Language) in Exported report of PDF from jasper report version 5.1.

What i have tried : After Running Tool open below location Step 1:

Tools -> Option -> Fonts

net.sf.jasperreports.default.font.name=Arial Unicode MS - the default     font name.
net.sf.jasperreports.default.font.size=10 - the default font size.
net.sf.jasperreports.default.pdf.font.name=Identity-H - the default PDF font.
net.sf.jasperreports.default.pdf.encoding=UTF-8 - the default PDF character encoding.
net.sf.jasperreports.default.pdf.embedded=true - by default PDF fonts are not embedded

Fonts "Arial Unicode MS" installed and its jar file also created.Jar files exits in class path of jasper report.

Link Followed :1) Fonts 2) Sample fonts

Step 2 : irfonts.xml

By default jasper report gives other fonts in this file i changed it to.

<fontFamily name="Arial Unicode MS">
<normal>net/sf/jasperreports/fonts/dejavu/ArialUnicodeMS.ttf</normal>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>

Step 3:

What result i am getting: હીપ્સ What i actually wanted: હિપ્સ

After Spending some time i came to know when you render a page in Template of Jasper report Fonts are working as per need but when you export it to pdf it changes Reason all the fonts goes through "itext pdf engine".

Now, trick is itext do not have fonts what I am currently using.

Is there any way by which I can achieve this.

Notes: It's not created using Java.I'm a Oracle Database Developer so created using tool only and printing withing tools boundaries.

Followed this blog also but it is in java so don't know where make changes. Blog of java

Sample Code:

<textField>
<reportElement x="111" y="26" width="100" height="20" uuid="5a471a16-de7b-4f55-9c9f-b01d37938b9f"/>
 <textElement>
    <font fontName="Arial Unicode MS" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{my_column_values}]]></textFieldExpression>

Some the similar questions are also in stack overflow but all of them uses java to call and print.

So, can I change itext library or any other workaround will be fine. How i created :

1) Jasper report

2) Jasper

2
I'm a Oracle Database Developer so created using tool only and printing withing tools boundaries. - What is your way for generating reports using JasperReports? And what is your task(s)?Alex K
Based on Data Source which will use my data base Columns and based on column i have created Report to render data.Pranav Shah
You can check out this to narrow down your problem How can I test my font in pdf and the problem of rendering with ligaturesPetter Friberg
Note you need to use font-extensions check out this how-can-i-render-hindi-correctly-when-exporting-to-pdfPetter Friberg
Try to narrow it down, then you need to provide a minimal reproducible example, a minimal jrxml (also without data, for example use title band and a parameter), link to ttf font, current result and expected result. That way we get a great question and can provide you with a decent answer.Petter Friberg

2 Answers

2
votes

Try JasperReports 6.2.0 or newer. It comes with PDF support for Indic scripts (achieved by transporting the AWT text layout to PDF).

The Unicode report sample (under demo/samples/unicode) from the JasperReports distribution shows texts in several Indic scripts, including Gujarati (using Google Noto fonts).

2
votes

I create a new question only related to itext as suggested by @AmedeeVanGasse I will show in this anwer how Alexey Subach answer could be implement in jasper reports.

NOTE: Alexy Subach states that "the implementation is really poor and you cannot expect to get correct results with it"

Strategy 1: You only need to export pdf

Add to classpath itext5 library (note latest jasper report use itext2, you will need both) and define the GujaratiLigaturizer class as an variable, then process your text every time you need to output.

Example

<?xml version="1.0" encoding="UTF-8"?>
<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="FontTest" columnCount="5" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="111" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="2347c131-1884-430a-b77f-59f08f896c8a">
    <variable name="g" class="com.itextpdf.text.pdf.languages.GujaratiLigaturizer">
        <initialValueExpression><![CDATA[new com.itextpdf.text.pdf.languages.GujaratiLigaturizer()]]></initialValueExpression>
    </variable>
    <title>
        <band height="25">
            <textField pattern="">
                <reportElement x="0" y="0" width="200" height="25" uuid="ee49d149-394b-4ac6-a0a2-6d207b0c8d89"/>
                <textElement>
                    <font fontName="Arial Unicode MS" size="14"/>
                </textElement>
                <textFieldExpression><![CDATA[$V{g}.process("\u0ab9\u0abf\u0aaa\u0acd\u0ab8")]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

Important notice: only the pdf export will be correct!

Pdf result

result

Strategy 2: Switch the text if you export to pdf

Before exporting via java if you need to generate pdf, switch the text in the JasperPrint object before exporting

JasperPrint print = JasperFillManager.fillReport(report, new HashMap<String, Object>(), theDatasource);
IndicLigaturizer g = new GujaratiLigaturizer();
for (JRPrintPage page : print.getPages()) {
    for (JRPrintElement element : page.getElements()) {
        if (element instanceof JRTemplatePrintText){
            JRTemplatePrintText text = (JRTemplatePrintText)element;
            text.setText(g.process(text.getText()));
        }
    }
}