Hot questions for Using JasperReports in encoding

Question:

How can I set charecter encondig before export the report?

Before version 5.6.0

exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "ISO-8859-1");

With the new version, how can I set?


Answer:

the API sais:

JRExporterParameter.CHARACTER_ENCODING is deprecated.

Replaced by

SimpleWriterExporterOutput.SimpleWriterExporterOutput(java.io.File, String), SimpleWriterExporterOutput.SimpleWriterExporterOutput(String, String) and SimpleWriterExporterOutput.SimpleWriterExporterOutput(java.io.OutputStream, String).

The property itself has following docu: public static final java.lang.String PROPERTY_CHARACTER_ENCODING Deprecated. Replaced by WriterExporterOutput.PROPERTY_CHARACTER_ENCODING.

see following link http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JRExporterParameter.html

Question:

I am using Jaspersoft Studio final version (v6.1.1). I have designed my report and it contains Turkish characters. When exporting the report from Studio as PDF Turkish characters are not shown although I've set the Studio workspace encoding to UTF-8. Also I have embed the jrxml report file in my JSF app and export the report, the result is the same.

Do I have to configure something more?


Answer:

I was able to solve the problem. In the Jaspersoft Studio IDE, change encoding to UTF-8 in Window/Prefrences/General/Workspace. Afterwards click any field, parameter, or text in the IDE and from the Properties/Advanced menu select PDF/PDF Encoding. I hope it will help.

Question:

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:

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?


Answer:

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

Question:

I have a very strange problem while downloading a file from my Spring Controller, first of all here's the code:

@RequestMapping(value = "/id/{fileId}", method = RequestMethod.GET)
public void getFile(@PathVariable("fileId") String fileId, HttpServletResponse response) {

    // check if file exists
    if (files.containsKey(fileId)) {
        response.reset();

        // StoredFile contains name, extension and content (byte[]) of the file
        StoredFile file = files.remove(fileId);
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=" + file.getFullName());

        byte[] payload = file.getContent();         
        response.setContentLength(payload.length);

        // writing file on disk
        File f = new File("/" + fileId + ".pdf");
        try {
            FileCopyUtils.copy(payload, f);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // send the same payload to the client for download
        try {
            response.getOutputStream().write(payload);
        } catch (IOException ex) {
            throw new RuntimeException("IOError writing file to output stream");
        }
    }
}

So, I retrieve a previously stored file, then I save its content on disk and send the same content to a client for download, but here's the strange thing: the two files are different!

The saved file is ok (it's a jasper report pdf).

The downloaded file displays nothing, and looking inside it with a binary editor I found the "EF BF BD" byte sequence that displays an UTF-8 unrecognised character.

I don't understand when the content of my byte[] is interpreted as UTF-8 and why the byte sequence is inserted; should I set the encoding somewhere even if I'm writing raw bytes inside the output stream?

I tried to set the response charactr encoding to different encoding with no change in the result, but I hadn't expected any since I'm not transferring text...

Any idea?


Answer:

The code works! A colleague downloaded my branch and run the application... The downloaded file was ok. So I tried to pass the code to another colleague and then to deploy it on the test server, always getting positive results. The issue must be in some workspace or machine setting, but for sure not in the code.