Preview of report, version A:
Preview of report, version B:
Introduction
This article describes one possible way to build an HTML report that has a dynamic number of pages. I used this technique to solve break-page issue while developing XSL reports on a Web application.
Background
The layout is designed to be printed on the A4 paper size. The data, stored in an XML file, is passed as input for the XSL transformation.
The Code
We can analyze two cases of the same invoice report where the number of pages depends on the order quantity. The first case is a report that contains a document header (recipient address) on all pages. In the second case, the document header is only on the first page. The main difference between the cases is the calculation of the size of the detail section (OrderRows
, in this sample).
In order to obtain the report, I wrote the code as below:
ReportHeader
- XSL variable containing the image logo and the Title
of the document DocumentRecipient
- XSL variable containing information about the customer OrderHeader
- XSL variable containing information about the order OrderRowsHeader
- XSL variable containing the header table about the ordered products OrderRows
- XSL for-each on order rows data. Exactly 40 rows for each page on report version A, 40 rows on first page and 46 rows on all next pages on report version B. Filler
- XSL template containing the blank rows necessary to reach the size of the page ReportFooter
- XSL variable containing the footer of the report (business address) OrderTotals
- XSL variable containing the total amount of the invoice (printed only on the last page)
The CSS page-break command:
<style type="text/css">
.pagebreak {page-break-after: always;}
</style>
Points of Interest
Use of page-break: how to calculate the number of rows and exactly point to put a page-break command.
Case of report version A:
<xsl:for-each select="Order/OrderRows/OrderRow">
<table class="tabledetails" cellspacing="0" style="table-layout:fixed">
<tr>
<td class="tdmargin" />
<td style="width:70px" align="right" class="blueline">
<xsl:value-of select="ProductID" />
<xsl:value-of select="translate(' ', ' ', ' ')"/>
</td>
<td class="blueline" style="width:220px" >
<xsl:value-of select="ProductName" />
<xsl:value-of select="translate(' ', ' ', ' ')"/>
</td>
<td style="width:50px" align="right" class="blueline">
<xsl:value-of select="Quantity" />
<xsl:value-of select="translate(' ', ' ', ' ')"/>
</td>
<td style="width:50px" align="right" class="blueline">
<xsl:value-of select="concat('$ ', UnitPrice)" />
<xsl:value-of select="translate(' ', ' ', ' ')"/>
</td>
<td style="width:50px" align="right" class="blueline">
<xsl:value-of select="concat(Discount, ' %')" />
<xsl:value-of select="translate(' ', ' ', ' ')"/>
</td>
<td style="width:100px" align="right" class="blueline">
<xsl:value-of select="concat('$ ', ExtendedPrice)" />
<xsl:value-of select="translate(' ', ' ', ' ')"/>
</td>
<td class="tdmargin" />
</tr>
</table>
<xsl:if test="(position() mod 40) = 0 ">
<xsl:call-template name="Filler">
<xsl:with-param name="fillercount" select="1" />
</xsl:call-template>
<xsl:copy-of select="$ReportFooter" />
<br class="pagebreak" />
<xsl:copy-of select="$ReportHeader" />
<xsl:call-template name="Filler">
<xsl:with-param name="fillercount" select="1" />
</xsl:call-template>
<xsl:copy-of select="$OrderRecipient"/>
<xsl:call-template name="Filler">
<xsl:with-param name="fillercount" select="1" />
</xsl:call-template>
<xsl:copy-of select="$OrderHeader"/>
<xsl:call-template name="Filler">
<xsl:with-param name="fillercount" select="1" />
</xsl:call-template>
<xsl:copy-of select="$OrderRowsHeader"/>
</xsl:if>
</xsl:for-each>
Case of report version B:
<xsl:for-each select="Order/OrderRows/OrderRow">
<xsl:choose>
<xsl:when test="(position() mod 40 = 0 and position() = 40)">
<xsl:call-template name="Filler">
<xsl:with-param name="fillercount" select="1" />
</xsl:call-template>
<xsl:copy-of select="$ReportFooter" />
<br class="pagebreak" />
<xsl:copy-of select="$ReportHeader" />
<xsl:call-template name="Filler">
<xsl:with-param name="fillercount" select="2" />
</xsl:call-template>
<xsl:copy-of select="$OrderRowsHeader"/>
</xsl:when>
<xsl:otherwise>
<xsl:if test="(40 - position() mod 46) = 0 and
position() > (40 + position() mod 40)">
<xsl:call-template name="Filler">
<xsl:with-param name="fillercount" select="1" />
</xsl:call-template>
<xsl:copy-of select="$ReportFooter" />
<br class="pagebreak" />
<xsl:copy-of select="$ReportHeader" />
<xsl:call-template name="Filler">
<xsl:with-param name="fillercount" select="2" />
</xsl:call-template>
<xsl:copy-of select="$OrderRowsHeader"/>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
The filler template:
<xsl:template name="Filler">
<xsl:param name="fillercount" select="1"/>
<xsl:if test="$fillercount > 0">
<table class="tabledetails">
<tr>
<td>
<xsl:value-of select="translate(' ', ' ', ' ')"/>
</td>
</tr>
</table>
<xsl:call-template name="Filler">
<xsl:with-param name="fillercount" select="$fillercount - 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
Simple use of the filler template: add blank rows:
<xsl:call-template name="Filler">
<xsl:with-param name="fillercount" select="2" />
</xsl:call-template>
Use of the filler template: how to calculate the number of blank rows on the last page:
<xsl:choose>
<xsl:when test="count(Order/OrderRows/OrderRow) <= 40">
<xsl:call-template name="Filler">
<xsl:with-param name="fillercount"
select="40 - (count(Order/OrderRows/OrderRow))"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="Filler">
<xsl:with-param name="fillercount" select="40 -
( ( count(Order/OrderRows/OrderRow)-40 ) mod 40 ) - 3 - 1"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>