3

I am using JasperReports and DynamicReports with this piece of java code to create a report in pdf format which contains utf-8 characters, the problem is generated pdf file does not contain utf-8 characters at all, like if they have been replaced with "". is there any thing that i should be aware of when using OutputStream to create a utf-8 file?

    public void toPdf(String path){
        OutputStream outHtml;
        try {
            outHtml = new FileOutputStream(path);

            jasperBuilder.toPdf(outHtml);
        } catch (Exception e1) {
            logger.error("failed to create PDF", e1);
        }
}

this may be notable that creating XLS and HTML file faces no such problem.

note that there are lots of lines of code under jasperBuilder.toPdf(outHtml); that i have traced and no where in those lines my utf-8 characters are being eliminated. so i guess the devil is in outHtml = new FileOutputStream(path);

MoienGK
  • 4,544
  • 11
  • 58
  • 92
  • What do you mean by "UTF-8 characters"? UTF-8 is just an encoding - it can encode *all* Unicode characters. – Jon Skeet Dec 14 '13 at 12:04
  • Japaneses characters. or persian characters are being removed from within the report – MoienGK Dec 14 '13 at 12:19
  • 1
    Right. It sounds like it's worth looking at how you configure `jasperBuilder`. It'll be about that rather than `FileOutputStream`. – Jon Skeet Dec 14 '13 at 12:23
  • since i am using dynamicreports to work with jasper, now i am trying to figure out what dynamicreport fellas have done in their library – MoienGK Dec 14 '13 at 12:51

2 Answers2

5

I managed to solve it. It was a font and encoding problem. Just followed tutorial here, but change <pdfEncoding>UTF-8</pdfEncoding> to <pdfEncoding>Identity-H</pdfEncoding> in fonts.xml

<fontFamilies>
  <fontFamily name="FreeUniversal">
    <normal>/home/moien/tahoma.ttf</normal>
    <bold>/home/moien/tahoma.ttf</bold>
    <italic>/home/moien/tahoma.ttf</italic>
    <boldItalic>/home/moien/tahoma.ttf</boldItalic>
    <pdfEncoding>Identity-H</pdfEncoding>
    <pdfEmbedded>true</pdfEmbedded>
  </fontFamily>
</fontFamilies> 

Now I have another challenge to solve, making font URL relative!

Salek
  • 449
  • 1
  • 10
  • 19
MoienGK
  • 4,544
  • 11
  • 58
  • 92
2

A FileOutputStream is completely agnostic of the "stuff" that gets written to it. It just writes bytes. If characters are being eliminated or mangled, then this is being caused by whatever is generating the bytes to be written to the stream.

In this case, my money would be on the way that you have configured / used the jasperBuilder object prior to running this code.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • hmm. thanks for reply. i don't know where to look.then i guess the pdf generator of jasper is causing the problem – MoienGK Dec 14 '13 at 12:17
  • @dave - that is a possibility. But (IMO) it is more likely that the problem is in the way that you are using Jasper Reports. I'm sure Jasper Reports is capable of handling the full range of valid Unicode characters ... when used correctly. – Stephen C Dec 14 '13 at 12:24
  • i am not directly involved with jasper, i am using Dynamic Reports to handle jasper report generation http://www.dynamicreports.org/ – MoienGK Dec 14 '13 at 12:29
  • @dave - once again, I'd expect something like that would be capable of handling the full range of valid Unicode characters. Check how you are using it. – Stephen C Dec 14 '13 at 23:36