cajuncoding/ApacheFOP.Serverless

Recommendation for custom font inclusion.

MichaelYabko opened this issue ยท 5 comments

Hi there,

I was wondering if you could make any recommendations for embedding custom fonts when the service is running in Azure.

I'm guessing that you mean to ask about how to use custom fonts in your XslFO Markup so they show up in the PDF; because embedding them in the PDF is a different topic and all fonts used are embedded by default in the output PDF as described here in the documentation.

Per the Apache FOP documentation you can register fonts (custom fonts) with Apache FOP for reference within your XslFO markup:
https://xmlgraphics.apache.org/fop/2.1/fonts.html#register

In addition there are more details here where they provide all configuration elements, whereby you can set the base path via font-base param for all Fonts paths to be relative to when loading/registering.

So with the configuration of those elements then you can register any font you like with ApachFOP when it loads.

Specifically in ApacheFOP.Serverless, the configuration xml file is loaded automatically from a resource file that is deployed with the Java Azure Function via the resources folder; you'll find it in the project path: /src/main/resources/apache-fop-config.xml

This file is loaded by the framework automatically and provided to ApacheFOP upon initialization.

So, Ideally we could drop in custom Font files into the resource folder as src/main/resource/{fontfile.ttf}, and then configure that path correctly in the apache-fop-config.xml file so it is found by ApacheFOP per the documentation.

_EDIT: The resources are deployed embedded in the JAR file, not to the filesystem, and after fiddling with Maven I realized that build time copying won't work... but runtime resolving might . . . _

Thus far I've not tried/validated this myself, so let me know how you get along with this . . . eventually when I get some time (may not be this week) I can try to test/validate with an example -- but the crux will just be determining what the full file path to font files would be after being deployed to Azure Functions and/or copying them out somewhere?

Good Luck!

@MichaelYabko

Ok, so getting the custom fonts to work definitely wasn't as straightforward as I hoped, but with a little more help now it is ๐Ÿš€

I haven't had a need for non-standard fonts yet, but it's something I wanted to get working anyway as it's definitely a critical feature.

Initially I thought that some form of the auto-detect would work -- and it did, but with the risk of access permission issues, etc. once deployed to the Azure Functions service I ended up identifying a more robust approach . . . by providing ApacheFOP with a custom ResourceResolver that now dynamically resolves requests from Java resources that are easily deployed within the JAR file.

This makes adding fonts, configuring them, and deploying the package a breeze ๐Ÿ™Œ

I've pushed the latest v1.2 (with readme updates)... which now supports a much easier way to include your TTF/OTF/etc. custom font files, and easily register them in configuration.

More details are now in the readme here: https://github.com/cajuncoding/ApacheFOP.Serverless#custom-fonts-via-resource-files

It's definitely working, here's a sample from my personal deployment in Azure via Postman:
image

This is really cool @cajuncoding. One other thing that we also found works was to include the fonts in the config as a url. Then we setup an azure storage link for each of the fonts. Gonna try this out shortly.

Thanks ๐Ÿ‘

works great :-)

Yes, that's correct, I can now confirm that if you have font file directly downloadable anonymously by url then the original/default resource resolver will work. I've now learned that It is the same resolver used for all external resources when Apache FOP processes it's configuration as well as when rendering the markup (e.g. images)!

Though it may not be impactful there could definitely be a performance impact with that approach, that would be minimized by embedding them with the JAR. ๐Ÿš€