Solved

reportlab multipage table

Posted on 2008-06-18
5
2,727 Views
Last Modified: 2010-10-15
i m using reportlab for pdf. i m trying generate a table but when table is long it won't go to multipage.

another thing i want to repeat header on multipage table.

is there any idea how do i do that i m posting my code in it.

give me example if it possible. thanks

please help me out.
ts = [('ALIGN', (1,1), (-1,-1), 'CENTER'),
            ('ALIGN', (-2,0), (-2,-1), 'CENTER'),
            ('VALIGN', (0,0), (-1,-1), 'MIDDLE'),
            ('FONT', (0,0), (-1,-1), 'Helvetica', 9),
            ('BOX', (0,0), (-1, -1), tableBorder, colors.black), tableGrid
             ,
            ('BACKGROUND', (0,0), (-1,0), colors.Color(0.85,0.85,0.85))]
 
        table = Table(data, style=ts, splitByRow = 1 )
        Flowable.wrapOn(table,c,400,625)
        
        table.drawOn(c, posX*cm, posY*cm)

Open in new window

0
Comment
Question by:deepakabc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 29

Accepted Solution

by:
Pravin Asar earned 500 total points
ID: 21846990
For a long table, you can have a fixed header and scrollable table rows.

you can define CSS for print media to add header, footer.

Look the following code.

 
<!-- This comment keeps IE6/7 in the reliable quirks mode -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
 
<title>Scrollable Table with Fixed Header, repeat print header and footer</title>
 
<meta name="keywords" content="fixed table header; non-scroll header; fixed footer; freeze header; CSS tips; print repeating headers; print repeating footers">
 
 
<style type="text/css">
div.tableContainer {
	width: 80%;		/* table width will be 99% of this*/
	height: 400px; 	/* must be greater than tbody*/
	overflow: auto;
	margin: 0 auto;
	}
 
table {
	width: 99%;		/*100% of container produces horiz. scroll in Mozilla*/
	border: none;
	background-color: #f7f7f7;
	}
	
table, tbody	{  /* child selector syntax which IE6 and older do not support*/
	overflow: auto; 
	height: 25px;
	overflow-x: hidden;
	}
	
thead tr	{
	position:relative; 
	top: expression(offsetParent.scrollTop); /*IE5+ only*/
	}
	
thead td, thead th {
	text-align: center;
	font-size: 14px; 
	background-color: oldlace;
	color: steelblue;
	font-weight: bold;
	border-top: solid 1px #d8d8d8;
	}	
	
td	{
	color: #000;
	padding-right: 2px;
	font-size: 12px;
	text-align: right;
	border-bottom: solid 1px #d8d8d8;
	border-left: solid 1px #d8d8d8;
	}
	
table tfoot tr { 
      position: relative; 
      overflow-x: hidden;
      top: expression(parentNode.parentNode.offsetHeight >= 
	  offsetParent.offsetHeight ? 0 - parentNode.parentNode.offsetHeight + offsetParent.offsetHeight + offsetParent.scrollTop : 0);
      }
 
 
tfoot td	{
	text-align: center;
	font-size: 11px;
	font-weight: bold;
	background-color: papayawhip;
	color: steelblue;
	border-top: solid 1px slategray;
	}
 
td:last-child {padding-right: 20px;}
 
</style>
 
 
 
<!-- print style sheet -->
<style type="text/css" media="print">
div.tableContainer {overflow: visible;	}
table,tbody	{overflow: visible; }
td {height: 14pt;} /*adds control for test purposes*/
thead td	{font-size: 11pt;	}
tfoot td	{
	text-align: center;
	font-size: 9pt;
	border-bottom: solid 1px slategray;
	}
	
thead	{display: table-header-group;	}
tfoot	{display: table-footer-group;	}
thead th, thead td	{position: static; } 
 
thead tr	{position: static; } /*prevent problem if print after scrolling table*/ 
table tfoot tr {     position: static;    }
</style>
 
</head>
 
<body>
 
<div id="container">
<div class="tableContainer">
  <table cellspacing="0" width="90%">
     <thead>
      <tr> 
        <td width="18%">Station</td>
        <td width="38%">Date</td>
        <td width="28%">Status</td>
        <td width="16%">Num.</td>
      </tr>
    </thead>
    <tfoot>
      <tr> 
        <td colspan="5">Table footer repeats on print</td>
      </tr>
    </tfoot>
    <tbody>
      <tr> 
        <td>KABC</td>
        <td>09/12/2002</td>
        <td>Submitted</td>
        <td>0</td>
      </tr>
      <tr> 
        <td>KCBS</td>
        <td>09/11/2002</td>
        <td>Lockdown</td>
        <td>2</td>
      </tr>
      <tr> 
        <td>WFLA</td>
        <td>09/11/2002</td>
        <td>Submitted</td>
        <td>1</td>
      </tr>
      <tr> 
        <td>WTSP</td>
        <td>09/15/2002</td>
        <td>In-Progress</td>
        <td>10</td>
      </tr>
      <tr> 
        <td>WROC</td>
        <td>10/11/2002</td>
        <td>Submitted</td>
        <td>12</td>
      </tr>
      <tr> 
        <td>WPPP</td>
        <td>09/16/2002</td>
        <td>In-Progress</td>
        <td>0</td>
      </tr>
      <tr> 
        <td>WRRR</td>
        <td>09/06/2002</td>
        <td>Submitted</td>
        <td>5</td>
      </tr>
      <tr> 
        <td>WTTT</td>
        <td>09/21/2002</td>
        <td>In-Progress</td>
        <td>0</td>
      </tr>
      <tr> 
        <td>W000</td>
        <td>11/11/2002</td>
        <td>Submitted</td>
        <td>7</td>
      </tr>
      <tr> 
        <td>KABC</td>
        <td>10/01/2002</td>
        <td>Submitted</td>
        <td>10</td>
      </tr>
      <tr> 
        <td>KCBS</td>
        <td>10/18/2002</td>
        <td>Lockdown</td>
        <td>2</td>
      </tr>
      <tr> 
        <td>WFLA</td>
        <td>10/18/2002</td>
        <td>Submitted</td>
        <td>1</td>
      </tr>
      <tr> 
        <td>WTSP</td>
        <td>10/19/2002</td>
        <td>In-Progress</td>
        <td>0</td>
      </tr>
      <tr> 
        <td>WROC</td>
        <td>07/18/2002</td>
        <td>Submitted</td>
        <td>2</td>
      </tr>
      <tr> 
        <td>WPPP</td>
        <td>10/28/2002</td>
        <td>In-Progress</td>
        <td>10</td>
      </tr>
      <tr> 
        <td>WRRR</td>
        <td>10/28/2002</td>
        <td>Submitted</td>
        <td>5</td>
      </tr>
      <tr> 
        <td>WTTT</td>
        <td>10/08/2002</td>
        <td>In-Progress</td>
        <td>0</td>
      </tr>
      <tr> 
        <td>WIL0</td>
        <td>10/18/2001</td>
        <td>Submitted</td>
        <td>7</td>
      </tr>
      <tr> 
        <td>KABC</td>
        <td>04/18/2002</td>
        <td>Submitted</td>
        <td>0</td>
      </tr>
      <tr> 
        <td>KCBS</td>
        <td>10/05/2001</td>
        <td>Lockdown</td>
        <td>2</td>
      </tr>
      <tr> 
        <td>WFLA</td>
        <td>10/18/2002</td>
        <td>Submitted</td>
        <td>1</td>
      </tr>
      <tr> 
        <td>WTSP</td>
        <td>10/19/2002</td>
        <td>In-Progress</td>
        <td>0</td>
      </tr>
      <tr> 
        <td>WROC</td>
        <td>12/18/2002</td>
        <td>Submitted</td>
        <td>2</td>
      </tr>
      <tr> 
        <td>WPPP</td>
        <td>12/28/2002</td>
        <td>In-Progress</td>
        <td>8</td>
      </tr>
      <tr> 
        <td>WRRR</td>
        <td>12/20/2002</td>
        <td>Submitted</td>
        <td>5</td>
      </tr>
      <tr> 
        <td>WTTT</td>
        <td>12/11/2002</td>
        <td>In-Progress</td>
        <td>0</td>
      </tr>
      <tr> 
        <td>W0VB</td>
        <td>01/18/2003</td>
        <td>Submitted</td>
        <td>17</td>
      </tr>
      <tr> 
        <td>KABC</td>
        <td>12/17/2002</td>
        <td>Submitted</td>
        <td>20</td>
      </tr>
      <tr> 
        <td>KCBS</td>
        <td>12/16/2002</td>
        <td>Lockdown</td>
        <td>2</td>
      </tr>
      <tr> 
        <td>WFAA</td>
        <td>12/18/2002</td>
        <td>Submitted</td>
        <td>1</td>
      </tr>
      <tr> 
        <td>WTSP</td>
        <td>12/18/2002</td>
        <td>In-Progress</td>
        <td>0</td>
      </tr>
      <tr> 
        <td>WROC</td>
        <td>12/19/2002</td>
        <td>Submitted</td>
        <td>2</td>
      </tr>
      <tr> 
        <td>WPPP</td>
        <td>12/06/2002</td>
        <td>In-Progress</td>
        <td>0</td>
      </tr>
      <tr> 
        <td>WRRR</td>
        <td>12/28/2002</td>
        <td>Submitted</td>
        <td>5</td>
      </tr>
      <tr> 
        <td>WTTT</td>
        <td>12/30/2002</td>
        <td>In-Progress</td>
        <td>0</td>
      </tr>
      <tr> 
        <td>UMBA</td>
        <td>12/26/2002</td>
        <td>Submitted</td>
        <td>7</td>
      </tr>
      <tr> 
        <td>KABC</td>
        <td>12/18/2002</td>
        <td>Submitted</td>
        <td>0</td>
      </tr>
      <tr> 
        <td>KCBS</td>
        <td>12/29/2002</td>
        <td>Lockdown</td>
        <td>2</td>
      </tr>
      <tr> 
        <td>WFFF</td>
        <td>12/22/2002</td>
        <td>Submitted</td>
        <td>1</td>
      </tr>
      <tr> 
        <td>WTSP</td>
        <td>12/18/2001</td>
        <td>In-Progress</td>
        <td>9</td>
      </tr>
      <tr> 
        <td>WROC</td>
        <td>11/19/2001</td>
        <td>Submitted</td>
        <td>2</td>
      </tr>
      <tr> 
        <td>WPPP</td>
        <td>11/20/2001</td>
        <td>In-Progress</td>
        <td>0</td>
      </tr>
      <tr> 
        <td>WRRR</td>
        <td>10/19/2001</td>
        <td>Submitted</td>
        <td>5</td>
      </tr>
      <tr> 
        <td>WTTT</td>
        <td>11/29/2001</td>
        <td>In-Progress</td>
        <td>8</td>
      </tr>
      <tr> 
        <td>KPLT</td>
        <td>11/19/2001</td>
        <td>Submitted</td>
        <td>7</td>
      </tr>
      <tr> 
        <td>KABC</td>
        <td>11/19/2001</td>
        <td>Submitted</td>
        <td>13</td>
      </tr>
      <tr> 
        <td>KBRE</td>
        <td>11/19/2001</td>
        <td>Lockdown</td>
        <td>2</td>
      </tr>
      <tr> 
        <td>WFLA</td>
        <td>11/19/2001</td>
        <td>Submitted</td>
        <td>1</td>
      </tr>
      <tr> 
        <td>WTSP</td>
        <td>02/19/2003</td>
        <td>In-Progress</td>
        <td>0</td>
      </tr>
      <tr> 
        <td>WROC</td>
        <td>02/17/2003</td>
        <td>Submitted</td>
        <td>2</td>
      </tr>
      <tr> 
        <td>WPPP</td>
        <td>02/16/2003</td>
        <td>In-Progress</td>
        <td>16</td>
      </tr>
      <tr> 
        <td>WRRR</td>
        <td>02/29/2003</td>
        <td>Submitted</td>
        <td>5</td>
      </tr>
      <tr> 
        <td>WTTT</td>
        <td>03/19/2003</td>
        <td>In-Progress</td>
        <td>19</td>
      </tr>
      <tr> 
        <td>KLTR</td>
        <td>02/10/2003</td>
        <td>Submitted</td>
        <td>7</td>
      </tr>
      <tr> 
        <td>KABC</td>
        <td>04/05/2003</td>
        <td>Submitted</td>
        <td>16</td>
      </tr>
      <tr> 
        <td>KCBS</td>
        <td>02/19/2003</td>
        <td>Lockdown</td>
        <td>2</td>
      </tr>
      <tr> 
        <td>WFLA</td>
        <td>02/16/2003</td>
        <td>Submitted</td>
        <td>1</td>
      </tr>
      <tr> 
        <td>WTSP</td>
        <td>02/13/2003</td>
        <td>In-Progress</td>
        <td>5</td>
      </tr>
      <tr> 
        <td>WROC</td>
        <td>02/14/2003</td>
        <td>Submitted</td>
        <td>2</td>
      </tr>
      <tr> 
        <td>WPPP</td>
        <td>03/19/2003</td>
        <td>In-Progress</td>
        <td>0</td>
      </tr>
      <tr> 
        <td>WRRR</td>
        <td>02/19/2002</td>
        <td>Submitted</td>
        <td>5</td>
      </tr>
      <tr> 
        <td>WTTT</td>
        <td>02/19/2002</td>
        <td>In-Progress</td>
        <td>0</td>
      </tr>
      <tr> 
        <td>WYYD</td>
        <td>02/11/2002</td>
        <td>Submitted</td>
        <td>7</td>
      </tr>
      <tr> 
        <td>KABC</td>
        <td>02/19/2002</td>
        <td>Submitted</td>
        <td>11</td>
      </tr>
      <tr> 
        <td>KCBS</td>
        <td>02/19/2002</td>
        <td>Lockdown</td>
        <td>12</td>
      </tr>
      <tr> 
        <td>WFLA</td>
        <td>05/19/2002</td>
        <td>Submitted</td>
        <td>1</td>
      </tr>
      <tr> 
        <td>WTSP</td>
        <td>02/20/2002</td>
        <td>In-Progress</td>
        <td>0</td>
      </tr>
      <tr> 
        <td>WROC</td>
        <td>05/20/2002</td>
        <td>Submitted</td>
        <td>2</td>
      </tr>
      <tr> 
        <td>WPPP</td>
        <td>02/19/2003</td>
        <td>In-Progress</td>
        <td>13</td>
      </tr>
      <tr> 
        <td>WRRR</td>
        <td>02/19/2002</td>
        <td>Submitted</td>
        <td>5</td>
      </tr>
 
    </tbody>
  </table>
 
</div>
 
 
</div> <!-- end container -->
</body>
</html>

Open in new window

0
 

Expert Comment

by:OpenRoadTech
ID: 33914686
The solution given above is for HTML, not for reportlab pdfs.  You instead want to use a document template, like so:


def myFirstPage(canvas, doc):
        canvas.saveState()
        canvas.setFont('Times-Roman',9)
        canvas.drawString(inch, 0.75 * inch, "Page %d %s" % (doc.page, pageinfo))
        canvas.restoreState()

    #We define the fixed features of the first page of the document with the function above.
    def myLaterPages(canvas, doc):
        canvas.saveState()
        canvas.setFont('Times-Roman',9)
        canvas.drawString(inch, 0.75 * inch, "Page %d %s" % (doc.page, pageinfo))
        canvas.restoreState()

    doc = SimpleDocTemplate(filename)
    doc.topMargin = .5*inch

    Story = []

    grid_style_attrs = [('LINEABOVE', (0,0), (MAX_COL, 0), 1.2, colors.black),
                        ('LINEBELOW', (0,0), (MAX_COL, 0), 1.1, colors.black),
                        ('OUTLINE', (0,0), (MAX_COL, 0), 1.1, colors.black),
                        ('OUTLINE', (0,1), (MAX_COL, -1), 1, colors.black),
                        ('GRID', (0,0), (-1,-1), 0.25, colors.black),
                        ('ALIGN', (1,1), (-1,-1), 'RIGHT'),
                        ]

    GRID_STYLE = TableStyle(grid_style_attrs)

    data = [['col1', 'col2', 'col3'],
            [1, 2, 3], [4, 5, 6], [7, 8, 9]]

    t = Table(data, style=GRID_STYLE, repeatRows=1, repeatCols=1, 
              colWidths=[.4*inch, .5*inch, .42*inch])
    Story.append(t)
    Story.append(Spacer(1,.2*inch))

    doc.build(Story, onFirstPage=myFirstPage, onLaterPages=myLaterPages)

Open in new window

0
 

Expert Comment

by:OpenRoadTech
ID: 33914699
Excuse me, that first

def myFirstPage(canvas, doc):

should be indented.
0

Featured Post

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help Creating a Powershell script 8 96
Hta File displays dynamic File names 4 65
Scripting 4 33
Writing reports to a script 7 27
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question