[reportlab-users] Refresh of SimpleDocTemplate

Wilgus, Jeff reportlab-users@reportlab.com
Tue, 29 Apr 2003 10:04:18 -0500


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C30E60.9B528392
Content-Type: text/plain;
	charset="iso-8859-1"

I've inherited a program that creates a PDF with information about a number
of servers.

Currently the program uses the subclass SimpleDocTemplate for creating this
report.

Because of the size of the report, I want to create a report per server.

My problem: creating the second document works fine, but the onFirstPage,
onLaterPages logic no longer recognizes separating the first page from the
later pages.  The first report looks great, but all subsequent reports have
the cover page info overlaid on top of the first report page.

Forgive me, but I'm a bit new to ReportLabs functionality.  How can I
"refresh" the environment to get each document printing with recognition of
the first page, later page logic?

My imports look like:

	# ReportLab import statements
	from reportlab.platypus import *
	from reportlab.lib.styles import getSampleStyleSheet
	from reportlab.lib.units import inch
	from reportlab.lib import colors
	
	styles = getSampleStyleSheet()
	
	Elements = []
	Elements.append(PageBreak())  # So the title page is separate.
	
	Title = 'vDASS Reports'
	Title2 = "Bankers Life and Casualty"
	arcane_dba = 'Jeff Wilgus'
	arcane_email = 'jwilgus@dbacrew.com'
	arcane_url = 'www.dbacrew.com'
	sql = "select name from scheduler where scheduler_id = %s" %
(ID['Scheduler_ID'])
	rs = vUtils.ExecSQL(sql, vUtils.db)
	RptTitle = rs[0][0]
	PrintTime = 'Generated: %s' % (DateTime.now().Format())
	pageinfo = '%s / %s / %s' % (arcane_url, arcane_dba, arcane_email)
	
	def myFirstPage(canvas, doc):
		canvas.saveState()
		canvas.setFont('Times-Bold',24)
		canvas.drawString(108, 792-108, Title) #PAGE_HEIGHT-108,
Title)
		canvas.drawString(108, 792-138, Title2) #PAGE_HEIGHT-108,
Title)
		canvas.setFont('Times-Bold',16)
		canvas.drawString(108, 792-216, RptTitle) #PAGE_HEIGHT-108,
Title)
		canvas.setFont('Times-Roman',10)
		canvas.drawString(inch, inch*0.75, PrintTime)
#PAGE_HEIGHT-108, Title)
		canvas.drawString(inch, inch*0.5, 'First Page / %s' %
(pageinfo))
		canvas.restoreState()
	
	def myLaterPages(canvas, doc):
		canvas.saveState()
	
		canvas.setFont('Times-Roman',10)
		canvas.drawString(inch, inch*0.5, 'Page %d - %s' %
(doc.page, pageinfo))
		canvas.restoreState() 
	
And the call to build each report is ...

	def BuildTheReport(rlvl):
		Elements.insert(0, Spacer(0, inch))
		RunDate = DateTime.now()
	
		if ID['Filename'] == 'Summary':
			prefx = "Sum_"
		else:
			prefx = "Dtl_"

		if rlvl == 1:
			rptfile = '/var/www/html/%s%s.pdf' % (ID['Filename']
+ "_", RunDate.strftime('%Y%m%d'))
		else:
			rptfile = '/var/www/html/%s%s.pdf' % (prefx +
ID['SI_Name'] + "_", RunDate.strftime('%Y%m%d'))
	
		doc = SimpleDocTemplate(rptfile,pagesize=(8.5*inch,
11*inch))  #, showBoundary=1)
		doc.topMargin = 0.0
		doc.bottomMargin = 0.75*inch
		doc.leftMargin = 0.75*inch
		doc.rightMargin = 0.75*inch
	
		doc.build(Elements, onFirstPage=myFirstPage,
onLaterPages=myLaterPages)
		return(rptfile)

------_=_NextPart_001_01C30E60.9B528392
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Diso-8859-1">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
5.5.2655.35">
<TITLE>Refresh of SimpleDocTemplate</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=3D2>I've inherited a program that creates a PDF with =
information about a number of servers.</FONT>
</P>

<P><FONT SIZE=3D2>Currently the program uses the subclass =
SimpleDocTemplate for creating this report.</FONT>
</P>

<P><FONT SIZE=3D2>Because of the size of the report, I want to create a =
report per server.</FONT>
</P>

