6

When using different fonts in jasper report, you need to use font-extensions.

However if the font is not rendered correctly is there a way that I can test if the font is supported by pdf so that I can understand if the problem is related to my font-extensions or to my .ttf font?

The incorrect rendering of font when exporting to pdf from jasper reports is a common problem example Jasper Reports PDF doesn't export cyrillic values, as seen in checklist point 1 using font-extensions are not always enough, the font need's also to be supported by pdf generating library and able to render the actual character. This is why I have decided to pass this Q-A style questions, so that future user when hitting checklist 1 can have a reference on how to quickly test the font.

Community
  • 1
  • 1
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109

1 Answers1

11

Since jasper report use the library the easiest way to test if your font will be rendered correctly in pdf is to test it directly with itext.

Example program*, adapted from iText: Chapter 11: Choosing the right font

import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfWriter;

public class FontTest {

    /** The resulting PDF file. */
    public static final String RESULT = "fontTest.pdf";
    /** the text to render. */
    public static final String TEST = "Test to render this text with the turkish lira character \u20BA";

    public void createPdf(String filename) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        document.open();
        BaseFont bf = BaseFont.createFont(
            "pathToMyFont/myFont.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font font = new Font(bf, 20);
        ColumnText column = new ColumnText(writer.getDirectContent());
        column.setSimpleColumn(36, 730, 569, 36);
        column.addElement(new Paragraph(TEST, font));
        column.go();
        document.close();
    }

    public static void main(String[] args) throws IOException, DocumentException {
        new FontTest().createPdf(RESULT);
    }
}

Some notes (seen in example):

  • To render special characters use it's encoded value example \u20BA to avoid problems of encoding type on your file.
  • Consider to always use Unicode encoding, this is recommended approach in the newer PDF standard (PDF/A, PDF/UA) and gives you the possibility to mix different encoding types, with the only dis-advantage of slightly larger file size.

Conclusion:

If your font is rendered correctly in the "fontTest.pdf", you have a problem with your font-extensions in jasper report.

If you font is not rendered correctly in the "fontTest.pdf", there is nothing you can do in jasper reports, you need to find another font.


*Latest jasper-reports distribution use a special version itext-2.1.7, the imports in this version is com.lowagie.text, if you are using later version the imports are com.itextpdf.text as in adapted example.

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • For anyone who may read this source code example, if you want to save time visiting the link provided to the original source code, the right package for the classes is com.itextpdf.text.* (there's also a com.lowagie.text.*). – madtyn Sep 12 '16 at 11:34
  • 1
    @madtyn Thanks, I have added imports, note jasper-reports is using a special version of itext-2.1.7, the correct imports using latest jasper-report distribution is `com.lowagie.text`, but true the attribution is to later itext code. – Petter Friberg Sep 12 '16 at 11:50
  • By the way, I understand that if the precedent text appears and the final turkish character doesn't appear, I should get another font, isn't it? Where do I get it? – madtyn Sep 12 '16 at 13:58
  • 1
    internet (google have some massive fonts, rendering almost all languages), have you tried the fonts distributed with jasper report "DejaVu Sans" etc?, check also that you have encoded the turkish character (see the note) – Petter Friberg Sep 12 '16 at 14:04
  • @madtyn this for example https://www.google.com/get/noto/, but I would try the the jasper-reports fonts (see maven distribution) before I know for a fact that dejavu-serif renders Turkish Lira, the google fonts are massive – Petter Friberg Sep 12 '16 at 14:08
  • I tried the DejaVuSans.ttf from iReport-Professsional/demo/fonts. The turkish lira didn't show up. :-( I'm following your google link now. – madtyn Sep 12 '16 at 14:16
  • @madtyn don't use the ttf add directly the jasper-reports-fonts.jar to your classpath and use fontName in jrxml see this [How to add a symbol ₺ (Turkish Lira) in iReport](http://stackoverflow.com/questions/34672093/how-to-add-a-symbol-%E2%82%BA-turkish-lira-in-ireport/34677052#34677052) – Petter Friberg Sep 12 '16 at 14:21
  • I was using the .ttf in the FontTest.java given in the former link in order to test. Should I use the same jasper-reports-fonts.jar with that? There's no jrxml in that code sample. – madtyn Sep 12 '16 at 14:23
  • Ok, if you just like to test then extract it and remember to add it as unicode \u20BA, but if you can choose whatever font, I would not even test, the Dejavu Sans should render all common languages in Europe (and I'm sure it renders the Turkish Lira), hence just add it to classpath, then test directly your jrxml file. – Petter Friberg Sep 12 '16 at 14:25