2008年3月27日星期四

中文docbook转换成PDF终于实现

终于搞定了~~~
  • 首先安装fop
# autounmask dev-java/fop-0.94-r1
据说fop对中文的断行支持得比0.93好。另外"zh_cn"也可以正确处理了。
# emerge fop -av
  • 然后生成字体描述文件
# java -cp /usr/share/fop/lib/fop.jar:/usr/share/avalon-framework-4.2/lib/avalon-framework.jar:/usr/share/commons-logging/lib/commons-logging.jar:/usr/share/commons-io-1/lib/commons-io.jar org.apache.fop.fonts.apps.TTFReader /usr/share/fonts/fireflysung/fireflysung.ttf fireflysung.xml
TTF Reader for Apache FOP 0.94

Parsing font...
Reading /usr/share/fonts/fireflysung/fireflysung.ttf...
Font Family: AR PL New Sung
Creating xml font file...
Creating CID encoded metrics...
Writing xml font file fireflysung.xml...
This font contains no embedding license restrictions.

XML font metrics file successfully created.

# java -cp /usr/share/fop/lib/fop.jar:/usr/share/avalon-framework
-4.2/lib/avalon-framework.jar:/usr/share/commons-logging/lib/commons-logging.jar:/usr/share/commons-io-1/lib/commons-io.jar org.apache.fop.fonts.apps.TTFReader -ttcname "SimSun" /usr/share/fonts/winfonts/simsun.ttf simsun.xml
TTF Reader for Apache FOP 0.94

Parsing font...
Reading /usr/share/fonts/winfonts/simsun.ttf...
This is a TrueType collection file with 2 fonts
Containing the following fonts:
SimSun <-- selected
NSimSun
Font Family: SimSun
Creating xml font file...
Creating CID encoded metrics...
Writing xml font file simsun.xml...
This font contains no embedding license restrictions.

XML font metrics file successfully created.
# mkdir /etc/fop
# mv *.xml /etc/fop
  • 建立FOP配置文件
# cp /usr/share/doc/fop-0.94-r1/examples/conf/fop.xconf /etc/fop
# cd /etc/fop

建立zhfop.conf的内容如下:

<?xml version="1.0"?>
<fop version="1.0">
<base>.</base>
<renderers>
<renderer mime="application/pdf">
<filterList>
<value>flate</value>
</filterList>
<fonts>
<font metrics-url="/etc/fop/simsun.xml" kerning="yes"
embed-url="/usr/share/fonts/winfonts/simsun.ttf">
<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>
  • 创建xsl文件
# cat /usr/share/sgml/docbook/xsl-stylesheets/fo/docbookzhcn.xsl
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:doc="http://nwalsh.com/xsl/documentation/1.0"
exclude-result-prefixes="doc"
version="1.0">
<xsl:import href="docbook.xsl"/>
<xsl:param name="body.font.family">SimSun</xsl:param>
<xsl:param name="body.font.size">12</xsl:param>
<xsl:param name="monospace.font.family">SimSun</xsl:param>
<xsl:param name="title.font.family">SimSun</xsl:param>
<xsl:param name="page.margin.inner">2cm</xsl:param>
<xsl:param name="page.margin.outer">2cm</xsl:param>
<xsl:param name="hyphenate">false</xsl:param>
<xsl:param name="paper.type" select="'A4'"/>
<xsl:param name="draft.mode" select="'no'"/>
</xsl:stylesheet>

  • 用xsltproc来生成HTML或者XSL-FO文件

譬如,下面是根据Docbook文档生成HTML的例子:
$ xsltproc --output myfile.html /usr/share/sgml/docbook/xsl-stylesheets/html/docbook.xsl myfile.xml

或者根据docbook文档生成XSL-FO文档的例子:
$ xsltproc --output myfile.fo /usr/share/sgml/docbook/xsl-stylesheets/fo/docbook.xsl myfile.xml

  • 用fop生成PDF文档
$ fop -c /etc/fop/zhfop.conf -xsl /usr/share/sgml/docbook/xsl-stylesheets/fo/docbookzhcn.xsl -xml myfile.xml -pdf myfile.pdf

没有评论: