如何使用FOP保留带有标题的表格但允许在XSL-FO中的表格正文内分页 [英] how to keep table with header but allow page breaks inside table body in XSL-FO using FOP

查看:27
本文介绍了如何使用FOP保留带有标题的表格但允许在XSL-FO中的表格正文内分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Apache FOP 和 XSL-FO 样式表生成 PDF.在 PDF 中,我输出属于表格的标题 (html equiv h1),因此应始终保持在一起.但是,我的表格也可能包含太多数据,以至于它们不一定适合一页.

I'm generating a PDF using Apache FOP and a XSL-FO stylesheet. In the PDF, I output headings (html equiv h1) which belong to a table, so should be kept together always. However, my tables may also contain so much data that they don't necessarily fit on one page.

期望的结果是一个规则集,它允许标题与表格一起保留并尽可能放在一页上,但如果表格数据太长以至于无法放在一页上,那么分页符就会插入到表格中,同时仍将标题元素与表格的开头保持在同一页面上.

The desired result is a rule set that allows a heading to be kept with the table and fit both on one page if possible, but if the table data is so long that it does not fit on one page, then page breaks would be inserted in the table while still keeping the heading element on the same page as the start of the table.

我现在拥有的解决方案确实将标题和表格保持在一起,但如果所有内容都不适合一页,我会收到表格数据的内容溢出错误.我希望 page-break-inside: avoid set on the table 元素会对此有所帮助,但显然不是...建议?

The solution I have right now does keep the heading and the table together, but I get an Content overflows error for the table data if everything does not fit onto one page. I was hoping that page-break-inside: avoid set on the table element would help with this, but apparently not... suggestions?

示例 xsl(为了便于阅读而缩短):

Example xsl (cut short for readability):

<!-- add test table with header that should stay on same page -->
<fo:block font-size="20pt" font-weight="bold">Table header</fo:block>

<fo:table keep-with-previous.within-page="always"
      page-break-inside="avoid" 
      table-layout="fixed" border-width="1mm" border-style="solid">
  <fo:table-column column-width="auto" />
  <fo:table-column column-width="30mm" />

  <fo:table-header text-align="center" background-color="silver">
    <fo:table-row>
      <fo:table-cell padding="1mm" border-width="1mm" border-style="solid">
    <fo:block>First name</fo:block>
      </fo:table-cell>
      <fo:table-cell padding="1mm" border-width="1mm" border-style="solid">
    <fo:block>Last name</fo:block>
      </fo:table-cell>
    </fo:table-row>
  </fo:table-header>
  <fo:table-body>
    <!-- the table-rows can be repeated N times, spanning several pages -->
    <fo:table-row >
      <fo:table-cell padding="1mm" border-width="1mm" border-style="solid">
    <fo:block>ab</fo:block>
      </fo:table-cell>
      <fo:table-cell padding="1mm" border-width="1mm" border-style="solid">
    <fo:block>cd</fo:block>
      </fo:table-cell>
    </fo:table-row>
    <!-- .... more table-row:s ---> 

更新所需的输出

为了更好地描述我正在寻找的结果:

Update on desired output

To better describe what result I'm looking for:

结果文档包含文本段落、标题和表格.

The result document contains both text paragraphs, headings and tables.

  1. 如果内容量使得表格在下一页上只有一两行,那么 xsl-fo 应该将整个表格和标题移动到下一页,前提是它完全适合该页面.
  2. 另一方面,如果表格中的行太多,无论如何都不适合放在一个全新的页面上,那么应该在文档流中立即开始输出,同时仍然允许将表格拆分为多个页面.

如果我删除 page-break-inside="avoid",那么我会得到一个可以很好地跨越多个页面的表格,但是 #1 中描述的情况将不匹配.IE.它不会移动表格,并且如果只有几行最终被移动到下一页,它会完全转到下一页.如果我将分页符留在里面,那么长表格就会出现内容溢出问题,因为页面上放不下的行不会移动到下一页.

If I remove page-break-inside="avoid", then I get a table that breaks across several pages nicely, but cases described in #1 would not be matched. I.e. it does not move a table and it's heading entirely to the next page if only a few rows would end up being moved to the next page. If I leave page-break-inside, then I get a content overflow problem with long tables, because rows that don't fit on the page are not moved to the next page.

我想其中一个基本问题是我的输出表的长度非常动态,不幸的是我无法控制生成到表中的数据量.

I guess one of the basic problems is that my output tables are very dynamic in length and unfortunately I don't have any control over what amount of data gets produced into the tables.

推荐答案

CSS 属性 page-break-inside="avoid" 映射到本机 FO 属性 keep-together="always".这种映射在 IMO 上是有问题的(但那是另一回事了).无论如何,我建议您使用原生 FO 属性 keep-together.within-column="1"而不是分页内.这允许 Apache FOP 在绝对必要时打破表格.

The CSS property page-break-inside="avoid" maps to the native FO property keep-together="always". This mapping is questionable IMO (but that's a different story). Anyway, I suggest you use the native FO property keep-together.within-column="1" instead of page-break-inside. That allows Apache FOP to break inside the table when absolutely necessary.

这篇关于如何使用FOP保留带有标题的表格但允许在XSL-FO中的表格正文内分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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