Rodiq » Page 'utf-8 characters in jasper pdfs with styled text'

utf-8 characters in jasper pdfs with styled text

Yes, it is possible to use utf-8 in pdfs generated by jasper.

The steps:

- install proper fonts (for me arial and arial bold) on your computer

- in the java code define a font map with the pdf fonts, refering to the installed fonts with full path (i did not find a solution to load them from classpath).

JRPdfExporter exporter = new JRPdfExporter();
Map fontMap = new HashMap();
String realPath = "usr/share/fonts/truetype/msttcorefonts/";
//(String fontName, Boolean bold, Boolean italic)
fontMap.put(new FontKey("Arial", false, false),
 new PdfFont(realPath + "arial.ttf", "Identity-H", true)); //(String pdfFontName, String pdfEncoding, Boolean isPdfEmbedded)
fontMap.put(new FontKey("Arial", true, false),
 new PdfFont(resourcesFilePath + "arialbd.ttf", "Identity-H", true));
 //@parameters – a map with report's parameters
 //@beanCollection - a Collection with reports's data
 //@output - the ResponseOutputStream
 //@reportStream - the compiled jasper as an InputStream
JasperPrint jasperPrint = JasperFillManager.fillReport(reportStream, parameters, new JRBeanCollectionDataSource(beanCollection));
exporter.setParameter(JRPdfExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, output);
exporter.setParameter(JRExporterParameter.FONT_MAP, fontMap);
exporter.exportReport();

in the report (the xml) set only the font size or weight, no font name!
<textElement textAlignment="Left" verticalAlignment="Top" rotation="Left" lineSpacing="Single">
<font size="8"/>
</textElement>

or

<textElement textAlignment="Center">
<font pdfFontName="Helvetica-Bold" size="20" isBold="true"/>
</textElement>

And declare the Arial style in the report definition, at the beginning, after the import statements, before the parameters:

<import value="net.sf.jasperreports.engine.data.*" />
<style name="Arial" isDefault="true" fontName="Arial" fontSize="10" />
<parameter name="Data" isForPrompting="false" class="java.lang.String"/>

And many thanks to Dror Friedman, who’s post gave me the solution: http://jasperforge.org/plugins/espforum/view.php?group_id=102&forumid=103&topicid=20460

Share and Enjoy:
  • Digg
  • StumbleUpon
  • Facebook
  • Google Bookmarks
  • Reddit
  • RSS

2 comments to “utf-8 characters in jasper pdfs with styled text”

  1. Hello Sir,

    In my project i have to display reports in english and marathi(Indian regional language). I’m using mangal.ttf font.

    I’ve tried the above given code.
    Its not giving any error but also it doesn’t display anything in pdf report.

    Please help

    Thankss
    Deepti

  2. Dear Sir
    Thank you for this post. It helps a lot.

    Regards
    Nima

Leave a comment


seven − = 5