[reportlab-users] Refresh of SimpleDocTemplate
Robin Becker
reportlab-users@reportlab.com
Tue, 29 Apr 2003 19:30:05 +0100
In article <00FFB5EA81D6D51196B60008C7C9BFD3134C0BC8@ntexchg1.conseco.co
m>, Wilgus, Jeff <Jeff_Wilgus@Conseco.com> writes
It seems you define something in the story out side of the build
function.
> Elements = []
> Elements.append(PageBreak()) # So the title page is
> separate.
it would make things more obvious if you put this at the top of
BuildTheReport, but perhaps you're doing something that needs Elements
to be global.
The doc.build() call should clear the Elements list to empty so the next
time you call it, it won't be like the first.
>
> 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)
--
Robin Becker