如何在使用 iText 和飞碟从 HTML 创建的 PDF 中嵌入字体?
我在将波兰语字体嵌入从 HTML 转换而来的 PDF 时遇到问题.
I have problem with embedding Polish fonts into PDF converted from HTML.
我的 HTML 代码在正文中有样式:
My HTML code have style in body:
<BODY style="font-family: Tahoma, Arial, sans-serif;font-size : 8pt;">
我尝试了两种将此类 HTML 转换为 PDF 的方法:
I tried 2 ways of converting such HTML into PDF:
- 带有 htmlcleaner 的 FOP
- 带有飞碟的 iText
对于 FOP,我可以将所有使用的字体添加到其配置文件中,然后创建的 PDF 嵌入这些字体(如果字体用于 HTML).在生成的 PDF 中,我在 Identity-H 编码中有 Tahoma 字体.看起来不错——所有波兰语字母都按预期显示.
For FOP I can add all used fonts into its config file and then created PDF have those fonts embedded (if font is used in HTML). In resulting PDF I have Tahoma font in Identity-H encoding. It looks good -- all Polish letters are displayed as expected.
然后我尝试使用 iText 进行这种转换:看起来更简单,因为我不需要为每个 HTML 创建转换.不幸的是,我不知道如何将使用过的字体嵌入到生成的 PDF 中.我发现的大多数示例都是从头开始创建 PDF,我不知道如何将这些方法应用于 Flying Saucer ITextRenderer 或其他用于转换的对象.
Then I tried such conversion with iText: seems simplier because I do not need to create transformation for every HTML. Unfortunately I don't know how to embed used fonts into resulting PDF. Most examples I found create PDF from scratch and I don't know how to apply those methods to the Flying Saucer ITextRenderer or other object used in conversion.
我当前的代码试图通过获取 ITextFontResolver
并添加字体 fs.addFont(path, true); 在
PDFCreationListener.preOpen()
中添加字体;代码>.但是我创建的所有 .pdf 都没有我想要的字体.
My current code tries to add fonts in PDFCreationListener.preOpen()
by getting ITextFontResolver
and adding font fs.addFont(path, true);
. But all .pdf I create do not have fonts I want.
第二个问题是结果PDF没有波兰语字母.是飞碟还是 iText 的问题?Acrobat 显示创建的 PDF 文档使用 Helvetica 和 Ansi 编码和 ArialMT 作为字体.我觉得这个 Ansi 编码不好.如何设置波兰语编码 (Identity-H)?
The second problem is that result PDF do not have Polish letters. Is it problem in Flying Saucer or in iText? Acrobat shows that created PDF document uses Helvetica with Ansi encoding and ArialMT as font. I think this Ansi encoding is not good. How can I set Polish encoding (Identity-H)?
我的错误是在 PDFCreationListener.preOpen()
中使用了 FontResolver.addFont()
.我在 renderer.layout();
之前移动了它,现在它可以工作了!
My mistake was to use FontResolver.addFont()
in PDFCreationListener.preOpen()
. I moved it just before renderer.layout();
and it works now!