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.

0

1 Answer 1

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.

9
  • 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
    Commented Sep 12, 2016 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. Commented Sep 12, 2016 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
    Commented Sep 12, 2016 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) Commented Sep 12, 2016 at 14:04
  • @madtyn this for example 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 Commented Sep 12, 2016 at 14:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.