Not finding font Arial.ttf with apache fop 1.0

jahuer1 picture jahuer1 · Mar 16, 2012 · Viewed 8.5k times · Source

I use:

  • Apache fop 1.0
  • Java

I need to handle with special characters which unicode is higher than u0100. The target font I should use is Arial. As I cannot expect Arial is present on the target platform (eg. Linux), I embedded an Arial.ttf in my jar. My actual config-file looks like this:

<fop ...>
<!-- Base URL for resolving relative URLs -->
<base>./</base>

<!-- Font Base URL for resolving relative font URLs -->
<font-base>./</font-base>

<renderers>
    <renderer mime="application/pdf">
        <fonts>

            <font kerning="yes" embed-url="arial.ttf" encoding-mode="auto">
                <font-triplet name="Arial" style="normal" weight="normal"/>
                <font-triplet name="ArialMT" style="normal" weight="normal"/>
            </font>

            <!--<directory>C:/Windows/Fonts</directory>-->

        </fonts>
        <auto-detect/>
    </renderer>
 ...
</fop>

Doing it this way I get an error loading my config-file to fop:

org.apache.fop.apps.FOPException: Failed to resolve font with embed-url 'arial.ttf'

The only way I managed to get it work until now was to hardcode the folder path into the config file:

            <font kerning="yes" embed-url="C:/myapp/arial.ttf" encoding-mode="auto">
                <font-triplet name="Arial" style="normal" weight="normal"/>
                <font-triplet name="ArialMT" style="normal" weight="normal"/>
            </font>

But obviously this cannot be the solution!

Using the "directory" tag whould work on Windows, just because you have Arial there. But as mentioned above it must also run on Linux, Macs, etc.

The "auto-detect" tag didn't work either (even not on Windows) - and isn't a solution as I cannot expect the target platform having Arial installed.

Any suggestions to solve this?

Answer

jahuer1 picture jahuer1 · Mar 20, 2012

I finally found the answer. See: http://www.publicstaticfinal.de/2011/01/26/fop-embedding-fonts-from-classpath/

The solution is to add an own URIResolver to the fopFactory.