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

[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]


<?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>


[color=darkred]注意:file:/// 路径要写对[/color]


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

<?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>


[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"/>
<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打开就可以看到"测试中文显示!"文字了。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值