5

I am tring to get Apache FOP to use Arial as font. So far, without luck.

In my config.xml it says:

<fonts>                           
 <font kerning="yes"  embed-url="file:///C:/Windows/Fonts/Arial.ttf" encoding-mode="auto">>
<font-triplet name="Arial" style="normal" weight="normal"/>
 </font>                     
</fonts>

But it doesnt seem to work. When I start FOP on the command line it says font Arial normal not found.

I have tried to generate the Arial.xml file from the ttf with TTFReader in FOP but that, too, hasnt worked (even following the instructions on the fop webpage). I get the following error message:

C:\Users\xy\Desktop\FOP2>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 org.apache.fop.TTFReader C:\Windows\Fonts\arial.ttf Arial.xml
TTF Reader for Apache FOP 1.1

Parsing font...
Reading C:\Windows\Fonts\arial.ttf...
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlgraphics/fonts/Glyphs
        at org.apache.fop.fonts.truetype.TTFFile.initAnsiWidths(TTFFile.java:662)
        at org.apache.fop.fonts.truetype.TTFFile.readFont(TTFFile.java:711)
        at org.apache.fop.fonts.apps.TTFReader.loadTTF(TTFReader.java:224)
        at org.apache.fop.fonts.apps.TTFReader.main(TTFReader.java:177)
Caused by: java.lang.ClassNotFoundException: org.apache.xmlgraphics.fonts.Glyphs
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)

Maybe Im not seeing the forrest for the trees, but I'm at a loss as to what to do.

Any suggestions?

UDPDATE:

I put them in the folder fop/src/codegen

I also put the arial.tff there

in the file fop/conf/xconf it says

<fonts>

      <directory>C:\Windows\Fonts</directory>
        <!-- embedded fonts -->
        <!--
        This information must exactly match the font specified
        in the fo file. Otherwise it will use a default font.

        For example,
        <fo:inline font-family="Arial" font-weight="bold" font-style="normal">
            Arial-normal-normal font
        </fo:inline>
        for the font triplet specified by:
        <font-triplet name="Arial" style="normal" weight="bold"/>

        If you do not want to embed the font in the pdf document
        then do not include the "embed-url" attribute.
        The font will be needed where the document is viewed
        for it to be displayed properly.

        possible styles: normal | italic | oblique | backslant
        possible weights: normal | bold | 100 | 200 | 300 | 400
                          | 500 | 600 | 700 | 800 | 900
        (normal = 400, bold = 700)
        -->

        <font metrics-url="C:\Users\z003a5bp\Desktop\FOP2\src\codegen\fonts\arial.xml" kerning="yes" embed-url="C:\Users\z003a5bp\Desktop\FOP2\src\codegen\fonts\arial.ttf">
          <font-triplet name="Arial" style="normal" weight="normal"/>
          <!-- <font-triplet name="ArialMT" style="normal" weight="normal"/> -->
        </font>
       <!-- <font metrics-url="arialb.xml" kerning="yes" embed-url="arialb.ttf">
          <font-triplet name="Arial" style="normal" weight="bold"/>
          <font-triplet name="ArialMT" style="normal" weight="bold"/> -->

          <auto-detect/>
      </fonts>

So ive got auto-detec and specified a directory for the fonts and embedded arial - stil I get error messages like "glyph xyz is not included in Helvetica" although nowhere in my stylesheets does it say font-family Helvetica. The pdfs are displayed in Times New Roman...

++ UPDATE ++

Okay...

my fop.xconf file says this now:

<fop version="1.0">

  <base>.</base>

  <source-resolution>72</source-resolution>
  <target-resolution>72</target-resolution>
  <default-page-settings height="11in" width="8.26in"/>
  <renderers>
    <renderer mime="application/pdf">
      <filterList>
        <value>flate</value>
      </filterList>

      <fonts>

       <font metrics-url="file:///C:/Users/z003a5bp/Desktop/FOP2/src/codegen/fonts/arial.xml"
            kerning="yes"
            embed-url="file:///C:/Users/z003a5bp/Desktop/FOP2/arial.ttf">
        <font-triplet name="Arial" style="normal" weight="normal"/>
            </font> 

          <auto-detect/>
      </fonts>

    </renderer>

I've added font-family="Arial" to pretty much all the elements now :D

I go to my fop directory in the command line and enter

fop -xml blabla\file.xml -xsl blabla\stylesheet.xsl -pdf output.pdf

And i get the following:

