Jan Vladimir Mostert
Jan Vladimir Mostert

Reputation: 12992

Apache FOP 2.2 Include Fonts using Relative Path

I'm setting up a fop.xconf and configuring which fonts are available to Apache FOP and would like to use a relative path rather than an absolute path:

<fop version="1.0">

    <strict-configuration>true</strict-configuration>
    <strict-validation>true</strict-validation>
    <base>./</base>
    <font-base>./</font-base>
    <source-resolution>72</source-resolution>
    <target-resolution>72</target-resolution>
    <default-page-settings height="11in" width="8.26in"/>

    <hyphenation-pattern lang="en" country="ZA">en_ZA</hyphenation-pattern>

    <renderers>
        <renderer mime="application/pdf">
        <fonts>
            <directory recursive="true">/Users/me/Code/company/project/src/main/resources/templates/fonts</directory>
            <substitutions>
                <substitution>
                    <from font-family="Helvetica" />
                    <to font-family="OpenSans" />
                </substitution>
            </substitutions>
        </fonts>
        </renderer>
   </renderers>    
</fop>

Is it possible to set that fonts directory inside renderers to use a relative path?

Or is possible to set it programmatically in which case I could make use of

this::class.java.getResource("/templates/fonts/MyFont.ttf")

to get the full path?

2017-10-24 05:24:48,591 WARN  org.apache.fop.apps.FOUserAgent - The font directory ./fonts could not be found.
2017-10-24 05:24:48,591 WARN  org.apache.fop.apps.FOUserAgent - The font directory ./templates/fonts could not be found.
2017-10-24 05:24:48,591 WARN  org.apache.fop.apps.FOUserAgent - The font directory ./resources/templates/fonts could not be found.

Upvotes: 3

Views: 3032

Answers (3)

Kai
Kai

Reputation: 891

Although this is an old thread, I might add that even in FOP 2.3 the issue is still present. You might find this FOP Bug useful:

  • font-base has been removed from config (not in the docs)
  • use absolute paths

Upvotes: 2

Josel Pasilan
Josel Pasilan

Reputation: 26

I would say that this footnote in the FOP configuration documentation is the most helpful.

https://xmlgraphics.apache.org/fop/trunk/configuration.html#fnref:1

It states that all relative paths in the configuration will resolve to the <base> path. In your case, it includes the <font-base>. Also, the last bit of the footnote states that if <base> is in itself a relative path, it will by default resolve to the current working directory.

Basically, there would be two approaches so that the fonts directory will be properly resolved and be found by FOP.

  1. You can programmatically set the <base> path using the FopFactory.setUserConfigBaseURI API. That is if you're using Java.
  2. If you're not using Java, you can set the path relative to the current working directory, e.g. <base>.</base>, and within your program/script call a system function to change the current working directory to something that's known and will resolve the relative paths. Here's an example with PHP.

    // This would be in the PHP script.
    chdir("/var/www/html/site");
    exec("$path_to_fop -c $path_to_fop_xconf ...");
    
    // This would be how the <directory> element in fop.xconf would be written.
    // Since the current working directory is changed with chdir(), this should
    // resolve to /var/www/html/site/libraries/fop/fonts.
    <directory recursive="true">./libraries/fop/fonts</directory>
    

Upvotes: 1

Esteban Ibarra
Esteban Ibarra

Reputation: 9

put the URL = (/Users/me/Code/company/project/src/main/resources/templates/fonts) in <font-base>./</font-base> like this:

  • <font-base>/Users/me/Code/company/project/src/main/resources/templates/fonts</font-base>

And you solve the problem! ;)

Upvotes: 0

Related Questions