How to check if the font is processed correctly in pdf?

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

However, if the font does not display correctly , there is a way I can check if the pdf font is supported so that I can understand if the problem is related to my font extensions or my .ttf font?

Incorrect font rendering when exporting to pdf from jasper reports is a common example of a problem Jasper Reports PDF does not export Cyrillic values , as seen from control point 1, using fonts is not always sufficient, the font must also be supported by the pdf generation library and be able to display the actual character. That's why I decided to pass on these QA style questions so that the future user, once in checklist 1, could get a link to how to quickly test the font.

+5
java fonts pdf jasper-reports
Feb 01 '16 at 10:17
source share
1 answer

Since the jasper report uses the itext library - the easiest way to check if the font is displayed correctly in pdf is to check it directly using 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 (see example):

  • To display special characters, use the example of the encoded value \u20BA to avoid problems with the encoding type in your file.
  • Consider always using Unicode encoding, this is the recommended approach in the new PDF standard (PDF / A, PDF / UA) and allows different types of encoding, with the only drawback a slightly larger file size.

Output:

If your font displays correctly in font.est.pdf, you have a problem with your font extensions in the jasper report.

If the font does not display correctly in font.est.pdf, you cannot do anything in jasper reports, you need to find a different font.




* The latest jasper-reports distribution uses a special version of itext-2.1.7, import in this version of com.lowagie.text , if you use a later version, import com.itextpdf.text , as in the adapted example.

+7
Feb 01 '16 at 10:17
source share



All Articles