Skip to content
Advertisement

Apache FOP embedded remote fonts

I’m using Apache FOP v2.1 embedded in a Java program. I’m trying to retrieve a remote font using a configuration file similar to the following:

<?xml version="1.0"?>
<fop version="1.0">
<renderers>
    <renderer mime="application/pdf">
        <fonts>
            <font kerning="yes"
                  embed-url="http://localhost:8000/MyFont.ttf" embedding-mode="subset">
                <font-triplet name="MyFont" style="normal" weight="normal"/>
            </font>
        </fonts>
    </renderer>
</renderers>
</fop>

However, when I run the application to generate a PDF file from XML and XSL files it gives me an error:

java.lang.IllegalArgumentException: URI scheme is not "file"
at java.io.File.<init>(File.java:421)
at org.apache.fop.fonts.FontCache.addFont(FontCache.java:335)
at org.apache.fop.fonts.DefaultFontConfigurator.getFontInfo(DefaultFontConfigurator.java:173)
at org.apache.fop.fonts.DefaultFontConfigurator.addFonts(DefaultFontConfigurator.java:136)
at org.apache.fop.fonts.DefaultFontConfigurator.configure(DefaultFontConfigurator.java:89)
at org.apache.fop.render.PrintRendererConfigurator.getCustomFontCollection(PrintRendererConfigurator.java:147)
at org.apache.fop.render.PrintRendererConfigurator.setupFontInfo(PrintRendererConfigurator.java:127)
at org.apache.fop.render.intermediate.IFUtil.setupFonts(IFUtil.java:170)
at org.apache.fop.render.intermediate.IFRenderer.setupFontInfo(IFRenderer.java:187)
at org.apache.fop.area.RenderPagesModel.<init>(RenderPagesModel.java:75)
at org.apache.fop.area.AreaTreeHandler.setupModel(AreaTreeHandler.java:135)
at org.apache.fop.area.AreaTreeHandler.<init>(AreaTreeHandler.java:105)
at org.apache.fop.render.RendererFactory.createFOEventHandler(RendererFactory.java:350)
at org.apache.fop.fo.FOTreeBuilder.<init>(FOTreeBuilder.java:107)
at org.apache.fop.apps.Fop.createDefaultHandler(Fop.java:104)
at org.apache.fop.apps.Fop.<init>(Fop.java:78)
at org.apache.fop.apps.FOUserAgent.newFop(FOUserAgent.java:182)
at org.apache.fop.apps.FopFactory.newFop(FopFactory.java:240)
at com.example.myapp.myFunction(MyApp.java:123)

By checking the source code of FOP I discovered that FOP is trying to add the font to a cache. However, because the font URI is an HTTP URL, when it tries to pass the URL to a java File object, it gives me the error “URI scheme is not file”.

Is there any step I’m missing to load a remote font?

Answer

The problem was the FontCache. It uses File Java class to read the content, and since the configuration file contains URL of http(s) protocol, it throws the mentioned exception. To solve the issue, you need to disable the font cache like this:

// load the config file with fonts from remote resources (HTTP)
File configFileWithRemoteFontUrl = new File("fop-config.xconf");

// create a factory instance and disable the font cache
FopFactory factory = FopFactory.newInstance(configFileWithRemoteFontUrl);
factory.getFontManager().disableFontCache();

// your code as usual...

By disabling the font cache your remote fonts should be loaded fine without aditional configuration.

Advertisement