Search code examples
javaspringutf-8jasper-reports

Getting UTF-8 PDF font into JasperReports 3.7.* report compiled in Java / Spring


By default, the JasperReports 3.7.* does not seem to come with any PDF fonts that support the 'Identity-H' UTF-8 encoding for the PDF Fonts.

I find a lot of examples of using iReports to import fonts and compile the report with the Pdf font embedded, but I haven't found any solutions that have worked for me when compiling the report with Java at runtime.

I have tried variations on the following:

  1. Creating a jasperreports_extension.properties file and defining an xml font family pointing to a UTF-8 TTF file, on the classpath of the Spring application
  2. Including a maven dependency for jasperreports-fonts: net.sf.jasperreports jasperreports-fonts 4.0.0
  3. Looked into inserting in the Jasper Parameter Object map with the PdfFont/FontMap approach, but the classes from the examples are marked as deprecated

On compile in these instances, the engine always reports an error about not being able to resolve the font. What is the solution here?


Solution

  • A team member was able to find a solution:

    When configuring the JRProperties for the Java compilation

    JRProperties.setProperty("net.sf.jasperreports.default.pdf.font.name", "org/gnu/freefonts/FreeSans.ttf");
    JRProperties.setProperty("net.sf.jasperreports.default.pdf.encoding", "Identity-H");
    JRProperties.setProperty("net.sf.jasperreports.default.pdf.embedded", true);
    

    Where org/gnu/freefonts/FreeSans.ttf is a UTF-8 font in the classpath of the application