Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding Font into Apache FOP

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 downloaded a lot of xml-Files from this site: http://www.java2s.com/Code/Jar/f/Downloadfopfontmetricsjar.htm (which to me look a lot like the xml-files that were already in fop. so I assume they are in the correct format)

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.

like image 392
user2791739 Avatar asked Nov 04 '13 11:11

user2791739


2 Answers

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.

like image 89
Zalem Avatar answered Sep 29 '22 11:09

Zalem


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.

like image 35
jjee Avatar answered Sep 29 '22 10:09

jjee