微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

xml – 如何在fop生成的pdf中显示预先格式化的文本?

有谁知道如何显示预格式化的html文本,如:

<ol>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ol>

在用fop生成的pdf里面?

谢谢

解决方法

这个XSLT 1.0:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <fo:layout-master-set>
        <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
          <fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="my-page">
        <fo:flow flow-name="xsl-region-body">
          <xsl:apply-templates/>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>

  <xsl:template match="ol">
    <fo:list-block provisional-distance-between-starts="24pt" space-before=".1in" space-after=".1in">
      <xsl:apply-templates/>
    </fo:list-block>
  </xsl:template>

  <xsl:template match="li">
    <fo:list-item>
      <fo:list-item-label end-indent="label-end()">
        <fo:block><xsl:number format="1."/></fo:block>
      </fo:list-item-label>
      <fo:list-item-body start-indent="body-start()">
        <fo:block>
          <xsl:apply-templates/>
        </fo:block>
      </fo:list-item-body>
    </fo:list-item>
  </xsl:template>

</xsl:stylesheet>

应用于您的输入将生成以下XSL-FO:

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:layout-master-set>
      <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
         <fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
      </fo:simple-page-master>
   </fo:layout-master-set>
   <fo:page-sequence master-reference="my-page">
      <fo:flow flow-name="xsl-region-body">
         <fo:list-block provisional-distance-between-starts="24pt" space-before=".1in" space-after=".1in">
            <fo:list-item>
               <fo:list-item-label end-indent="label-end()">
                  <fo:block>1.</fo:block>
               </fo:list-item-label>
               <fo:list-item-body start-indent="body-start()">
                  <fo:block>item 1</fo:block>
               </fo:list-item-body>
            </fo:list-item>
            <fo:list-item>
               <fo:list-item-label end-indent="label-end()">
                  <fo:block>2.</fo:block>
               </fo:list-item-label>
               <fo:list-item-body start-indent="body-start()">
                  <fo:block>item 2</fo:block>
               </fo:list-item-body>
            </fo:list-item>
            <fo:list-item>
               <fo:list-item-label end-indent="label-end()">
                  <fo:block>3.</fo:block>
               </fo:list-item-label>
               <fo:list-item-body start-indent="body-start()">
                  <fo:block>item 3</fo:block>
               </fo:list-item-body>
            </fo:list-item>
         </fo:list-block>
      </fo:flow>
   </fo:page-sequence>
</fo:root>

这将生成包含以下内容的PDF(使用FOP 1.0测试):

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。