Embedding Font into Apache FOP

六月ゝ 毕业季﹏ 提交于 2019-12-18 12:38:58

问题


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.


回答1:


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.




回答2:


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.




回答3:


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]




回答4:


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.




回答5:


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


来源:https://stackoverflow.com/questions/19767205/embedding-font-into-apache-fop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!