在Windows上安装配置Apache FOP

使用Apache FOP将Docbook文档转换成PDF一文中,Easwy介绍了在Debian Linux上使用Apache FOP将Docbook文档转换成PDF文件。本来在那篇文章之后,Easwy想写一篇如何在Windows上配置Apache FOP的文章,不过一直没抽出时间。今天刚好有网友问这个问题,于是教她一步步做下来,把步骤列在下面。

你可以在这里下载到文章中Easwy用到的所有文件:点此下载

  1. 安装Docbook样式表和xsltproc程序

    按照文章一个简单的Docbook 5.0例子,安装Docbook样式表和xsltproc转换程序,并能够成功将Docbook文件转换成html格式。

  2. 安装Apache FOP和Java虚拟机

    Apache Download Mirrors下载Apache FOP,Easwy下载的是FOP 1.0。下载后,将下载的文件解压,Easwy把它解压到C:\docbook\fop-1.0。

    接下来,还要安装Java虚拟机,因为Apache FOP需要Java虚拟机才能运行。到Java网站下载安装即可。

  3. 设置Apache FOP

    我们需要配置Apache FOP进行PDF转换时所用到的字体信息。首先,用下面的命令把字体信息从Windows自带的系统字体中提取出来:

    cd C:\docbook\fop-1.0
    mkdir fonts
    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 -ttcname SimSun c:\windows\fonts\simsun.ttc fonts\simsun.xml
    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 -ttcname SimHei c:\windows\fonts\simhei.ttf fonts\simhei.xml 

    上面的命令,把C:\Windows\fonts\目录下的宋体和黑体这两种字体信息提取到fonts\simsun.xmlfonts\simhei.xml文件中。

    接下来配置C:\docbook\fop-1.0\conf\fop.xconf,使Apache FOP能够找到这两种字体。首先在文件中找到<renderer mime=”application/pdf”>这一行,然后在此标签中加入关于字体的配置。

            
    <fonts>
      <font metrics-url="fonts/simsun.xml" kerning="yes" embed-url="file:///c:/windows/fonts/simsun.ttc">
        <font-triplet name="SimSun" style="normal" weight="normal"/>
        <font-triplet name="SimSun" style="normal" weight="bold"/>
        <font-triplet name="SimSun" style="italic" weight="normal"/>
        <font-triplet name="SimSun" style="italic" weight="bold"/>
      </font>
    
      <font metrics-url="fonts/simhei.xml" kerning="yes" embed-url="file:///c:/windows/fonts/simhei.ttf">
        <font-triplet name="SimHei" style="normal" weight="normal"/>
        <font-triplet name="SimHei" style="normal" weight="bold"/>
        <font-triplet name="SimHei" style="italic" weight="normal"/>
        <font-triplet name="SimHei" style="italic" weight="bold"/>
      </font>
    
      <directory recursive="true">file:///c:/windows/fonts/</directory>
    
      <auto-detect/>
    </fonts>
             
  4. 配置XLT转换样式表

    接下来,我们要配置XSL转换样式表,让xsltproc在转换Docbook文档时,使用我们指定的中文字体。在下面我给出了一个简单的样式表,假设存为docbook_fo.xsl:

     
    <?xml version='1.0'?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:exsl="http://exslt.org/common"
      xmlns:fo="http://www.w3.org/1999/XSL/Format"
      xmlns:ng="http://docbook.org/docbook-ng"
      xmlns:db="http://docbook.org/ns/docbook"
      exclude-result-prefixes="db ng exsl"
      version='1.0'>
    
      <xsl:import href="C:/docbook/docbook-xsl-1.74.3/fo/docbook.xsl"/>
      <xsl:param name="body.font.family">SimSun</xsl:param>
    
      <xsl:param name="monospace.font.family">SimSun</xsl:param>
      <xsl:param name="title.font.family">SimHei</xsl:param>
    </xsl:stylesheet>
             

    在这个转换样式表里,首先导入了Docbook的FO样式表,然后重新设置了其中的字体参数,改成我们所要的字体名称。需要注意的是,这里的字体名字需要和fop.xconf中设置的font-triplet中的名字完全一致。

  5. 输出PDF文件

    完成上述配置后,就可以进行PDF文件的转换了。把下面的文件保存为example.xml

            
    <?xml version='1.0' encoding="utf-8"?>
    
    <article xmlns="http://docbook.org/ns/docbook" version="5.0" xml:lang="zh-CN"
      xmlns:xlink='http://www.w3.org/1999/xlink'>
      <articleinfo>
        <title>我的第一篇Docbook 5.0文档</title>
        <author>
          <firstname>Easwy</firstname>
    
          <surname>Yang</surname>
        </author>
      </articleinfo> 
    
      <section>
        <title>文档介绍</title>
    
        <para>
          这是我的第一篇Docbook 5.0文档,欢迎你来到<link xlink:href='http://easwy.com/blog/'>Easwy的博客</link>。
        </para>
      </section>
    </article> 
             

    然后输入下面的命令将其转换成PDF:

    xsltproc -o example.fo c:\docbook\fop-1.0\conf\docbook_fo.xsl example.xml
    fop -c c:\docbook\fop-1.0\conf\fop.xconf example.fo -pdf example.pdf 

