Simpler way to set fonts/set default encoding to Unicode/IDENTITY-H for PDFs?

79 views
Skip to first unread message

Paul Pop

unread,
Oct 22, 2024, 5:06:59 AM10/22/24
to Flying Saucer Users
Hello, do you know of any simple approach to set the default text encoding to Unicode for the iText PDF renderer other than adding an external font with the IDENTITY_H option in `.addFont(...)`? Docs reference for the current approach: The Flying Saucer User's Guide

I also tried adding multiple fonts at once via `renderer.getFontResolver.addFontDirectory`. However, when the PDF is generated, the encoding used is still the default one, ANSI/Latin-1, even if the font files would support Unicode, and the method has no parameter to set the encoding.
fonts.jpg

I'm thinking to contribute a PR to have an overloaded `addFontDirectory` where the encoding can be specified (or maybe we can even have Unicode set for the renderer by default?). The one that is called now I see then calls `addFont` with `Cp1252` as encoding.

PS: @Andrei / @Peter, this project has been very useful to me. If you have some crypto wallet(s) for the project, I'll happily chip in the price of a coffee :)

Paul Pop

unread,
Oct 23, 2024, 3:50:02 AM10/23/24
to Flying Saucer Users
Also, is it okay to reuse an ITextRenderer? If I generate multiple PDFs from (different) HTML, how should I clean up the renderer's document between successive runs (also to save memory)?
Thanks.

Andrei Solntsev

unread,
Oct 26, 2024, 5:37:55 AM10/26/24
to flying-sa...@googlegroups.com
Hi Paul!

1. For adding a single font with a specific encoding, there is a method:
public void addFont(String path, String encoding, boolean embedded)
public void addFont(String path, String encoding, boolean embedded, @Nullable String pathToPFB)
2. For adding multiple fonts, I've added method with "encoding" parameter:
public void addFontDirectory(String dir, boolean embedded)
public void addFontDirectory(String dir, String encoding, boolean embedded)

It's only in the "main" branch for now. Not released yet.

3. About reusing ITextRenderer instance - I don't know the right answer, but in our projects, we always create a new instance of ITextRenderer for every new PDF.

Andrei Solntsev


вт, 22 окт. 2024 г. в 12:07, Paul Pop <paul.po...@gmail.com>:
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/flying-saucer-users/886220cf-a10c-4506-a2cb-2a920dac9530n%40googlegroups.com.

Andrei Solntsev

unread,
Oct 26, 2024, 5:39:44 AM10/26/24
to flying-sa...@googlegroups.com
P.S. I have zero knowledge about crypto wallets. I am too old for this shit. :)

But there is GitHub sponsors program: https://github.com/sponsors/asolntsev

Andrei Solntsev


сб, 26 окт. 2024 г. в 12:37, Andrei Solntsev <andrei....@gmail.com>:

Andrei Solntsev

unread,
Oct 28, 2024, 6:53:46 PM10/28/24
to flying-sa...@googlegroups.com
Hi Paul!
FlyingSaucer 9.10.2 has been released.
You can use the new method addFontDirectory(String dir, String encoding, boolean embedded).

Andrei Solntsev


сб, 26 окт. 2024 г. в 12:37, Andrei Solntsev <andrei....@gmail.com>:
Reply all
Reply to author
Forward
0 new messages