How to embed font in PDF created from HTML with iText and Flying Saucer?

8,877 views
Skip to first unread message

michal niklas

unread,
Sep 28, 2011, 3:59:33 AM9/28/11
to Flying Saucer Users
Hello,

This is copy of my question on StackOverflow:
http://stackoverflow.com/questions/7525403/how-to-embed-font-in-pdf-created-from-html-with-itext-and-flying-saucer

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

Regards,
Michał

Peter Brant

unread,
Oct 6, 2011, 7:00:54 PM10/6/11
to flying-sa...@googlegroups.com
Hi Michal,

Take a look at ITextRenderer#getFontResolver(). Basically you need to
make the font known to both iText and FS to use it. An alternative is
to use @font-face in the CSS.

Pete

michal niklas

unread,
Oct 10, 2011, 6:17:12 AM10/10/11
to Flying Saucer Users
Hi,

> Take a look at ITextRenderer#getFontResolver().  Basically you need to
> make the font known to both iText and FS to use it.  An alternative is
> to use @font-face in the CSS.

Thanks for reply!

I tried with code where in bfList I have full paths to .ttf files (i.e
C:\fonts\timesbi.ttf):

private static void registerFonts(List<String> bfList,
ITextFontResolver fs)
{
for (String path: bfList)
{
HDBLog.warn("LoadFont:" + path);
FontFactory.register(path);
BaseFont baseFont;
try
{
baseFont = BaseFont.createFont(path, BaseFont.IDENTITY_H,
BaseFont.EMBEDDED);
baseFont.setSubset(true);
fs.addFont(path, true);
}
catch (DocumentException e)
....

In my .html source to convert I have style with Tahoma font and I
added also -fs* to help Flyins Saucer.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>pliterki utf-8</title>
<style>
@font-face {
font-family: Tahoma;
font-size: 10pt;
src: url(/fonts/Arial.ttf);
-fs-pdf-font-embed: embed;
-fs-pdf-font-encoding: Identity-H;
}
</style>
</head>
<body>
<p>
Test konwersji tabeli z polskimi literami ąćęłńóśźżĄĆĘŁŃÓŚŹŻ na PDF
</p>

Unfortunately, this do not work, ie. in created .pdf file I have only
4 basic fonts,
all of them with Ansi encoding.

Regards,
Michał

Peter Brant

unread,
Oct 10, 2011, 6:27:50 PM10/10/11
to flying-sa...@googlegroups.com
Hi Michal,

The BaseFont and FontFactory calls aren't necessary. FS will make the
necessary arrangements (i.e. the #addFont() call is enough). There
are overloaded variations of #addFont() that allow to set the
encoding, whether or not the font is embedded, etc.

With the @font-face block, url() needs to be a valid URL/URI (e.g. a
file:// URL or a relative URL).

Regardless you need to reference the font in your CSS or X/HTML (e.g.

<p style="font-family: MyFontFamily">...</p>

Pete

shikha tyagi

unread,
Oct 11, 2011, 2:44:40 PM10/11/11
to flying-sa...@googlegroups.com
Hi Michael,
                   Peter is right, you only need to add .ttf file in itextFontResolver class using addFont method of itextFontResolver class and
                   also add declaration of that font family in html
                   I had also faced this kind of problem in my project.

Thanks
Shikha

michal niklas

unread,
Oct 17, 2011, 3:03:50 AM10/17/11
to Flying Saucer Users
Hello Peter and Shikha, thanks for your help. I rearranged code and it
works now.

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

Thanks,
Michał

Geert Coelmont

unread,
Dec 3, 2013, 7:11:51 AM12/3/13
to flying-sa...@googlegroups.com
Note that the font-family name you use in the CSS must correspond to the embedded name inside the TTF file, you can't just make one up.

Op maandag 17 oktober 2011 09:03:50 UTC+2 schreef michal niklas:

mat

unread,
Dec 29, 2013, 5:58:42 PM12/29/13
to flying-sa...@googlegroups.com
Hi, my first post in this group :-)

Is it correct that only the TTF-format is supported?

Peter Brant

unread,
Dec 30, 2013, 5:49:30 AM12/30/13
to flying-sa...@googlegroups.com
Font support is provided by iText so it supports the same fonts as iText.  TTF (including collections), Type-1, and CJK fonts (as distributed with the Asian font pack for Acrobat).

Pete




--
You received this message because you are subscribed to the Google Groups "Flying Saucer Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flying-saucer-u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages