[reportlab-users] another question on headers and footers

John Pywtorak jpywtora at calpoly.edu
Mon Aug 8 13:12:19 EDT 2005


Nicholas Wieland wrote:
> How can I pass arguments to onPage and onPageEnd callables ?
> For example, I need to display a date in my header, so actually I'm using a global :(
>  
> Suggestions ?
>   ngw
> 

Sure.  If you wrap your reportlab objects in a python class.  Then make
your onpage and onpageend callables instance, or class methods, thus
they have access to instance variables, or in python class attributes.
For example I use a class that represents the pdf, like so:

def build(self):
  """Build method wraps platypus doc build"""
  self._doc.build(
    self._story,
    onFirstPage = self._on_page_one,
    onLaterPages = self._on_pages,
  )

def _on_page_one(self, canvas, doc):
        canvas.setAuthor("Academic Records/Accessible")
        canvas.setTitle("PolyProgress Degree Audit")
        canvas.setSubject("Degree Audit")
        self._on_pages(canvas, doc)

def _on_pages(self, canvas, doc):
        canvas.saveState()
        canvas.setFont('Helvetica', 8)
        canvas.drawString(7.5 * inch, 10.5 * inch, "Page %d" % doc.page)
        canvas.restoreState()


In this case the self._doc is an instance of SimpleDocTemplate and
self._story is the platypus story, or list of flowables.  Hope that helps.

Johnny P
-- 
Windows
Start Here
Frustrating Hanging Crashing
Blue Screen of Death
Reboot



More information about the reportlab-users mailing list