【问题标题】:How display preformatted text in a pdf generated by fop?如何在 fop 生成的 pdf 中显示预格式化文本?
【发布时间】:2012-02-03 08:47:48
【问题描述】:

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

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

在使用 fop 生成的 pdf 中?

谢谢

【问题讨论】:

  • 您的意思是要将&lt;ol&gt; 代码显示为纯文本还是您实际上想要一个包含 3 个列表项的无序列表?
  • 我实际上想要一个包含 3 个列表项的无序列表

标签: xml xslt xsl-fo apache-fop


【解决方案1】:

这个 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 测试):

【讨论】:

  • 嘿。你能发布任何博客,我们可以详细了解所使用的标签。
  • @DhirajGandhi - 看看免费书籍Practical Formatting Using XSL-FO(如果有帮助,请考虑捐赠;我与 Crane Softwrights 没有任何联系)。也看看the spec
【解决方案2】:

Use the attributes linefeed-treatment="preserve" and white-space-collapse="false"

要么转义 HTML:

<block linefeed-treatment="preserve" white-space-collapse="false">
     &lt;ol>
      &lt;li>item 1&lt;/li>
      &lt;li>item 2&lt;/li>
      &lt;li>item 3&lt;/li>
    &lt;/ol>
</block>

或者使用 CDATA:

<block linefeed-treatment="preserve" white-space-collapse="false">  
   <![CDATA[
    <ol>
      <li>item 1</li>
      <li>item 2</li>
      <li>item 3</li>
    </ol>
    ]]>
</block>

【讨论】:

  • 仍然有这些属性,我无法转义 HTML 标签。它在 PDF 上显示
      ....
    。请帮忙
猜你喜欢
  • 2018-07-01
  • 1970-01-01
  • 2018-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多