和荣笔记 - XSL-FO 与 XHTML
杨和荣, 版本 2.03, 2007年

中文网页和排版

Part:   1  2  3  4  5 

(Continued from previous part...)

 <xsl:template match="p[@class='section_title']">
   <fo:block font-size="125%" font-weight="bold"
    space-before="9pt" space-after="4pt">
  <xsl:apply-templates select="text()"/>
   </fo:block>
 </xsl:template>

 <xsl:template match="a">
   <fo:basic-link external-destination="url('{@href}')" color="#0000ff">
  <xsl:apply-templates select="text()"/>
   </fo:basic-link>
 </xsl:template>

 <xsl:template match="b">
   <fo:wrapper font-weight="bold">
  <xsl:apply-templates select="text()"/>
   </fo:wrapper>
 </xsl:template>

 <xsl:template match="i">
   <fo:wrapper font-style="italic">
  <xsl:apply-templates select="text()"/>
   </fo:wrapper>
 </xsl:template>

 <xsl:template match="p[@class='toc_item']">
   <fo:block space-before="8pt" space-after="4pt"
    text-align-last="justify">
  <xsl:apply-templates select="a|text()"/>
   </fo:block>
 </xsl:template>

 <xsl:template match="p">
   <fo:block space-before="8pt" space-after="4pt">
  <xsl:apply-templates select="a|b|i|text()"/>
   </fo:block>
 </xsl:template>

 <xsl:template match="text()">
  <xsl:value-of select="."/>
 </xsl:template>

 <xsl:template match="pre[@class='block_source']">
    <fo:block font-family="SimHei" font-size="10pt" 
     background-color="#cfcfcf"
     white-space-collapse="false" wrap-option="no-wrap">
  <xsl:value-of select="./text()"/>
    </fo:block>
 </xsl:template>

 <xsl:template match="pre[@class='block_bold']">
    <fo:block font-family="SimHei" font-size="10pt" 
     background-color="#cfcfcf" font-weight="bold"
     white-space-collapse="false" wrap-option="no-wrap">
  <xsl:value-of select="./text()"/>
    </fo:block>
 </xsl:template>

 <xsl:template match="pre[@class='block_syntax']">
    <fo:block font-family="SimHei" font-size="10pt" 
     background-color="#cfcfcf" font-style="italic"
     white-space-collapse="false" wrap-option="no-wrap">
  <xsl:value-of select="./text()"/>
    </fo:block>
 </xsl:template>

 <xsl:template match="//p/ul">
    <fo:list-block space-before="8pt" space-after="4pt">
  <xsl:apply-templates select="li"/>
    </fo:list-block>
 </xsl:template>

 <xsl:template match="ul">
    <fo:list-block space-before="1pt" space-after="1pt">
  <xsl:apply-templates select="li"/>
    </fo:list-block>
 </xsl:template>

 <xsl:template match="li">
    <fo:list-item>
     <fo:list-item-label start-indent="0.1in">
     <fo:block>•</fo:block>
     </fo:list-item-label>
     <fo:list-item-body start-indent="0.25in">
     <fo:block space-after="3pt">
  <xsl:apply-templates select="a|b|i|ul|text()"/>
     </fo:block>
     </fo:list-item-body>
    </fo:list-item>
 </xsl:template>
</xsl:stylesheet>

有几点值得注意的地方:

  • “//p”用来越过了多层网页表格结构。
  • 只有三种 HTML 指令://p|//ul|//pre 被转换成了段落。
  • 页面边缘空白部份的尺寸可以随意调节,但是要注意 //pre 段落。
  • 如果需要,你可以添加转换语句来处理其它 HTML 指令。
  • 在“output”指令中必须加上 encoding="gb2312"。

制作中文 PDF 文件

为了处理中文,我对 FOP 的运行环境作了入下调整:

一。下载和安装微软的中文简体字字库,成功后有一字库文件: \winnt\fonts\simhei.ttf

二。为 FOP 制作字库说明文件:simhei.xml,使用命令为: java org.apache.fop.fonts.apps.TTFReader \winnt\fonts\simhei.ttf simhei.xml

三。将新字库引入 FOP 的运行环境,方法为修改 conf\userconfig.xml:

<fonts>
 <font metrics-file="simhei.xml" kerning="yes" 
    embed-file="\winnt\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>
</fonts>

制作中文 PDF 文件过程:

  • 把 *.xhtml 和 book.css 复制到网站的文件系统上,网页制作完成。
  • 在 FOP 的环境内,用“xalan -in *.xhtml -xsl book_fo.xsl -out *.fo” 对每一章作转换处理。
  • 打开 toc.fo,把其它章节的 *.fo 文件内容串联在后面,存档为 toc_expanded.fo。
  • 由于 FOP 无法对中文自动分行,我不得不在每个中文字后面加上一个空格来帮助它分行。
  • 运行“run_fop toc_expanded.fo toc_expanded.pdf”, PDF 排版完成。
  • 当然,制作的一些细节工作我已经用 perl 程式自动化了。

Part:   1  2  3  4  5 

杨和荣,修改于2007年
和荣笔记 - XSL-FO 与 XHTML - 中文网页和排版