5

我想让 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 not found。

我曾尝试在 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)

也许我没有看到森林的森林,但我不知道该怎么做。

有什么建议么?

更新日期:

  • 我从这个站点下载了很多 xml 文件: http ://www.java2s.com/Code/Jar/f/Downloadfopfontmetricsjar.htm (在我看来很像已经在 fop 中的 xml 文件。所以我认为它们的格式正确)

我把它们放在文件夹 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 指定了一个目录——尽管我的样式表中没有任何地方说字体系列 Helvetica,但我仍然收到诸如“glyph xyz 不包含在 Helvetica 中”之类的错误消息。pdf 以 Times New Roman 格式显示...

++更新++

好的...

我的 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.

不要介意网址。

4

5 回答 5

9

我有同样的问题,使用 .NET 中的 FopFactory 并通过 Apache 文档上的 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");

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

于 2014-03-26T17:35:32.877 回答
4

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

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

对我有用的解决方案是 -

  1. 创造conf\fop.conf
  2. 配置启用自动检测pdf字体conf\fop.conf

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

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

正如我之前所说,我们并不是唯一一个被这个抓住的人,例如 -

如何在 pdf 中嵌入 base14 字体

嵌入式字体在 Apache FOP 中不起作用

恕我直言——我认为如果 Apache FOP 分发时带有一个已在调用脚本中引用的空 fop.conf,我们就不会被这个问题所困扰。

于 2016-06-30T15:44:44.563 回答
2

由于历史原因,Helvetica 是 FOP PDF 渲染所基于的 postscript 的默认字体。结果,如果您没有在 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>

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

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

[更新]我可以补充一下,我使用的是 FOP 2.1[/更新]

于 2016-06-01T15:11:14.553 回答
0

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

于 2013-11-06T17:27:55.800 回答
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
于 2016-02-15T22:48:58.807 回答