You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用XSLT生成PDF时跨页表格无页脚及续表标识需求

解决XSLT生成PDF时表格跨页的页脚显示与“cont”标识问题

我来帮你搞定XSLT生成PDF时表格跨页的这两个问题——页脚不显示和添加“cont”续标识!下面是具体的解决方案和代码示例:

1. 先解决表格页脚跨页不显示的问题

默认情况下,XSL-FO的fo:table-footer应该在表格的每一页底部重复显示,但如果你的表格设置了page-break-inside: avoid或者table-layout: fixed导致分页异常,可能会让页脚只在最后一页显示。你可以通过以下方式确保页脚在每一页都正常显示:

  • 不要给fo:table-body设置page-break-inside: avoid(除非你想完全避免表格内部分页)
  • 显式声明table-omit-footer-at-break="false"(默认值就是false,但显式写出来可以避免被其他设置意外覆盖)
  • 调整页面边距和区域大小,保证页脚内容不会超出页面范围

2. 添加跨页第一页的“cont”续标识

要在表格跨页时的第一页末尾显示“cont”,我们可以利用XSL-FO的**标记(marker)和检索标记(retrieve-marker)**机制,结合页面位置判断来实现。

示例代码

Sample XML

<data>
  <table>
    <row><col>Row 1</col><col>Sample Content for Row 1</col></row>
    <row><col>Row 2</col><col>Sample Content for Row 2</col></row>
    <!-- 可添加更多行让表格内容超过一页 -->
    <row><col>Row 50</col><col>Sample Content for Row 50</col></row>
  </table>
</data>

修改后的Sample XSL(XSL-FO)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

  <xsl:template match="/">
    <fo:root>
      <!-- 定义页面模板 -->
      <fo:layout-master-set>
        <!-- 通用页面模板 -->
        <fo:simple-page-master master-name="default-page" 
                               page-height="29.7cm" page-width="21cm"
                               margin-top="2cm" margin-bottom="2cm"
                               margin-left="2cm" margin-right="2cm">
          <fo:region-body margin-bottom="1.5cm"/> <!-- 给页脚预留空间 -->
          <fo:region-after extent="1cm"/> <!-- 页脚显示区域 -->
        </fo:simple-page-master>
        <!-- 表格分页的页面序列模板 -->
        <fo:repeatable-page-master-alternatives master-name="table-pages">
          <fo:conditional-page-master-reference master-reference="default-page" page-position="any"/>
        </fo:repeatable-page-master-alternatives>
      </fo:layout-master-set>

      <!-- 表格的页面序列 -->
      <fo:page-sequence master-reference="table-pages">
        <!-- 静态页脚内容 -->
        <fo:static-content flow-name="xsl-region-after">
          <fo:block text-align="right" font-style="italic">
            <!-- 检索标记:仅在表格跨页的第一页显示"cont (续)" -->
            <fo:retrieve-marker retrieve-class-name="table-has-more" 
                                retrieve-position="first-ending-with-page"
                                retrieve-boundary="table"/>
          </fo:block>
        </fo:static-content>

        <fo:flow flow-name="xsl-region-body">
          <fo:table table-omit-footer-at-break="false" width="100%">
            <!-- 表格页眉(每一页重复显示) -->
            <fo:table-header>
              <fo:table-row background-color="#eee">
                <fo:table-cell padding="5pt" border="1pt solid #000">
                  <fo:block font-weight="bold">Column 1</fo:block>
                </fo:table-cell>
                <fo:table-cell padding="5pt" border="1pt solid #000">
                  <fo:block font-weight="bold">Column 2</fo:block>
                </fo:table-cell>
              </fo:table-row>
            </fo:table-header>

            <!-- 表格主体 -->
            <fo:table-body>
              <xsl:for-each select="data/table/row">
                <fo:table-row>
                  <!-- 给非最后一行设置标记,说明表格还有后续内容 -->
                  <xsl:if test="position() != last()">
                    <fo:marker marker-class-name="table-has-more">cont (续)</fo:marker>
                  </xsl:if>
                  <fo:table-cell padding="5pt" border="1pt solid #000">
                    <fo:block><xsl:value-of select="col[1]"/></fo:block>
                  </fo:table-cell>
                  <fo:table-cell padding="5pt" border="1pt solid #000">
                    <fo:block><xsl:value-of select="col[2]"/></fo:block>
                  </fo:table-cell>
                </fo:table-row>
              </xsl:for-each>
            </fo:table-body>

            <!-- 表格页脚(每一页重复显示) -->
            <fo:table-footer>
              <fo:table-row background-color="#eee">
                <fo:table-cell padding="5pt" border="1pt solid #000" colspan="2">
                  <fo:block font-weight="bold">Table Footer</fo:block>
                </fo:table-cell>
              </fo:table-row>
            </fo:table-footer>
          </fo:table>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>

</xsl:stylesheet>

代码解释

  • 标记机制:在表格的非最后一行添加fo:marker,标记内容为“cont (续)”,告诉FO引擎表格还有后续内容
  • 检索标记:在页脚区域使用fo:retrieve-marker,仅在表格跨页的第一页末尾提取并显示这个标记内容
  • 页脚保障:通过table-omit-footer-at-break="false"强制页脚在每一页的表格底部都显示
  • 布局优化:设置了合理的页边距和区域大小,避免内容重叠或溢出

这样修改后,当表格内容跨页时,第一页的表格底部会显示“cont (续)”标识,同时每一页都会正常显示表格页眉和页脚。

内容的提问来源于stack exchange,提问作者Logeshwaran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:04:01