Embedding Font into Apache FOP

我与影子孤独终老i 提交于 2019-11-30 07:36:18

I had the same Problem, using FopFactory from .NET and solved it whith the java example on the Apache doc: http://xmlgraphics.apache.org/fop/1.1/embedding.html#config-external

I created a config file with this code:

<?xml version="1.0" encoding="utf-8" ?>
 <fop>
   <renderers>
    <renderer mime="application/pdf">
     <fonts>
       <auto-detect/>
     </fonts>
    </renderer>
  </renderers>
 </fop>

Then added it info FopFactory:

FopFactory fopFactory = FopFactory.newInstance();
fopFactory.setUserConfig("fop.xconf");

The option "auto-detect" is for detecting all fonts on the system and it may takes a while and the output has a big size. To configure a specific font from the system, see the doc in the link above.

I had exactly the same issue (although s/Arial/Verdana/ and fwiw - fop-2.1 on Windows). After trying various things, and then taking a step back, i discovered i was making the same mistake as a number of us have -

  1. I was editing the supplied/example configuration file that has the non-default extension and therefore is not automatically used by fop i.e. fop.xconf
  2. fop.bat (or any of the other supplied platform invocation scripts) does not invoke org.apache.fop.cli.Main with -c fop.xconf, or -c with any .conf file, so any changes to fop.xconf are not used

The solution that worked for me was -

  1. create conf\fop.conf
  2. Put the configuration to enable auto-detect for pdf fonts inside conf\fop.conf

  3. Ensure you are supplying fop with the conf file i.e. call fop.bat (or other platform script) with the -c option e.g. -

fop.bat -fo path\to\fo.fo -c path\to\fop.conf -pdf path\to\output.pdf

As i was saying before, we're not alone in being caught out by this, see e.g. -

How can I embed a base14 font in a pdf

Embedded font don't work in Apache FOP

IMHO - I think we wouldn't have all got caught out by this if Apache FOP was distributed with an empty fop.conf that was already referenced in the invocation scripts.

For historical reason, Helvetica is the default font for postscript on which FOP PDF rendering is based off. As an outcome, if you do not explicit declare a font in your XSL templates, FOP will try to use Helvetica.

So my Problem was FOP trying to embed Helvetica font when generating a PDF/A document, but Helvetica is never declared in templates nor installed on my Linux system.

Solution was simple, I mapped a substitution for Helvetica in my fopconf.xml and made LiberationSans my default font that way. Example fopconf.xml:

<fop version="1.0">
    <renderers>
        <renderer mime="application/pdf">
        <fonts>
            <!-- auto-detect operating system installed fonts -->
            <auto-detect />

            <!-- substitute default unfree font Helvetica with free font LiberationSans 
                (which is an Arial Clone, which is an Helvetica Clone) -->
            <substitutions>
                <substitution>
                    <from font-family="Helvetica" />
                    <to font-family="LiberationSans" />
                </substitution>
            </substitutions>
        </fonts>
        </renderer>
    </renderers>
</fop>

The alternative solution for templates that do use other fonts, was setting the font-family directly on the root element like <fo:root font-family="LiberationSans">.

Hope my answer adds some additional value to this question.

[UPDATE]I might add, that I am using FOP 2.1[/UPDATE]

The "ClassNotFoundException" indicates that you have a class loading problem. Given that it's missing "org.apache.xmlgraphics.fonts.Glyphs", it means you don't have xmlgraphics-commons.jar in your classpath (or an outdated version). You'll find that in FOP's "lib" directory.

I just thought I would add a partial solution here for people who are still wondering about this, to generate the xml file, you also need to add xmlgraph_commons-1.4.jar to the classpath.

Also, the path to the application is org.apache.fop.fonts.apps.TTFReader, not what the OP posted, at least in older versions of fop.

Here is a working export

java -cp build\fop.jar;lib\avalon-framework-4
.2.0.jar;lib\commons-logging-1.0.4.jar;lib\commons-io-1.3.1.jar;lib\xmlgraphics-
commons-1.4.jar org.apache.fop.fonts.apps.TTFReader C:\Windows\Fonts\arial.ttf A
rial.xml
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!