Stefan Kendall
Stefan Kendall

Reputation: 67812

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

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.

Upvotes: 6

Views: 25066

Answers (3)

This is an answer for other developer which also have encoding problem when generating reports...

I had a similar problem when generating reports with, for example, French locale. My bundles and templates files are stored in a database to allow new templates and locale without recompiling the project. BUT every characters with accents contains in the French bundle aren't print correctly. So, I :

  • Check the bundle char encoding (set to UTF-8 without BOM)
  • Check the XML header of the .jrxml report file ()

I think jasper reports take UTF-8 by default to generate reports but I'm not sure at 100%. It can be forced with :

JRExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");

The problem persist again so instead of :

ResourceBundle bundle = new PropertyResourceBundle(new ByteArrayInputStream(myBundle)); // myBundle is a byte array retrieve from a DB

I do this :

InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(myBundle), Charset.forName("UTF-8"));
ResourceBundle bundle = new PropertyResourceBundle(reader);

After that, just passing the resource bundle to the JasperFillManager and every thing goes well now. May be it's so simple but it would have saved me 2 or 3 hours of research...

Upvotes: 2

Alex K
Alex K

Reputation: 22857

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>

Upvotes: 10

Stefan Kendall
Stefan Kendall

Reputation: 67812

Make sure your resource bundles don't have a byte order marker. This will break jasperreports, and it won't show up in IDEs or text editors.

Upvotes: 1

Related Questions