如何使用iText和Flying Saucer从HTML创建的PDF中嵌入字体? [英] How to embed font in PDF created from HTML with iText and Flying Saucer?

查看:544
本文介绍了如何使用iText和Flying Saucer从HTML创建的PDF中嵌入字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将波兰语字体嵌入到从HTML转换而来的PDF中存在问题。



我的HTML代码在主体中具有样式:

 < BODY style =font-family:Tahoma,Arial,sans-serif; font-size:8pt;> 

我尝试了2种将这种HTML转换为PDF格式的方法:


  • FOP with htmlcleaner

  • iText with flying-saucer



对于FOP,我可以将所有使用的字体添加到其配置文件中,然后创建PDF以嵌入这些字体(如果字体在HTML中使用)。在生成的PDF中,我使用了Identity-H编码中的Tahoma字体。它看起来很不错 - 所有的波兰语字母都按预期显示。

然后,我尝试用iText进行这种转换:似乎更简单,因为我不需要为每个HTML创建转换。不幸的是,我不知道如何在生成的PDF中嵌入使用的字体。我发现的大多数示例都是从头开始创建PDF,我不知道如何将这些方法应用到Flying Saucer ITextRenderer或用于转换的其他对象。



我当前的代码尝试通过获取 ITextFontResolver 并添加字体 fs.addFont,在 PDFCreationListener.preOpen()中添加字体(path,true); 。但所有.pdf我创建没有我想要的字体。



第二个问题是,结果PDF没有波兰字母。飞碟或iText中有问题吗? Acrobat显示创建的PDF文档使用Helvetica和Ansi编码,ArialMT作为字体。我认为这个Ansi编码不好。如何设置波兰语编码(Identity-H)?

解决方案

我的错误是使用 FontResolver。在 PDFCreationListener.preOpen()中添加addFont()。我在 renderer.layout(); 之前移动了它,它现在可以工作了!


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

解决方案

My mistake was to use FontResolver.addFont() in PDFCreationListener.preOpen(). I moved it just before renderer.layout(); and it works now!

这篇关于如何使用iText和Flying Saucer从HTML创建的PDF中嵌入字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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