`
wangxc
  • 浏览: 209605 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

fop中文配置 (windows和Ubuntu)操作系统

    博客分类:
  • fop
阅读更多
windows操作系统的配置

下载fop-0.95压缩包,以支持windows的Simhei和Simsun为例,列出将fo文件转为pdf的整个过程

步骤1: 生成字体相关的xml文件simhei.xml和simsun.xml

java -cp java -cp build\fop.jar;  lib\avalon-framework-4.2.0.jar;lib\batik-all-1.7.jar;lib \commons-logging-1.0.4.jar;lib\commons-io-1.3.1.jar;lib\serializer-2.7.0.jar;lib\xalan-2.7.0.jar;lib\xercesImpl-2.7.1.jar;lib\xml-apis-1.3.04.jar;lib\xml-apis-ext-1.3.04.jar;lib\xmlgraphics-commons-1.3.1.jar org.apache.fop.fonts.apps.TTFReader
注意:下载的fop压缩包不同,lib目录下的各jar名也不同,请大家改为自己的jar名
C:\WINDOWS\Fonts\simhei.ttf D:\fop\conf\simhei.xml
java -cp build\fop.jar;  lib\avalon-framework-4.2.0.jar;lib\batik-all-1.7.jar;lib \commons-logging-1.0.4.jar;lib\commons-io-1.3.1.jar;lib\serializer-2.7.0.jar;lib\xalan-2.7.0.jar;lib\xercesImpl-2.7.1.jar;lib\xml-apis-1.3.04.jar;lib\xml-apis-ext-1.3.04.jar;lib\xmlgraphics-commons-1.3.1.jar
org.apache.fop.fonts.apps.TTFReader -ttcname "SimSun" C:\WINDOWS\Fonts\simsun.ttc D:\fop\conf\simsun.xml


步骤1操作完成,检查一下你的simhei.xml和simsun.xml有没有生成成功

步骤2:修改用户定义文件,在解压FOP包的目录下conf创建config.xml,文件内容修改为:

<?xml version="1.0"?> 
<fop version="1.0"> 
<base>.</base> 
   <renderers> 
    <renderer mime="application/pdf"> 
      <filterList> 
         <value>flate</value> 
     </filterList> 
      <fonts>   
        <font metrics-url="file:///D:\fop\conf\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> 
          <font metrics-url="file:///D:\fop\conf\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> 
        </fonts> 
    </renderer> 
</renderers> 
</fop>   


注意:file:/// 路径要写对


步骤3:修改相应的xsl文件,应用上Simsun字体。在相应的节点上添加字体属性font-family="Simsun" ,例如创建文件simplecn.fo,文件内容修改为:

<?xml version="1.0" encoding="utf-8"?> 
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
  <fo:layout-master-set> 
    <fo:simple-page-master master-name="simple" 
        page-height="29.7cm" 
        page-width="21cm" 
        margin-top="1cm" 
        margin-bottom="2cm" 
        margin-left="2.5cm" 
        margin-right="2.5cm"> 
      <fo:region-body margin-top="3cm"/> 
      <fo:region-before extent="3cm"/> 
      <fo:region-after extent="1.5cm"/> 
    </fo:simple-page-master> 
  </fo:layout-master-set> 
  <fo:page-sequence master-reference="simple"> 
    <fo:flow flow-name="xsl-region-body"> 
      <fo:block font-size="18pt" 
          font-family="simsun" 
          line-height="24pt" 
          text-align="center" 
          padding-top="3pt"> 
        这是宋体 
      </fo:block> 
      <fo:block font-size="18pt" 
          font-family="simhei" 
          line-height="24pt" 
          text-align="center" 
          padding-top="3pt"> 
        这是黑体 
      </fo:block> 
    </fo:flow> 
  </fo:page-sequence> 
</fo:root> 


步骤4:将XML转为PDF,执行如下:
fop -c conf/config.xml -fo test/simplecn.fo -pdf pdf/simplecn.pdf

操作结束!


ubuntu下的中文配置:
环境: Ubuntu desktop 9.04
sudo apt-get install docbook-xml docbook-xsl xsltproc fop

下载simsun.ttc和simhei.ttf
sudo apt-get install docbook-xml docbook-xsl xsltproc fop

sudo mkdir -p /etc/fop/font
sudo cp simsun.ttc /etc/fop/font
sudo cp simhei.ttf /etc/fop/font

生成simsun.xml和simhei.xml文件
sudo fop-ttfreader /etc/fop/font/simsun.ttc /etc/fop/simsun.xml -ttcname SimSun
sudo fop-ttfreader /etc/fop/font/simhei.ttf /etc/fop/simhei.xml

分别为simsun和simhei字体生成Metrics文件
sudo fop-ttfreader -ttcname SimSun /etc/fop/font/simsun.ttc /etc/fop/simsun.xml
sudo fop-ttfreader -ttcname SimHei /etc/fop/font/simhei.ttf /etc/fop/simhei.xml

从FOP包里取出fop.xconf文件并cp到 /etc/fop下,修改这个文件,在<fonts></fonts>间加入如下内容:
<fonts>
<font metrics-url="file:///etc/fop/simsun.xml" kerning="yes" embed-url="file:///etc/fop/font/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="file:///etc/fop/simhei.xml" kerning="yes" embed-url="file:///etc/fop/font/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">/usr/share/fonts/</directory>
<auto-detect/>
</fonts>

在/etc/fop下创建一个样式表fop.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="/usr/share/xml/docbook/stylesheet/nwalsh/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>


输出pdf文件:
test.xml还是拿docbook的那个文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://docbook.org/xml/4.2/docbookx.dtd">
<article>
<title>My first Docbook document</title>
<sect1>
<title>The greeting</title>
<para>
测试中文显示!
</para>
</sect1>
</article>

sudo xsltproc -o test.fo fop.xsl test.xml
sudo fop -c /etc/fop/fop.xconf test.fo -pdf test.pdf

查看你的fop目录下有一个test.pdf打开就可以看到"测试中文显示!"文字了。


  
分享到:
评论
3 楼 newlikeda 2011-10-14  
C:\WINDOWS\Fonts\         
2 楼 f002489 2011-05-30  
<renderer mime="application/x-afp"> 这个节点,ubuntu配置过没?
也是中文问题,pdf汉字显示为#::

:49:20 org.apache.fop.events.LoggingEventListener processEvent
警告: Glyph "测" (0x6d4b) not available in font "Times-Roman".
1 楼 f002489 2011-05-25  
thanks, share...

相关推荐

Global site tag (gtag.js) - Google Analytics