iReport preview/exporter output not handling UTF-8 translations?

Stefan Kendall picture Stefan Kendall · Sep 15, 2011 · Viewed 24k times · Source

I'm trying to make my reports work with a translations bundle. No matter what I try, I can't seem to render UTF-8 for any language - I just get two broken characters for every UTF-8 character.

My report starts with this: <?xml version="1.0" encoding="UTF-8"?> ...but iReport preview doesn't work,

and my actual code has this:

JRProperties.setProperty("net.sf.jasperreports.default.pdf.encoding", "UTF-8");
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8")

Neither seems to be able to output UTF-8. What could possibly be going on here? I'm pulling my hair out. Even with the font set to Arial, I'm having issues. My web-app is using the same font and similar bundles with Grails, and I have no issues there, so something is happening specific to jasperreports.

EDIT: I thought byte order markers may have been the issue, but it isn't. I've created a basic report that shows the failure in 4.0.2.

Here's the resource bundle:

Greek properties file

And the output in iReport:

enter image description here

And here's the report, and the properties file used to observe these results.

Nore information: When the properties files are saved in ANSI encoding, I can get all german characters, including ä and ß. When the properties files are saved as UTF-8, only ASCII characters work.

Answer

Alex K picture Alex K · Sep 18, 2011

Stefan, I know that it is not very good solution, but it works for your sample.

        <textField>
            <reportElement x="49" y="0" width="359" height="38"/>
            <textElement>
                <font fontName="Arial" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
            </textElement>
            <textFieldExpression><![CDATA[new String($R{title}.getBytes("ISO-8859-1"), "UTF-8")]]></textFieldExpression>
        </textField>

In case you know source codepage the convertation will help you. May be you can pass codepage in report and use it as parameter or variable in expression.

The expression can be like this:

   <textFieldExpression><![CDATA[new String($R{title}.getBytes($P{codePage}), "UTF-8")]]></textFieldExpression>

or like this, if you can pass codepage in header of your properties file:

   <textFieldExpression><![CDATA[new String($R{title}.getBytes($R{codePage}), "UTF-8")]]></textFieldExpression>

UPDATE:
After adding Arial font my irfonts.xml (%IREPORT_DIR%\ireport\fonts) has this content:

<?xml version="1.0" encoding="UTF-8"?>

<fontFamilies>
   <fontFamily name="Arial">
       <normal><![CDATA[arial.ttf]]></normal>
       <bold><![CDATA[arialbd.ttf]]></bold>
       <italic><![CDATA[ariali.ttf]]></italic>
       <boldItalic><![CDATA[arialbi.ttf]]></boldItalic>
       <pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
       <pdfEmbedded><![CDATA[false]]></pdfEmbedded>
   </fontFamily>

</fontFamilies>