<P><FONT SIZE=3D2>My problem: creating the second document works fine, =
but the onFirstPage, onLaterPages logic no longer recognizes separating =
the first page from the later pages.&nbsp; The first report looks =
great, but all subsequent reports have the cover page info overlaid on =
top of the first report page.</FONT></P>

<P><FONT SIZE=3D2>Forgive me, but I'm a bit new to ReportLabs =
functionality.&nbsp; How can I "refresh" the environment to get each =
document printing with recognition of the first page, later page =
logic?</FONT></P>

<P><FONT SIZE=3D2>My imports look like:</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2># =
ReportLab import statements</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>from =
reportlab.platypus import *</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>from =
reportlab.lib.styles import getSampleStyleSheet</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>from =
reportlab.lib.units import inch</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>from =
reportlab.lib import colors</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>styles =
=3D getSampleStyleSheet()</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>Elements =
=3D []</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>Elements.append(PageBreak())&nbsp; # So the title page is =
separate.</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>Title =3D =
'vDASS Reports'</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>Title2 =
=3D &quot;Bankers Life and Casualty&quot;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>arcane_dba =3D 'Jeff Wilgus'</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>arcane_email =3D 'jwilgus@dbacrew.com'</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>arcane_url =3D 'www.dbacrew.com'</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>sql =3D =
&quot;select name from scheduler where scheduler_id =3D %s&quot; % =
(ID['Scheduler_ID'])</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>rs =3D =
vUtils.ExecSQL(sql, vUtils.db)</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>RptTitle =
=3D rs[0][0]</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>PrintTime =
=3D 'Generated: %s' % (DateTime.now().Format())</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>pageinfo =
=3D '%s / %s / %s' % (arcane_url, arcane_dba, arcane_email)</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>def =
myFirstPage(canvas, doc):</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>canvas.saveState()</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>canvas.setFont('Times-Bold',24)</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>canvas.drawString(108, 792-108, Title) #PAGE_HEIGHT-108, =
Title)</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>canvas.drawString(108, 792-138, Title2) #PAGE_HEIGHT-108, =
Title)</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>canvas.setFont('Times-Bold',16)</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>canvas.drawString(108, 792-216, RptTitle) #PAGE_HEIGHT-108, =
Title)</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>canvas.setFont('Times-Roman',10)</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>canvas.drawString(inch, inch*0.75, PrintTime) =
#PAGE_HEIGHT-108, Title)</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>canvas.drawString(inch, inch*0.5, 'First Page / %s' % =
(pageinfo))</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>canvas.restoreState()</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>def =
myLaterPages(canvas, doc):</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>canvas.saveState()</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>canvas.setFont('Times-Roman',10)</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>canvas.drawString(inch, inch*0.5, 'Page %d - %s' % (doc.page, =
pageinfo))</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>canvas.restoreState() </FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
<BR><FONT SIZE=3D2>And the call to build each report is ...</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>def =
BuildTheReport(rlvl):</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>Elements.insert(0, Spacer(0, inch))</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>RunDate =3D =
DateTime.now()</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>if =
ID['Filename'] =3D=3D 'Summary':</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>prefx =3D =
&quot;Sum_&quot;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>else:</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>prefx =3D =
&quot;Dtl_&quot;</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>if rlvl =
=3D=3D 1:</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>rptfile =3D =
'/var/www/html/%s%s.pdf' % (ID['Filename'] + &quot;_&quot;, =
RunDate.strftime('%Y%m%d'))</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>else:</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>rptfile =3D =
'/var/www/html/%s%s.pdf' % (prefx + ID['SI_Name'] + &quot;_&quot;, =
RunDate.strftime('%Y%m%d'))</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>doc =3D =
SimpleDocTemplate(rptfile,pagesize=3D(8.5*inch, 11*inch))&nbsp; #, =
showBoundary=3D1)</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>doc.topMargin =
=3D 0.0</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>doc.bottomMargin =3D 0.75*inch</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>doc.leftMargin =3D 0.75*inch</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>doc.rightMargin =3D 0.75*inch</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>doc.build(Elements, onFirstPage=3DmyFirstPage, =
onLaterPages=3DmyLaterPages)</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>return(rptfile)</FONT>
</P>

</BODY>
</HTML>
------_=_NextPart_001_01C30E60.9B528392--