[color=green]windows操作系统的配置[/color]
下载fop-0.95压缩包,以支持windows的Simhei和Simsun为例,列出将fo文件转为pdf的整个过程
步骤1: [color=red]生成字体相关的xml文件simhei.xml和simsun.xml[/color]
[color=black]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[/color]
[color=darkred]注意:下载的fop压缩包不同,lib目录下的各jar名也不同,请大家改为自己的jar名[/color]
[color=black]C:\WINDOWS\Fonts\simhei.ttf D:\fop\conf\simhei.xml[/color]
[color=black]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
[/color]
[color=darkred]步骤1操作完成,检查一下你的simhei.xml和simsun.xml有没有生成成功[/color]
[color=red]步骤2:修改用户定义文件,在解压FOP包的目录下conf创建config.xml,文件内容修改为:[/color]
[color=darkred]注意:file:/// 路径要写对[/color]
[color=red]步骤3:修改相应的xsl文件,应用上Simsun字体。在相应的节点上添加字体属性font-family="Simsun" ,例如创建文件simplecn.fo,文件内容修改为:[/color]
[color=red]步骤4:将XML转为PDF,执行如下:[/color]
[color=black]fop -c conf/config.xml -fo test/simplecn.fo -pdf pdf/simplecn.pdf[/color]
操作结束!
[color=green]ubuntu下的中文配置:[/color]
环境: Ubuntu desktop 9.04
[color=black]sudo apt-get install docbook-xml docbook-xsl xsltproc fop[/color]
下载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" target="_blank" rel="external nofollow" />
<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打开就可以看到"测试中文显示!"文字了。