如何在使用 iText 和飞碟从 HTML 创建的 PDF 中嵌入字体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7525403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to embed font in PDF created from HTML with iText and Flying Saucer?
提问by Micha? Niklas
I have problem with embedding Polish fonts into PDF converted from HTML.
我在将波兰语字体嵌入从 HTML 转换的 PDF 时遇到问题。
My HTML code have style in body:
我的 HTML 代码在正文中有样式:
<BODY style="font-family: Tahoma, Arial, sans-serif;font-size : 8pt;">
I tried 2 ways of converting such HTML into PDF:
我尝试了两种将此类 HTML 转换为 PDF 的方法:
- FOP with htmlcleaner
- iText with flying-saucer
- 带有 htmlcleaner 的 FOP
- 带有飞碟的 iText
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.
对于 FOP,我可以将所有使用的字体添加到其配置文件中,然后创建的 PDF 嵌入这些字体(如果字体用于 HTML)。在生成的 PDF 中,我在 Identity-H 编码中有 Tahoma 字体。看起来不错——所有波兰语字母都按预期显示。
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.
然后我尝试使用 iText: 进行这种转换:看起来更简单,因为我不需要为每个 HTML 创建转换。不幸的是,我不知道如何将使用过的字体嵌入到生成的 PDF 中。我发现的大多数示例都是从头开始创建 PDF,我不知道如何将这些方法应用于飞碟 ITextRenderer 或其他用于转换的对象。
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.
我当前的代码尝试PDFCreationListener.preOpen()
通过获取ITextFontResolver
和添加 font 来添加字体fs.addFont(path, true);
。但是我创建的所有 .pdf 都没有我想要的字体。
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)?
第二个问题是结果PDF没有波兰语字母。是飞碟还是 iText 的问题?Acrobat 显示创建的 PDF 文档使用 Helvetica 和 Ansi 编码和 ArialMT 作为字体。我觉得这个 Ansi 编码不好。如何设置波兰语编码 (Identity-H)?
采纳答案by Micha? Niklas
My mistake was to use FontResolver.addFont()
in PDFCreationListener.preOpen()
. I moved it just before renderer.layout();
and it works now!
我的错误是FontResolver.addFont()
在PDFCreationListener.preOpen()
. 我之前移动过它renderer.layout();
,现在可以使用了!
回答by Adam
You may try the -fs-pdf-font-embed, and -fs-pdf-font-encoding css rules.
你可以试试 -fs-pdf-font-embed 和 -fs-pdf-font-encoding css 规则。
From the User's Guide:
从用户指南:
-fs-pdf-font-embed:use with the value embed inside a font-face rule to have Flying Saucer embed a font file within a PDF document, avoiding the need to call the addFont() method of the FontResolver class
-fs-pdf-font-encoding:use inside a font-face rule to specify the enconding for a custom font you are embedding inside a PDF; takes the name of the encoding as value.
-fs-pdf-font-embed:与font-face规则中嵌入的值一起使用,让飞碟在PDF文档中嵌入字体文件,避免调用FontResolver类的addFont()方法
-fs-pdf-font-encoding:在 font-face 规则中使用来指定嵌入 PDF 中的自定义字体的编码;将编码的名称作为值。
For example in your print css:
例如在您的打印 css 中:
@font-face {
font-family: DejaVu Serif;
src: url(fonts/DejaVuSerif.ttf);
-fs-pdf-font-embed: embed;
-fs-pdf-font-encoding: Identity-H;
}
回答by Marcin Kunert
Working example:
工作示例:
Files in the root project directory:
项目根目录下的文件:
- Calibri.ttf
- input.html
- 校准文件
- 输入.html
Code:
代码:
File inputFile = new File("input.html");
File outputFile = new File("example.pdf");
ITextRenderer renderer = new ITextRenderer();
String url = inputFile.toURI().toURL().toString();
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
renderer.setDocument(url);
renderer.getFontResolver().addFont("Calibri.ttf", BaseFont.IDENTITY_H, true);
renderer.layout();
renderer.createPDF(fileOutputStream);
fileOutputStream.close();
HTML:
HTML:
<style type="text/css">
body {
font-family: Calibri, sans-serif;
}
</style>
Surprisingly @font-face
css is not needed
令人惊讶的@font-face
是不需要 css