如何测试我的字体是否在 pdf 中正确呈现? [英] How can I test if my font is rendered correctly in pdf?

查看:17
本文介绍了如何测试我的字体是否在 pdf 中正确呈现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在jasper report中使用不同的字体时,需要使用font-extensions.

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

但是,如果字体未正确呈现,是否有一种方法可以测试该字体是否受 pdf 支持,以便我了解问题是否与我的字体扩展或我的 .ttf 字体?

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?

从 jasper 报告导出为 pdf 时字体渲染不正确是一个常见问题示例 Jasper Reports PDF 不导出西里尔文值,如检查表第 1 点中所见,使用字体扩展名并不总是足够的,该字体还需要 pdf 生成库支持并能够呈现实际性格.这就是为什么我决定通过这个 Q-A 风格的问题,以便未来的用户在点击清单 1 时可以参考如何快速测试字体.

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.

推荐答案

由于 jasper report 使用 库 测试您的字体是否可以在 pdf 中正确呈现的最简单方法是直接使用 itext 进行测试.

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

示例程序*,改编自iText:第11章:选择合适的字体

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):

  • 要呈现特殊字符,请使用它的编码值示例u20BA 避免文件编码类型问题.
  • 考虑始终使用 Unicode 编码,这是推荐的方法在较新的 PDF 标准(PDF/A、PDF/UA)中,并让您可以混合不同的编码类型,唯一的缺点是稍微更大的文件大小.
  • 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.

结论:

如果您的字体在fontTest.pdf"中正确渲染,您有一个您在 jasper 报告中的字体扩展有问题.

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

如果您的字体在fontTest.pdf"中未正确呈现,则有在 jasper 报告中您无能为力,您需要找到另一种字体.

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 发行版使用了一个特殊的版本 itext-2.1.7,这个版本的导入是 com.lowagie.text,如果你使用的是更高版本的导入是com.itextpdf.text 如改编的例子.


*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.

这篇关于如何测试我的字体是否在 pdf 中正确呈现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