如何在 fop 生成的 pdf 中显示预先格式化的文本? [英] How display preformatted text in a pdf generated by fop?

查看:18
本文介绍了如何在 fop 生成的 pdf 中显示预先格式化的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

    <li>项目 1</li><li>项目 2</li><li>项目 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/格式"><xsl:output indent="yes"/><xsl:strip-space elements="*"/><xsl:template match="node()|@*"><xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy></xsl:模板><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:模板><xsl:template match="ol"><fo:list-block promptal-distance-between-starts="24pt" space-before=".1in" space-after=".1in"><xsl:apply-templates/></fo:list-block></xsl:模板><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:模板></xsl:stylesheet>

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

<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 promptal-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>项目 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>项目 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>项目 3</fo:block></fo:list-item-body></fo:list-item></fo:list-block></fo:flow></fo:page-sequence></fo:root>

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

Does anyone know how I can display preformatted html text like:

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

inside a pdf generated with fop?

Thanks

解决方案

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

applied to your input will produce the following 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>

which will produce a PDF that contains the following (tested with FOP 1.0):

这篇关于如何在 fop 生成的 pdf 中显示预先格式化的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