更多内容,请阅读易水博客上的其它文章。

参考文档

原创文章,请阅读页脚的许可方式,转载请注明:转载自易水博客 [ http://easwy.com/blog/ ]

本文链接地址: http://easwy.com/blog/archives/install-apache-fop-on-windows/

文章的脚注信息由WordPress的wp-posturl插件自动生成

《在Windows上安装配置Apache FOP》有15个想法

  1. 谢谢你详细的介绍。非常受益。让不太懂程序的我也一步步走下来。
    但在最后一个关头遇到了障碍。在命令行执行 fop 命令时,提示 upported major.minor version 48.0,据说就是版本不兼容的问题。我的jdk版本是jdk1.6.0_21。请问是否有适合这个版本的fop?我该怎么解决这个问题?多谢!!!!

  2. 感谢教程的分享!
    请教个问题:PDF中的书签功能(阅读器左边按章节导航的部分)是在docbook设置控制的吗?还是生成PDF后用其它工具完成的?谢谢

  3. fop -c c:\docbook\fop-1.0\conf\fop.xconf example.fo -pdf example.pdf

    这一步我的出错,html生成没问题
    java版本 :
    java version “1.6.0_27”
    Java(TM) SE Runtime Environment (build 1.6.0_27-b07)
    Java HotSpot(TM) Client VM (build 20.2-b06, mixed mode, sharing)

    错误:

    2011-10-18 17:55:14 org.apache.fop.cli.Main startFOP
    严重: Exception
    org.apache.fop.apps.FOPException: Failed to resolve font with metric-url ‘fonts/simsun.xml’
    at org.apache.fop.util.LogUtil.handleError(LogUtil.java:38)
    at org.apache.fop.fonts.FontInfoConfigurator.getFontInfo(FontInfoConfigurator.java:221)
    at org.apache.fop.fonts.FontInfoConfigurator.addFonts(FontInfoConfigurator.java:168)
    at org.apache.fop.fonts.FontInfoConfigurator.configure(FontInfoConfigurator.java:100)
    at org.apache.fop.render.PrintRendererConfigurator.buildFontList(PrintRendererConfigurator.java:106)
    at org.apache.fop.render.PrintRendererConfigurator.setupFontInfo(PrintRendererConfigurator.java:129)
    at org.apache.fop.render.intermediate.IFUtil.setupFonts(IFUtil.java:165)
    at org.apache.fop.render.intermediate.IFRenderer.setupFontInfo(IFRenderer.java:181)
    at org.apache.fop.area.RenderPagesModel.(RenderPagesModel.java:73)
    at org.apache.fop.area.AreaTreeHandler.setupModel(AreaTreeHandler.java:130)
    at org.apache.fop.area.AreaTreeHandler.(AreaTreeHandler.java:102)
    at org.apache.fop.render.RendererFactory.createFOEventHandler(RendererFactory.java:359)
    at org.apache.fop.fo.FOTreeBuilder.(FOTreeBuilder.java:105)
    at org.apache.fop.apps.Fop.createDefaultHandler(Fop.java:101)
    at org.apache.fop.apps.Fop.(Fop.java:79)
    at org.apache.fop.apps.FopFactory.newFop(FopFactory.java:271)
    at org.apache.fop.cli.InputHandler.renderTo(InputHandler.java:109)
    at org.apache.fop.cli.Main.startFOP(Main.java:174)
    at org.apache.fop.cli.Main.main(Main.java:205)

    这跟java版本有关系吗 ,我生成的simsun.xml 都跟从你这down下来的一样 fop.xconf配置文件内容也是一样

    1. 你出的错是:Failed to resolve font with metric-url ‘fonts/simsun.xml’

      你的simsun.xml放在哪个目录下呢?如果你完全按照我的文章来做,simsun.xml应该在C:\docbook\fop-1.0\fonts目录下

  4. 您好,我也用FOP1.0,也同样的按照你的方法配置的字体文件,但是输出的依然是井号“#”,我是作为application来用的,其中输出信息中有:
    2011-11-24 13:17:31 org.apache.fop.events.LoggingEventListener processEvent
    警告: Glyph “中” (0x4e2d) not available in font “Helvetica”.
    2011-11-24 13:17:31 org.apache.fop.events.LoggingEventListener processEvent
    警告: Glyph “文” (0x6587) not available in font “Helvetica”.
    “中”和“问”是xml文件中的内容,真的有点头大,请问是否要好一点的解决方法!?

  5. 你好,我用从你的博客中下载的文件生成的pdf,依然是一堆井号“#”,输出的命令行中的信息如下:
    F:\fop>fop -c F:\fop\conf\fop.xconf F:\fop\hello\example.fo -pdf example.pdf
    2011-11-24 15:33:45 org.apache.fop.apps.FopFactoryConfigurator configure
    信息: Default page-height set to: 11in
    2011-11-24 15:33:45 org.apache.fop.apps.FopFactoryConfigurator configure
    信息: Default page-width set to: 8.26in
    2011-11-24 15:33:46 org.apache.fop.events.LoggingEventListener processEvent
    警告: Font “Symbol,normal,700” not found. Substituting with “Symbol,normal,40

    2011-11-24 15:33:46 org.apache.fop.events.LoggingEventListener processEvent
    警告: Font “ZapfDingbats,normal,700” not found. Substituting with “ZapfDingba
    normal,400”.
    2011-11-24 15:33:46 org.apache.fop.events.LoggingEventListener processEvent
    警告: Glyph “我” (0x6211) not available in font “Symbol”.
    2011-11-24 15:33:46 org.apache.fop.events.LoggingEventListener processEvent
    警告: Glyph “的” (0x7684) not available in font “Symbol”.
    2011-11-24 15:33:46 org.apache.fop.events.LoggingEventListener processEvent
    警告: Glyph “第” (0x7b2c) not available in font “Symbol”.
    2011-11-24 15:33:46 org.apache.fop.events.LoggingEventListener processEvent
    警告: Glyph “一” (0x4e00) not available in font “Symbol”.
    2011-11-24 15:33:46 org.apache.fop.events.LoggingEventListener processEvent
    警告: Glyph “篇” (0x7bc7) not available in font “Symbol”.
    2011-11-24 15:33:46 org.apache.fop.events.LoggingEventListener processEvent
    警告: Glyph “D” (0x44, D) not available in font “Symbol”.
    2011-11-24 15:33:46 org.apache.fop.events.LoggingEventListener processEvent
    警告: Glyph “o” (0x6f, o) not available in font “Symbol”.
    2011-11-24 15:33:46 org.apache.fop.events.LoggingEventListener processEvent
    警告: Glyph “c” (0x63, c) not available in font “Symbol”.
    2011-11-24 15:33:46 org.apache.fop.hyphenation.Hyphenator getHyphenationTree
    严重: Couldn’t find hyphenation pattern zh_cn
    2011-11-24 15:33:46 org.apache.fop.events.LoggingEventListener processEvent
    警告: Font “SimHei,normal,700” not found. Substituting with “any,normal,700”.
    2011-11-24 15:33:46 org.apache.fop.events.LoggingEventListener processEvent
    警告: Glyph “文” (0x6587) not available in font “Times-Bold”.
    2011-11-24 15:33:46 org.apache.fop.events.LoggingEventListener processEvent
    警告: Glyph “档” (0x6863) not available in font “Times-Bold”.
    2011-11-24 15:33:46 org.apache.fop.events.LoggingEventListener processEvent
    警告: Glyph “介” (0x4ecb) not available in font “Times-Bold”.
    2011-11-24 15:33:46 org.apache.fop.events.LoggingEventListener processEvent
    警告: Glyph “绍” (0x7ecd) not available in font “Times-Bold”.

    F:\fop>

  6. 能把你生成的simhei.xml和simhei.ttf发我一份吗,万分感激,我搞不定
    ,邮箱:kms1989@163.com,谢了!

发表评论

电子邮件地址不会被公开。 必填项已用*标注