3

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

Petter Friberg
  • 19,652
  • 9
  • 51
  • 94
Pranav Shah
  • 89
  • 2
  • 12
  • `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 Apr 13 '16 at 14:05
  • Based on Data Source which will use my data base Columns and based on column i have created Report to render data. – Pranav Shah Apr 13 '16 at 14:19
  • You can check out this to narrow down your problem [How can I test my font in pdf](http://stackoverflow.com/questions/35127956/how-can-i-test-if-my-font-is-rendered-correctly-in-pdf) and the problem of rendering with [ligatures](http://stackoverflow.com/questions/34295596/font-myanmar3-not-rendering-correctly-in-itext) – Petter Friberg Apr 13 '16 at 16:27
  • Note you need to **use font-extensions** check out this [how-can-i-render-hindi-correctly-when-exporting-to-pdf](http://stackoverflow.com/questions/34287186/how-can-i-render-hindi-correctly-when-exporting-to-pdf) – Petter Friberg Apr 13 '16 at 16:28
  • Try to narrow it down, then you need to provide a [mcve], 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 Apr 13 '16 at 16:31
  • @Petter Friberg About first first comment its checking using JAVA code how can check without using java.. i am not familiar with it. Font Extension would be Font Ext if yes i have done this as i mention in my question. – Pranav Shah Apr 13 '16 at 19:32
  • How are you using jasper-reports?, itext, jasper-reports are java we have not much choice. Try to put together the [mcve] and I can test, (I need .ttf font and the text you need to print). – Petter Friberg Apr 13 '16 at 19:36
  • @Petter Friberg About first comment its checking using JAVA code how can check without using java.. i am not familiar with it. Font Extension would be http://jasperreports.sourceforge.net/sample.reference/fonts/index.html#fontextensions if yes i have done this as i mentioned in my question. – Pranav Shah Apr 13 '16 at 19:37
  • @Petter Friberg give me your email i'll send all docs. – Pranav Shah Apr 13 '16 at 19:40
  • :29933796 You can Download .ttf file from http://www.myfontfree.com/arial-unicode-ms-myfontfreecom126f36926.htm its Arial Unicode MS. What result i am getting: હીપ્સ What i actually wanted: હિપ્સ...You can test it with Static text as my data is based on Data source. – Pranav Shah Apr 13 '16 at 19:48
  • Ok I will run a test – Petter Friberg Apr 13 '16 at 19:51
  • Thanks...let me know if you anything – Pranav Shah Apr 13 '16 at 20:11
  • I have same problem running latest version of jasper-reports (reverted back to i-text 2.1.7) , I guess itext have problem with the font (tonight I will not study why), Quick solution, switch font .tff – Petter Friberg Apr 13 '16 at 20:31
  • Only Arial Unicode MS shows fonts what i needed. I tried to install font Code2000 but after installing it jasper studio crashes i can not see designer window. Jasper report version 5.1 which i am using – Pranav Shah Apr 13 '16 at 20:48
  • I have test also with latest version of iText (without jasper reports), same problem, you need to switch font our contact iText directly : ) – Petter Friberg Apr 13 '16 at 20:50
  • Which font you suggest i am out of ideas right now.. – Pranav Shah Apr 14 '16 at 05:42
  • Sorry, I do not really know, I can only search on google and someone told me maybe some Latha font – Petter Friberg Apr 14 '16 at 09:20
  • Thanks but i am not getting what i am missing this problem wasted my 3 damn freaking days. – Pranav Shah Apr 14 '16 at 09:56
  • Can we print from HTML..like right now i am able to generate HTML file but is it possible i can add a button on HTML page and that will open Chrome Print window. I had followed this. – Pranav Shah Apr 14 '16 at 12:25
  • @PetterFriberg can we put print button on html page i was following http://stackoverflow.com/questions/34673513/how-can-i-add-a-button-in-jasper-report – Pranav Shah Apr 15 '16 at 05:55
  • @PetterFriberg adding a tag in the hope that one particular person may notice a question is really frowned upon. In particular if the question lies outside that persons field of expertise. This question is about `jasper-reports`, I see nothing in the original question that relates to `itext`. – Amedee Van Gasse Apr 15 '16 at 09:02
  • @PetterFriberg I see in the comments that, according to your research, the problem could be in iText, but unfortunately you do not share your findings. Please start a new question with all the details you found, and leave out anything related to jasper-reports. – Amedee Van Gasse Apr 15 '16 at 09:06
  • @AmedeeVanGasse, Thanks for your interest, I will do a new question with [mcve] so you can see the problem, sorry for adding tag (it was a quick and dirty fix : ) – Petter Friberg Apr 15 '16 at 17:51
  • @AmedeeVanGasse I posted the question http://stackoverflow.com/questions/36655923/why-is-the-gujarati-indian-text-not-rendered-correctly-using-arial-unicode-ms – Petter Friberg Apr 15 '16 at 20:15
  • @PetterFriberg thanks for opening up a new question how ever i dont have that much of reputation to comment in that Question section...I would like to see if this GujaratiLigaturizer can be done with in Jasper report it self as you know i'm not using java to call print from resource bundle. Thank you. – Pranav Shah Apr 16 '16 at 06:30
  • @petterfriberg okay !! – Pranav Shah Jul 29 '16 at 05:20

2 Answers2

2

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()));
        }
    }
}
Community
  • 1
  • 1
Petter Friberg
  • 19,652
  • 9
  • 51
  • 94
  • @peter i am getting this error when i tried to preview java.lang.ClassNotFoundException: Will not load classes from default package (GujaratiLigaturizer). After implementing above. – Pranav Shah Apr 18 '16 at 05:57
  • @PranavShah, you need to add itext5 to your classpath – Petter Friberg Apr 18 '16 at 09:11
  • yes i did downlaod it from here and then add to class path... inTools-->option-> JasperReportProperties http://www.java2s.com/Code/Jar/i/Downloaditextpdf506sourcesjar.htm After that created a variable which will have values as you stated Variable Class = GujaratiLigaturizer and initialValueExpression = com.itextpdf.text.pdf.languages.GujaratiLigaturizer – Pranav Shah Apr 18 '16 at 10:59
  • @PranavShah Hmm, that is the source you need the bin https://sourceforge.net/projects/itext/, the official is on github but I do not know where... (sourceforge is just a backup but will serve you well) – Petter Friberg Apr 18 '16 at 11:02
  • i got this error java.lang.ClassNotFoundException: Will not load classes from default package (GujaratiLigaturizer) – Pranav Shah Apr 18 '16 at 11:19
  • You still have same problem you need to download the lib, add it to classpath (in jasper studio if displaying in jasper studio), in jasper-report-server (under WEB-INF) if using in jasper report server ecc. – Petter Friberg Apr 18 '16 at 11:26
  • Or you can check here https://docs.google.com/document/d/1JY8wUYKI8-FlGItYpdRPCNNQmm4WJHQuoi5CHgz5wes/edit?usp=sharing – Pranav Shah Apr 18 '16 at 12:00
  • in ireport tools>>Options>>Classpath add itextpdf-5.5.5.jar, close ireport and reopen. (but this will only solve the problem of exporting to pdf from ireport), you need to do it where your application is deployed. – Petter Friberg Apr 18 '16 at 12:12
  • can you make edits in above docs i wanted to call $V{g}.process($F{COL10}) is it right way or not? – Pranav Shah Apr 18 '16 at 12:35
  • How it is working for you i did manage to successfully call itext 5.5.5 as you said but i am seeing only Wrong Characters...હીપ્સ... – Pranav Shah Apr 18 '16 at 13:23
  • Check the Docs i had uploaded everything how i am doing it. :)https://docs.google.com/document/d/1JY8wUYKI8-FlGItYpdRPCNNQmm4WJHQuoi5CHgz5wes/edit?usp=sharing – Pranav Shah Apr 18 '16 at 13:28
  • @PranavShah, I have added my output related to the jrxml example (example in question). I have hard to check all your values, test mine and see if you get same result (**NOTE: you need to check the pdf result, naturally the preview is not correct**) – Petter Friberg Apr 18 '16 at 13:38
  • I can only see that it seems similar to your expected result. – Petter Friberg Apr 18 '16 at 13:41
  • I had tried your way too it also shows me Above PDF result as you mentioned...After Important notice: only the pdf export will be correct! – Pranav Shah Apr 18 '16 at 13:52
  • This is how you implement Alexey Subach solutions in jasper reports, note "the implementation is really poor", so if you don't always get correct result it would not surprise me, I have upvoted dada76 answer ; ), upgrade jasper reports..... – Petter Friberg Apr 18 '16 at 13:55
  • Furthermore Bruno have commented on the other post, telling that they will release a paid version that handles the language correctly. – Petter Friberg Apr 18 '16 at 13:56
  • Since your very kind response and Limitation of itext now,I am trying in HTML print but getting Cache data whenever i try to print ....is there any way to clear cache every time users Prints. – Pranav Shah Apr 18 '16 at 14:00
  • http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers – Petter Friberg Apr 18 '16 at 14:00
  • can i use this solution jrxml file i can see only php,asp.net,html4 etc – Pranav Shah Apr 18 '16 at 14:05
  • use html4, add it to the header of your output – Petter Friberg Apr 18 '16 at 14:07
  • If you open Tool its say Preview -> Html (Selected) Now, Three option on top Designer, XML, Preview now where to make changes – Pranav Shah Apr 18 '16 at 14:08
  • You need to use this property http://jasperreports.sourceforge.net/config.reference.html#net.sf.jasperreports.export.html.header, search for some examples – Petter Friberg Apr 18 '16 at 14:14
  • All this properties are available in version 5.5 and above – Pranav Shah Apr 18 '16 at 14:18
  • Okay sir thank you for your help so far :) Some how my limitation is such i can not upgrade it. – Pranav Shah Apr 18 '16 at 14:21
  • One more thing where html 4 option is located – Pranav Shah Apr 18 '16 at 14:47
  • You set it in head to avoid cache – Petter Friberg Apr 18 '16 at 14:52
  • Like this IN jrxml i can do this... Can not find html 4 option to make changes in it – Pranav Shah Apr 18 '16 at 14:55
  • In fact you need to add it to the html header when you export, that is done with that property or by setting this parameter JRHtmlExporterParameter.HTML_HEADER (search some on the issue and try with the parameter – Petter Friberg Apr 18 '16 at 15:04
  • i am trying to add this property in Tools ->> Jasper Report Properties Property : exporter.setParameter.JRHtmlExporterParameter.IMAGES_URI Value : servlets/image?image= Not Working though i want something similar which will give me the correct output.(Clearing Cache of Html Print). – Pranav Shah Apr 18 '16 at 15:08
  • I found HtmlExporterConfiguration.getHtmlHeader() can we use this as property and value : no-cache...dont know just guessing !!! – Pranav Shah Apr 18 '16 at 15:33
  • You need to get the no-cache into the html you are exporting : ), to do this you need to change the header, add it to the header, in old version I believe this is done through the parameter. – Petter Friberg Apr 18 '16 at 15:36
  • If you have old version can you please copy paste code in docs i have created...as far as i know if i create parameter then it will popup dialog box where i need to insert some value every time. – Pranav Shah Apr 18 '16 at 15:40
  • Now, it looks like this (my jrxml file) : Still no luck – Pranav Shah Apr 18 '16 at 15:47
  • Check out this, http://stackoverflow.com/questions/24612975/jasperreports-exporting-html-modify-default-html-template, you need to use the old code (in question) set the parameter as the header you like. This comment thread is getting a bit to long and far from the original question – Petter Friberg Apr 18 '16 at 15:50
  • Yes i did follow below link http://stackoverflow.com/questions/24612975/jasperreports-exporting-html-modify-default-html-template After following it i implement within boundaries of Jasper soft. Result jrxml is this : – Pranav Shah Apr 18 '16 at 15:53
  • Can we pass random Parameter with image which will clean cache...Something like Math.random()+$F{value}.....as in Image Expression...i am trying above(not working) but i need a right way to pass random parameter with Value. – Pranav Shah Apr 19 '16 at 06:18
2

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).

dada67
  • 3,883
  • 13
  • 15
  • i can use only uptill version 5.1. – Pranav Shah Apr 18 '16 at 08:55
  • @PranavShah, since the "the implementation is really poor" (it works with your example, but maybe with some other it do not), I really suggest you update jasper reports and use this approach. – Petter Friberg Apr 18 '16 at 12:14
  • @PranavShah I think you posted on the wrong question, the comment does not seem related to the topic. – dada67 Apr 19 '16 at 10:07
  • Created a new question here http://stackoverflow.com/questions/36710290/how-to-pass-math-random-parameter-to-clear-cache-from-with-in-jasper-report-ve – Pranav Shah Apr 19 '16 at 10:20