当前位置: 首页 > 知识库问答 >
问题:

将字体嵌入Apache FOP

谢鸿羲
2023-03-14

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

也许我没有看到树木的阿甘,但我不知所措。

有什么建议吗?

更新日期:

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

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

我还放了咏叹调。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>

所以我得到了auto-detec,并为字体和嵌入的arial-stil指定了一个目录,我收到了类似“glyph xyz is not included in Helvetica”这样的错误html" target="_blank">消息,尽管在我的样式表中没有写字体家族Helvetic。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.

不要介意网址。

共有3个答案

罗允晨
2023-03-14

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

使用其他字体的模板的替代解决方案是直接在根元素上设置字体系列,例如

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

[更新]我可能会添加,我正在使用FOP 2.1[/更新]

左宁
2023-03-14

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

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

对我有效的解决方案是-

>

  • 创建 conf\fop.conf
  • 将配置以启用 pdf 字体的自动检测放在 conf\fop.conf

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

    fop.bat-fo路径\to\fo.fo-c路径\to\fop.conf-pdf路径\to\output.pdf

    正如我之前所说的,我们并不是唯一一个被这种情况所困扰的人,例如-

    如何在pdf中嵌入base14字体

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

    我认为,如果Apache FOP发布时带有一个空的fop.conf,并且在调用脚本中已经引用了它,我们就不会被这个问题困扰了。

  • 东郭海阳
    2023-03-14

    我遇到了同样的问题,使用.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");
    

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

     类似资料:
    • 我在apache fop 1.0环境中使用嵌入式字体: 配置文件如下所示: 这工作得很好,文本以字体hnlt57con呈现。 我现在想做的是用粗体呈现一些文本: 配置文件如下所示: 不幸的是,文本没有以粗体显示,但是以和上面例子一样的方式显示。 添加一个额外的字体三元组(如 http://www.scriptorium.com/whitepapers/fop_fonts/FOP_fonts5.ht

    • 问题内容: 我有一个自定义字体,我想在Java程序中炫耀它,用户无需安装就可以查看它。有谁知道这是怎么做到的吗? 工作解决方案 我已经实现了以下内容: 问题答案: 我从未做过,但似乎您想要的方法是 导入字体 或者 该参数是或,而或参数包含字体的二进制数据。 导入后使用字体: 要使Font对Font构造函数可用,必须通过调用在中注册返回的Font 。

    • 我的系统: Qt Embedded(开源版)4.8 用于图形的Linux帧缓冲区 字体:(拉丁语、日语、希腊语)。(/usr/lib/fonts) 我正在尝试将拉丁字母与其他类型的字符(例如日语)混合使用。我有拉丁字母和日语字符的字体,但它们不是同一种字体。我研究了帖子: Qt同时使用多个字体以及如何在Qt-embedded中正确输出多语言文本 我按照步骤操作,但无法正确显示所有字符 有没有办法表

    • 所以我有下面的问题。我收到一个文件,其中包含一组字体。这些字体没有嵌入到文件中。下面是一个简单的例子: 我希望将这些字体嵌入到中,这样它们就可以自包含并且始终可用。但事情似乎没那么简单。我正在使用进行处理。 我已经阅读并尝试了以下问题/答案: null

    • 我正在尝试将字体嵌入到我的pdf/a中。 我找到了很多答案,但使用的是iTextSharp 在我的cas中,我使用iText7,但我所尝试的都是错误的: “所有字体都必须嵌入……” 我有一个ttf文件为我的字体,但我没有找到一种方法嵌入到我的pdf使用它... 有人能帮我吗? 事先谢谢 kor6k

    • 问题内容: 我有一个使用Foxit表单设计器创建的pdf文件。在我的设计系统上,我安装了条形码字体。条形码字体在AcroField之一中使用。foxit似乎没有在文档中嵌入字体。 我也有一些客户的计算机上未安装条形码字体,因此我想将该字体嵌入到PDF中。我是否可以使用iText将AcroFields中使用的字体嵌入到PDF中? 编辑: 字体似乎包含在文本中,但没有包含在字段中,因此字体(条形码)将