Nov 07, 2013 1:34:29 PM org.apache.fop.events.LoggingEventListener processEvent
Schwerwiegend: Image not found. URI: 15300145419.PNG. (See position 44:-1)
Nov 07, 2013 1:34:29 PM org.apache.fop.events.LoggingEventListener processEvent
Warnung: The following feature isn't implemented by Apache FOP, yet: table-layout="auto" (on fo:table) (Siehe Position 29:-1)
Nov 07, 2013 1:34:29 PM org.apache.fop.events.LoggingEventListener processEvent
Schwerwiegend: Image not found. URI: 15627351051.PNG. (See position 44:-1)
Nov 07, 2013 1:34:29 PM org.apache.fop.events.LoggingEventListener processEvent
Schwerwiegend: Image not found. URI: 15629782795.PNG. (See position 44:-1)
Nov 07, 2013 1:34:29 PM org.apache.fop.events.LoggingEventListener processEvent
Warnung: Font "Arial,normal,400" not found. Substituting with "any,normal,400".
Nov 07, 2013 1:34:29 PM org.apache.fop.events.LoggingEventListener processEvent
Warnung: Glyph "?" (0x95) not available in font "Times-Roman".
Nov 07, 2013 1:34:29 PM org.apache.fop.events.LoggingEventListener processEvent
Warnung: Font "Arial,italic,400" not found. Substituting with "any,italic,400".
Nov 07, 2013 1:34:29 PM org.apache.fop.events.LoggingEventListener processEvent
Warnung: Content overflows the viewport of the fo:region-before on page 1 in block-progression direction by 2989 millipoints. (See position 50:-1)
Nov 07, 2013 1:34:29 PM org.apache.fop.events.LoggingEventListener processEvent
Information: Rendered page #1.
Nov 07, 2013 1:34:29 PM org.apache.fop.events.LoggingEventListener processEvent
Warnung: Content overflows the viewport of the fo:region-before on page 2 in block-progression direction by 2989 millipoints. (See position 59:-1)
Nov 07, 2013 1:34:29 PM org.apache.fop.events.LoggingEventListener processEvent
Information: Rendered page #2.

Dont mind the URLs.

user2791739
  • 217
  • 1
  • 4
  • 17

5 Answers5

9

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.

Zalem
  • 154
  • 1
  • 10
4

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.

jjee
  • 225
  • 1
  • 6
  • .xconf - nothing to do with "example", it is more possible that it stands for XML Configuration. name and extension do not matter as long as we are supplying this file as configuration. E.g. in the code that I am examining at the moment this file is named fop.xconf.xml – hello_earth Feb 28 '19 at 13:15
2

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]

mondjunge
  • 1,219
  • 14
  • 24
0

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.

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
Jeremias Märki
  • 1,727
  • 10
  • 10
  • Well, added this to my path-variable: C:\Users\z003a5bp\Desktop\FOP2\lib\commons-logging-1.1.3.jar I type the following into the console: C:\Users\z003a5bp\Desktop\FOP2>java -cp build\fop.jar;lib\avalon-framework-4.2.0.jar;lib\commons-logging-1.1.3.jar;lib\commons-io-1.3.1.jar org.apache.fop.TTFReader C:\Users\z003a5bp\Desktop\FOP2\arial.ttf Arial.xml all of these jars are in the respective folder in the versions that can be seen here but now I get the message that the main class TTFReader could not be found or loaded. – user2791739 Nov 07 '13 at 08:28
  • I should have seen it earlier: you don't need TTFReader anymore with FOP 1.0 or later. FOP can properly extract the right font metrics on the fly. Also, consider using the tag instead of to more easily configure fonts for FOP. See also http://xmlgraphics.apache.org/fop/1.1/fonts.html#bulk – Jeremias Märki Nov 07 '13 at 09:16
  • I've only just seen that you updated the question. If you use , it makes C:\Windows\Fonts redundant. To make sure you really don't use any Times/serif font, specify font-family="Arial" on fo:root. And finally, make sure that the tag is a child of the right format-specific tag. – Jeremias Märki Nov 07 '13 at 09:23
  • I updated the question again, in case you'd like to have another look at it. – user2791739 Nov 07 '13 at 13:19
  • OK, add "-c fop.xconf" to the command-line. Remove the metrics-url from the font config. Actually, you can remove the whole tag and just go with . – Jeremias Märki Nov 07 '13 at 14:12
  • Okay.. I think it kinda worked now... The only weird thing is that I still get the warning that glyph (0x95) is not available in Helvetica. Why Helvetica? Does FOP default to that if the specified font doesnt have that glyph either? And the second thing is: I need to use fop in Eclipse - how do I make fop use the fop.xconf then? I tried compiling fop with ant, which does work, but it doesnt find the Arial. You dont happen to know that? Plus, I need to make sure that the fonts are available on all computers. So maybe auto-detect isnt the best choice after all. – user2791739 Nov 08 '13 at 07:55
  • Helvetica is one of the "base 14" font set defined in the PDF spec. These fonts can always be expected to "be around". But some characters may not be available in these fonts. I have no idea what you mean by "using FOP in Eclipse". FOP is not an Eclipse plug-in. Anyway, that's not in scope of this question. This page has long gone away from being concise and about a single topic. Maybe it's better you go to the [fop-users mailing list](http://xmlgraphics.apache.org/fop/maillist.html#fop-user) for further help. Be sure to include enough information. – Jeremias Märki Nov 11 '13 at 18:47
  • Well, I built fop with ant and included the jar eclipse. thats what I meant by that. But I'll follow your suggestion and ask on the fop mailing list. Thanks! – user2791739 Nov 14 '13 at 11:58
0

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
techdude
  • 1,334
  • 20
  • 29