0

I'm using JDK 1.6.0_35 in a Java Project and I'm having a problem with java.util.ResourceBundle when recovering a properties file (encoded in ISO8859-1).

We've been asked to show an invoice with english labels but an only one, which must be written in bulgarian. The issue is that it recovers and displays only the text which is not written in Bulgarian.

I've tried to insert the text in two ways:

\u0424\u0430\u043A\u0442\u0443\u0440\u0430\r\n\u041E\u0440\u0438\u0433\u0438\u043D\u0430\u043B

"Фактура\r\nОригинал"

Neither of them worked. I'm googling and it seems like it should work with the first way (though uncomfortable, it should work), but the text written like this does not display in my PDF (I'm using Jasperreports).

When doing JasperFillManager.fillReport() my params variable contains "INVOICE=INVOICE/XXXФактураXXX", but when opening the PDF it only displays "INVOICE/XXX".

Java code:

Object obj = JRLoader.loadObject(new InputStream(someSource));
JasperReport reportMaster = (JasperReport) obj;
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(data);
JasperPrint jasperPrint = JasperFillManager.fillReport(reportMaster, params, ds);
String pathArialFont = "";
String pathArialBFont = "";
String pathArialIFont = "";
String pathArialBIFont = "";
try {
    pathArialFont = Thread.currentThread().getContextClassLoader().getResource("arial.ttf").toURI()
        .toString();


    pathArialBFont = Thread.currentThread().getContextClassLoader().getResource("arialbd.ttf").toURI()
        .toString();


    pathArialIFont = Thread.currentThread().getContextClassLoader().getResource("ariali.ttf").toURI()
        .toString();


    pathArialBIFont = Thread.currentThread().getContextClassLoader().getResource("arialbi.ttf").toURI()
        .toString();
    LOGGER.debug("pathArialBIFont:".concat(pathArialBIFont));

} catch (final URISyntaxException e) {
    LOGGER.error("", e);
}

String arial = "Arial";
HashMap<FontKey, PdfFont> fontMap = new HashMap<FontKey, PdfFont>();
FontKey key1 = new FontKey(arial, false, false);
PdfFont font1 = new PdfFont(pathArialFont, BaseFont.CP1252, true);

FontKey key2 = new FontKey(arial, true, false);
PdfFont font2 = new PdfFont(pathArialBFont, BaseFont.CP1252, true);

FontKey key3 = new FontKey(arial, false, true);
PdfFont font3 = new PdfFont(pathArialIFont, BaseFont.CP1252, true);
FontKey key4 = new FontKey(arial, true, true);
PdfFont font4 = new PdfFont(pathArialBIFont, BaseFont.CP1252, true);

fontMap.put(key1, font1);
fontMap.put(key2, font2);
fontMap.put(key3, font3);
fontMap.put(key4, font4);

JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, informe);
exporter.setParameter(JRExporterParameter.FONT_MAP, fontMap);

reportMaster.setProperty("net.sf.jasperreports.awt.ignore.missing.font", "true");

exporter.exportReport();

Here you can see in one of my debuggings that the string arrives to Java code in cyrillic:

Cyrillic comes correctly into Java

fontsfamily1365159936026.xml:

<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
    <fontFamily name="Arial">
        <normal><![CDATA[fonts/arial.ttf]]></normal>
        <bold><![CDATA[fonts/arialbd.ttf]]></bold>
        <italic><![CDATA[fonts/ariali.ttf]]></italic>
        <boldItalic><![CDATA[fonts/arialbi.ttf]]></boldItalic>
        <pdfEncoding><![CDATA[Cp1252]]></pdfEncoding>
        <pdfEmbedded><![CDATA[false]]></pdfEmbedded>
    </fontFamily>
</fontFamilies>

jasperreports_extension.properties:

net.sf.jasperreports.extension.registry.factory.fonts=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.ireportfamily1365159936026=fontsfamily1365159936026.xml

Any clue?

madtyn
  • 1,469
  • 27
  • 55
  • Try character encoding UTF-8 – N00b Pr0grammer Sep 12 '16 at 10:45
  • 3
    You are missing [font-extensions](http://stackoverflow.com/documentation/jasper-reports/5773/font-extensions)?, see also this http://stackoverflow.com/questions/34041619/jasper-reports-pdf-doesnt-export-cyrillic-values – Petter Friberg Sep 12 '16 at 10:48
  • I'm looking right now – madtyn Sep 12 '16 at 10:52
  • @N00bPr0grammer The file is in ISO8859-1, I don't know if I'm allowed to change this. Also, as I wrote, the String value is correctly retrieved from the properties file. The problem comes when writing with Jasper. – madtyn Sep 12 '16 at 10:59
  • 1
    @madtyn, if you have not added font-extension you can be pretty sure this is the problem (add a textField in jrxml with your text and try) if not showing correctly, you have a font-extension problem. – Petter Friberg Sep 12 '16 at 11:15
  • @PetterFriberg I'm thinking now you're completely right, but I'm new to using Jasper and I need some time to read docs and understand what do I have to do to test it all. I will write back just as I finish. Thank you all – madtyn Sep 12 '16 at 11:18
  • 2
    I see someone has suggested it as duplicate, I will not vote for now, consider to accept if this solves you problem (there is no problem with duplicates, it gives more entry points to answers). If you like to test your font (ttf) individually in itext, I have posted this Q/A [How can I test if my font is rendered correctly in pdf?](http://stackoverflow.com/questions/35127956/how-can-i-test-if-my-font-is-rendered-correctly-in-pdf) – Petter Friberg Sep 12 '16 at 11:21

1 Answers1

0

I finally achieved it. I needed to change these two lines in the xml:

<pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
<pdfEmbedded><![CDATA[true]]></pdfEmbedded>

I changed in the Java code the BaseFont constant BaseFont.CP1252 in these lines:

new PdfFont(pathArialFont, BaseFont.IDENTITY_H, true);

and entered the text in my ISO8859-1 properties file like this:

label=\u0424\u0430\u043A\u0442\u0443\u0440\u0430\r\n\u041E\u0440\u0438\u0433\u0438\u043D\u0430\u043B

And in the designer mode I edited the field to font "Arial Unicode MS" and checked "Pdf embedded" with Pdf Encoding "Identity-H".

madtyn
  • 1,469
  • 27
  • 55