我如何testing如果我的字体在pdf中正确显示?

在jasper报告中使用不同的字体时,您需要使用font-extensions 。

但是,如果字体不能正确呈现有没有一种方法,我可以testing,如果该字体是由PDF支持,以便我可以理解,如果问题是与我的字体扩展名或我的.ttf字体?

从jasper报告导出为pdf时,字体显示不正确是一个常见问题示例Jasper Reports PDF不会导出西里尔文的值 ,如清单1所示,使用font-extensions并不总是足够的,字体也需要支持PDF生成库,并能够呈现实际的字符。 这就是为什么我决定通过这个QA风格的问题,以便将来的用户打击清单1时可以有一个如何快速testing字体的参考。

由于jasper报告使用itext库,最简单的方法来testing您的字体是否将在pdf中正确呈现是直接用itext进行testing。

示例程序* ,从iText改编:第11章:select正确的字体

 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); } } 

一些笔记(例如):

  • 要呈现特殊字符,请使用它的编码值示例\u20BA来避免文件上编码types的问题。
  • 考虑总是使用Unicode编码,这是在新的PDF标准(PDF / A,PDF / UA)中推荐的方法,使您可以混合使用不同的编码types,只有略大的文件大小的不利条件。

结论:

如果您的字体在“fontTest.pdf”中正确显示,那么您在jasper报告中的字体扩展名有问题。

如果字体在“fontTest.pdf”中没有正确显示,那么在碧玉报告中没有什么可以做的,你需要find另一种字体。


*最新的jasper-reports发行版使用了一个特殊的版本itext-2.1.7,在这个版本中的导入是com.lowagie.text ,如果你使用的是以后的版本,导入是com.itextpdf.text就像在改编的例子中一样。