如何使用 iText 和 Flying Saucer 在从 HTML 创建的 PDF 中嵌入字体?
我在将波兰语字体嵌入从 HTML 转换为 PDF 时遇到问题。
我的 HTML 代码在正文中具有样式:
<BODY style="font-family: Tahoma, Arial, sans-serif;font-size : 8pt;">
我尝试了两种将此类 HTML 转换为 PDF 的方法:
- FOP 与 htmlcleaner
- iText 与飞碟
对于 FOP,我可以将所有使用的字体添加到其配置文件中,然后创建 PDF 嵌入这些字体(如果字体用于 HTML)。在生成的 PDF 中,我有 Identity-H 编码的 Tahoma 字体。看起来不错——所有波兰语字母都按预期显示。
然后我尝试使用 iText 进行此类转换:似乎更简单,因为我不需要为每个 HTML 创建转换。不幸的是,我不知道如何将使用过的字体嵌入到生成的 PDF 中。我发现的大多数示例都是从头开始创建 PDF,我不知道如何将这些方法应用于 Flying Saucer ITextRenderer 或转换中使用的其他对象。
我当前的代码尝试通过获取 ITextFontResolver
并添加字体 fs.addFont(path, true);
来在 PDFCreationListener.preOpen()
中添加字体。但我创建的所有 .pdf 都没有我想要的字体。
第二个问题是结果 PDF 没有波兰语字母。是飞碟问题还是iText问题? Acrobat 显示创建的 PDF 文档使用带有 Ansi 编码的 Helvetica 和 ArialMT 作为字体。我觉得这个Ansi编码不好。如何设置波兰编码(Identity-H)?
I have problem with embedding Polish fonts into PDF converted from HTML.
My HTML code have style in body:
<BODY style="font-family: Tahoma, Arial, sans-serif;font-size : 8pt;">
I tried 2 ways of converting such HTML into PDF:
- FOP with htmlcleaner
- iText with flying-saucer
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.
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.
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.
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试 -fs-pdf-font-embed 和 -fs-pdf-font-encoding css 规则。
来自用户指南:
例如在你的打印CSS中:
You may try the -fs-pdf-font-embed, and -fs-pdf-font-encoding css rules.
From the User's Guide:
For example in your print css:
工作示例:
根项目目录中的文件:
代码:
HTML:
令人惊讶的是不需要
@font-face
cssWorking example:
Files in the root project directory:
Code:
HTML:
Surprisingly
@font-face
css is not needed我的错误是在
PDFCreationListener.preOpen()
中使用FontResolver.addFont()
。我把它移到了 renderer.layout(); 之前,现在它可以工作了!My mistake was to use
FontResolver.addFont()
inPDFCreationListener.preOpen()
. I moved it just beforerenderer.layout();
and it works now!如果您已尝试了所有选项但仍然不起作用,则很可能是
font-family
值与文件名不匹配的问题。您可以找出正确的值使用 FontForge。在此程序中打开字体文件,然后选择菜单项
Element ->字体信息
。正确的值将位于Family Name
字段中所需的最低 html 代码:
所需的最低 java 代码:
If you've tried all the options and it still doesn't work, it's most likely a problem with with a
font-family
value that doesn't match the file nameYou can find out the correct value using FontForge. Open font file in this program, then select the menu item
Element -> Font Info
. The correct value will be in theFamily Name
fieldMinimum required html code:
Minimum required java code: