如何测试我的字体是否在 pdf 中正确呈现?
How can I test if my font is rendered correctly in pdf?
jasper报表中使用不同字体时,需要使用font-extensions。
但是,如果字体 未正确呈现 有没有一种方法可以测试 pdf 是否支持该字体,以便我了解问题是否与我的相关字体扩展或我的 .ttf
字体?
从 jasper 报告导出为 pdf 时字体呈现不正确是一个常见问题示例 ,如清单第 1 点所示,使用字体扩展并不总是足够,字体还需要 pdf 生成库支持并能够呈现实际字符。这就是为什么我决定通过这种问答式的问题,以便将来的用户在点击清单1时可以参考如何快速测试字体。
自从 jasper 报告使用 itext 库以来,测试您的字体是否会在 pdf 中正确呈现的最简单方法是直接使用 itext 进行测试。
示例程序*,改编自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);
}
}
一些注释(见示例):
- 要渲染特殊字符,请使用它的编码值示例
\u20BA
以避免文件的编码类型问题。
- 考虑始终使用 Unicode 编码,这是推荐的方法
在较新的 PDF 标准中 (PDF/A, PDF/UA) 并让您可以混合
不同的编码类型,唯一的缺点是
更大的文件大小。
结论:
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.
*最新的jasper-reports distribution使用特殊版本itext-2.1.7,这个版本的imports是com.lowagie.text
,如果你使用更高版本imports是com.itextpdf.text
如改编示例中所示。
jasper报表中使用不同字体时,需要使用font-extensions。
但是,如果字体 未正确呈现 有没有一种方法可以测试 pdf 是否支持该字体,以便我了解问题是否与我的相关字体扩展或我的 .ttf
字体?
从 jasper 报告导出为 pdf 时字体呈现不正确是一个常见问题示例
自从 jasper 报告使用 itext 库以来,测试您的字体是否会在 pdf 中正确呈现的最简单方法是直接使用 itext 进行测试。
示例程序*,改编自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);
}
}
一些注释(见示例):
- 要渲染特殊字符,请使用它的编码值示例
\u20BA
以避免文件的编码类型问题。 - 考虑始终使用 Unicode 编码,这是推荐的方法 在较新的 PDF 标准中 (PDF/A, PDF/UA) 并让您可以混合 不同的编码类型,唯一的缺点是 更大的文件大小。
结论:
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.
*最新的jasper-reports distribution使用特殊版本itext-2.1.7,这个版本的imports是com.lowagie.text
,如果你使用更高版本imports是com.itextpdf.text
如改编示例中所示。