使用超过页面大小的XSLT将表包装为PDF



我正在尝试将一个基于XML文件的多列表放入标准A4页面中。问题是超过边距的列没有出现在生成的文件中(如预期的那样(。我想让外部列出现在下一行,但是我找不到任何方法来做到这一点。我试着简单地更改页面的字体大小或方向,但这些解决方案只是一种绕过方法,当列数更高时就会失败。使用普通XSLT1.0是否可以做到这一点?我的代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8"/>
<xsl:template match="/">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="A4" page-height="29cm" page-width="21cm"
margin-bottom="2cm" margin-top="2cm" margin-left="1.5cm" margin-right="1.5cm">
<fo:region-body margin-top="1cm"/>
<fo:region-before extent="1.5cm"/>
<fo:region-after extent="1.5cm"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="A4">

<fo:static-content flow-name="xsl-region-before">
<fo:block>Stylistique suppliers report. Generated <xsl:value-of
select="report/statistics/dateOfGeneration"/></fo:block>
</fo:static-content>
<fo:static-content flow-name="xsl-region-after">
<fo:block>Page <fo:page-number/></fo:block>
</fo:static-content>
<fo:flow flow-name="xsl-region-body">
<fo:block>
<fo:external-graphic src="logo.jpg" width="auto" height="auto"
content-height="300px"/>
</fo:block>
<fo:block linefeed-treatment="preserve">Summary table</fo:block>
<fo:table>
<xsl:for-each select="report/suppliers/supplier">
<fo:table-column column-width="30mm"/>
</xsl:for-each>
<fo:table-header>
<fo:table-row>
<xsl:for-each select="report/suppliers/supplier">
<fo:table-cell>
<fo:block font-weight="bold" width="30mm" font-size="8px">
<xsl:value-of select="name"/>
</fo:block>
</fo:table-cell>
</xsl:for-each>
</fo:table-row>
</fo:table-header>
<fo:table-body>
<fo:table-row>
<xsl:for-each select="report/suppliers/supplier">
<fo:table-cell>
<xsl:for-each select="productList/product">
<fo:block>
<xsl:value-of select="name"/>
</fo:block>
<fo:block>
<xsl:value-of select="priceInPLN"/>
</fo:block>
</xsl:for-each>
</fo:table-cell>
</xsl:for-each>
</fo:table-row>
</fo:table-body>
</fo:table>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>

通过对表行使用fo:block,对每个表单元格使用fo:inline-container(具有固定宽度(来伪造表。

您可能难以使"单元格"具有相同的高度,但您想要做的操作超出了fo:table的范围。

或者,如果您确切地知道表应该在哪里中断,则可以省略fo:table-row,并使用fo:table-cell上的starts-rowends-row属性来引起中断。


由于您使用的是XSLT1.0并且您知道表单元格的宽度,因此可以使用Muenchian分组(https://stackoverflow.com/search?q=muenchian)以对每行适当数量的单元格进行分组。(如果您使用的是XSLT2.0或XSLT3.0,并且可以使用xsl:for-each-group,则会简单得多。(

此外,还有多个关于在表格单元格中包装文本的SO问题,包括:

  • XSL-FO:对表条目强制换行
  • 用FOP中的长单词在表单元格内换行

(如果使用XSLT2.0或XSLT3.0(或使用AH格式化程序FWIW(,则换行文本也会更简单。参见例如。,https://stackoverflow.com/a/33689540/4092205.)

最新更新