Adding support for Font declarations

194 views
Skip to first unread message

Per Nyfelt

unread,
Apr 26, 2024, 2:56:18 PM4/26/24
to Flying Saucer Users
Hi,

I think it would be nice if font declarations in the html were automatically handled by flying saucer when creating a PDF instead of having to add fonts explicitly to the renderer.

I looked for a way to hook in such handling, similar to how we can add a ReplacedElementFactory e.g. to deal with e.g. SVG content but did not find a way so i ended up pre-parsing the html and add the fonts specified in all @font-face declarations.

It would be nice if this was supported out of the box though.
Do you think it would be a good idea to add the ability to add style sheet rule actions e.g:

renderer.addStyleSheetRuleListener("@font-face", ruleListener)

where a ruleListener would be something like:

public interface StyleSheetRuleListener {
  void onRule(Properties ruleProperties);
}

This would enable a user to do any type of action (such as adding a font) based on the style rules.

Given an html with the following font declaration:
```html
<style>
  @font-face {
    font-family: "Jersey 25";
    src: url(file:///usr/local/fonts/Jersey25-Regular.ttf);
}
```
The rule action would be called once with the Properties object containing two properties (one for the font-family and one for the src)

This would have to happen before layout() though (e.g. in . setDocumentFromString) When i tried to add handling code in the ReplacedElementFactory it was too late (the font was not rendered properly).

An alternative to this approach would be to add a configuration option e.g:
var cfg = new Configuration(Configuration.VERSION_2_3_32);
cfg.setAddCssDeclaredFonts(true);
... and handle the font additions without the user having to to anything else.

What do you think?
Regards,
Per

Andrei Solntsev

unread,
Apr 29, 2024, 11:57:42 PM4/29/24
to flying-sa...@googlegroups.com
Hi Per.
It seems an absolutely valid idea to load fonts from @font-face declaration just by default,  even without any configuration. 

Without the user having to to anything else.



--
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/a0f39a85-f501-44a5-9a49-5ca2b32b41efn%40googlegroups.com.

Per Nyfelt

unread,
Apr 30, 2024, 6:56:10 AM4/30/24
to Flying Saucer Users
After some deeper investigation, I found that this is actually already supported 
Line 240 in ITextRenderer will import font face declarations (deferring to ITextFontResolver#importFontFaces)
The reason "it was not working" for me was that the fonts are not embedded in the PDF by default so by adding the -fs-pdf-font-embed: embed; property to the font-face declaration everything works

e.g:
@font-face {
font-family: "Jacquard 24";
src: url("../../../../fonts/Jacquard24-Regular.ttf");
-fs-pdf-font-embed: embed;
}

So I missed an opportunity to contribute my first patch but I'm sure there will be other opportunities ;)

Best regards,
Per
Reply all
Reply to author
Forward
0 new messages