将字体嵌入Apache FOP

时间:2013-11-04 11:54:59

标签: fonts apache-fop

我想让Apache FOP使用Arial作为字体。到目前为止,没有运气。

在我的config.xml中,它说:

<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>

但它似乎没有用。当我在命令行上启动FOP时,它表示找不到字体Arial normal。

我曾尝试使用FOP中的TTFReader从ttf生成Arial.xml文件,但这也没有奏效(甚至按照fop网页上的说明操作)。 我收到以下错误消息:

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)

也许我没有看到树木的福雷斯特,但我不知道该怎么做。

有什么建议吗?

UDPDATE:

我把它们放在文件夹fop / src / codegen

我也把arial.tff放在那里

文件fop / conf / xconf中的

表示

<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>

所以我得到了自动检测并为字体和嵌入式arial指定了一个目录 - stil我收到错误信息,例如“字形xyz不包含在Helvetica中”,尽管我的样式表中没有任何地方说它是字体系列Helvetica。 pdf以Times New Roman ...

显示

++ UPDATE ++

好了...

我的fop.xconf文件现在说:

<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>

我现在为几乎所有的元素添加了font-family =“Arial”:D

我在命令行中转到我的fop目录并输入

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

我得到以下内容:

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.

不介意网址。

5 个答案:

答案 0 :(得分:9)

我遇到了同样的问题,使用.NET中的FopFactory并使用Apache doc上的java示例解决了它: http://xmlgraphics.apache.org/fop/1.1/embedding.html#config-external

我使用以下代码创建了一个配置文件:

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

然后添加了信息FopFactory:

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

“自动检测”选项用于检测系统上的所有字体,可能需要一段时间,输出的大小很大。 要从系统配置特定字体,请参阅上面链接中的文档。

答案 1 :(得分:2)

我有完全相同的问题(虽然在Windows上有s / Arial / Verdana /和fwiw - fop-2.1)。在尝试了各种各样的事情,然后退后一步之后,我发现我犯的错误与我们中的许多人一样 -

  1. 我正在编辑提供/示例配置文件,该文件具有非默认扩展名,因此不会被fop自动使用,即fop.xconf
  2. fop.bat(或任何其他提供的平台调用脚本)使用-c fop.xconf调用org.apache.fop.cli.Main,或使用任何.conf调用-c文件,因此不使用对fop.xconf的任何更改
  3. 对我有用的解决方案是 -

    1. 创建conf\fop.conf
    2. 点击configuration to enable auto-detect for pdf fonts inside conf\fop.conf

    3. 确保您向fop提供conf文件,即使用-c选项调用fop.bat(或其他平台脚本),例如 -

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

      正如我之前所说的那样,我们并不是唯一一个因此而被抓住的人, -

      How can I embed a base14 font in a pdf

      Embedded font don't work in Apache FOP

      恕我直言 - 如果Apache FOP与调用脚本中已经引用的空fop.conf一起分发,我认为我们不会被这一切搞定。

答案 2 :(得分:1)

由于历史原因,Helvetica是postscript的默认字体,FOP PDF渲染基于该字体。结果,如果您没有在XSL模板中明确声明字体,FOP将尝试使用Helvetica。

所以我的问题是FOP在生成PDF / A文档时尝试嵌入Helvetica字体,但Helvetica从未在模板中声明,也未安装在我的Linux系统上。

解决方案很简单,我在我的fopconf.xml中映射了Helvetica的替换,并使LiberationSans成为我的默认字体。示例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>

使用其他字体的模板的替代解决方案是直接在根元素上设置font-family,如<fo:root font-family="LiberationSans">

希望我的回答能为这个问题增添一些额外的价值。

[更新]我可能会补充说,我正在使用FOP 2.1 [/ UPDATE]

答案 3 :(得分:0)

“ClassNotFoundException”表示您有类加载问题。鉴于它缺少“org.apache.xmlgraphics.fonts.Glyphs”,这意味着你的类路径中没有xmlgraphics-commons.jar(或过时的版本)。你会在FOP的“lib”目录中找到它。

答案 4 :(得分:0)

我只是想在这里为那些仍然想知道这一点的人添加一个部分解决方案,要生成xml文件,还需要将xmlgraph_commons-1.4.jar添加到类路径中。

此外,应用程序的路径是org.apache.fop.fonts.apps.TTFReader,而不是OP发布的内容,至少在旧版本的fop中。

这是一个有效的导出

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
相关问题