Click here to Skip to main content
Click here to Skip to main content

HTML reporting with page break, using XSL and CSS

By , 16 Mar 2008
 

Preview of report, version A:

Preview of Report A

Preview of report, version B:

Preview of Report 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:

  1. ReportHeader - XSL variable containing the image logo and the tiTitle of the document.
  2. DocumentRecipient - XSL variable containing information about the customer.
  3. OrderHeader - XSL variable containing information about the order.
  4. OrderRowsHeader - XSL variable containing the header table about the ordered products.
  5. 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.
  6. Filler - XSL template containing the blank rows necessary to reach the size of the page.
  7. ReportFooter - XSL variable containing the footer of the report (business address).
  8. 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(' ', ' ', '&#160;')"/>
                </td>
                <td class="blueline" style="width:220px" >
                    <xsl:value-of select="ProductName" />
                    <xsl:value-of select="translate(' ', ' ', '&#160;')"/>
                </td>
                <td style="width:50px" align="right" class="blueline">
                    <xsl:value-of select="Quantity" />
                    <xsl:value-of select="translate(' ', ' ', '&#160;')"/>
                </td>
                <td style="width:50px" align="right" class="blueline">
                    <xsl:value-of select="concat('$ ', UnitPrice)" />
                    <xsl:value-of select="translate(' ', ' ', '&#160;')"/>
                </td>
                <td style="width:50px" align="right" class="blueline">
                    <xsl:value-of select="concat(Discount, ' %')" />
                    <xsl:value-of select="translate(' ', ' ', '&#160;')"/>
                </td>
                <td style="width:100px" align="right" class="blueline">
                    <xsl:value-of select="concat('$ ', ExtendedPrice)" />
                    <xsl:value-of select="translate(' ', ' ', '&#160;')"/>
                </td>
                <td class="tdmargin" />
            </tr>
        </table>
        <xsl:if test="(position() mod 40) = 0 ">
            <!--40 rows per page-->
            <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>
            <!-- case of only one page-->
            <xsl:when test="(position() mod 40 = 0 and position() = 40)">
                <!--40 rows per page-->
                <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>
            <!-- case of more than one page-->
            <xsl:otherwise>
                <xsl:if test="(40 - position() mod 46) = 0 and 
                              position() &gt; (40 + position() mod 40)">
                    <!--46 rows per page-->
                    <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:

    <!-- Template Filler-->
    <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(' ', ' ', '&#160;')"/>
                    </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:

  <!-- Add two blank row -->
  <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>
        <!-- case of only one page-->
        <xsl:when test="count(Order/OrderRows/OrderRow) &lt;= 40">
            <xsl:call-template name="Filler">
                <xsl:with-param name="fillercount" 
                   select="40 - (count(Order/OrderRows/OrderRow))"/>
            </xsl:call-template>
        </xsl:when>
        <!-- case of more than one page-->
        <xsl:otherwise>
            <xsl:call-template name="Filler">
                <!--(Rows per page = 40) -  (Rows in current page) - 
                    (Total Order rows = 3 ) - (Filler Row = 1)-->
                <xsl:with-param name="fillercount" select="40 - 
                   ( ( count(Order/OrderRows/OrderRow)-40 ) mod 40 ) - 3 - 1"/>
            </xsl:call-template>
        </xsl:otherwise>
    </xsl:choose>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Martin Garcia
Software Developer (Senior) BrisConnections Operations Pty Ltd
Australia Australia
Member
Senior Software Developer on Microsoft and Unix-like environments. Senior Data Analyst and Database Developer in particular on the MSSQL and MySQL platforms

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionPDF outputmemberTelson Cheung5 May '09 - 11:19 
If we want to combine this with ASP.NET to stream back PDF of your report back, what would be your recomemdations on how to do this?
Thanks in advance!!
GeneralItems with severall linesmemberpedroalvesimediata20 Apr '09 - 4:45 
Hi. Great article.
 
However, If you have items with description larger than one line, It will not respect the page reporting.
Do you know a workaround? Is it possible to calculate the text html height size?
 
Thanks.
 
Pedro
GeneralRe: Items with severall linesmemberMartin Garcia20 Apr '09 - 5:54 
Hi, thanks for your post.
 
Unfortunatelly it's my opinion that this article shown already a "big" workaround in building html report.
 
I'm afraid that there aren't a specific solution for description larger than one line. I can only say that I considered, calculating the total lines per page, an average quantity of "extra" lines. In other words, I assumed setting 48 lines per page the possibility to have some of these as double lines, for example. Instead to set 52 lines (exacted singled lines) per page. So I thought the possibility that not more of 4 lines can be "doubled" (two rows in one due to his larger text lenght).
 
Hope it's help you.
If you have any suggestion, please let me know.
 
Bye.
Martin.
 
Martin Garcia
 

GeneralRe: Items with severall linesmemberMartin Garcia20 Apr '09 - 6:02 
and
 
> Is it possible to calculate the text html height size?
 
I need to check this but I think yes, in theory.
 
For example, If you will set that one description line can have not more than 50 chars (in according with your layout), you will able to calculate programmatically the number of rows and use this value into a variable instead as row numbers.
 
You agree?
 
Bye.
Martin.
 
Martin Garcia
 

QuestionGood article but got a querymemberPrav4u25 Nov '08 - 22:29 
Hi,
 
It is a good article it solved most of my problems,
but i am still facing a problem,
if you have multiple customers like
 


















 
then your page break doesnt work
 
could you please suggest some sollution
 
Regards
Praveen
 
If you have knowledge, let others light their candles in it.

AnswerRe: Good article but got a querymemberMartin Garcia25 Nov '08 - 22:58 
If I understood good you need to build a nested loop starting with "Customer" data. I cannot read your image in attachment if any.
Hope this help.
Bye.
Martin Garcia
 
Martin Garcia
 

GeneralRe: Good article but got a querymemberPrav4u26 Nov '08 - 0:14 
I am very bad in xsl
if you dont mind can you give me small example
 
Regards
 
Praveen
 
If you have knowledge, let others light their candles in it.

GeneralRe: Good article but got a querymemberPrav4u26 Nov '08 - 0:18 
structure is something like
 
Data
Cusromer
Order
OrderRows
OrderRow
OrderRow
OrderRows
Order
Customer
Cusromer
Order
OrderRows
OrderRow
OrderRow
OrderRows
Order
Customer
Data
 
If you have knowledge, let others light their candles in it.

Generaldoubt on thismemberMaha_M7 Nov '08 - 19:07 
Nice one.
Is it finally render in browser like html page. Upon using demo files, how can I?
 
Maharajan,
software engineer,
Kom7 technologies,
India..

GeneralRe: doubt on thismemberMartin Garcia25 Nov '08 - 22:51 
I have written this article based on my experience in developing XSL reports on a Web application which use owner framework.
I used VS 2005/2008 as debugger. Maybe you can use also other 3rd party tools like Altova XML Spy, XML Pad. Sincerely I don't know.
 
Bye.
Martin Garcia
 
Martin Garcia
 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 16 Mar 2008
Article Copyright 2008 by Martin Garcia
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid