[reportlab-users] Subclassing BaseDocTemplate

Robin Becker robin at reportlab.com
Mon Aug 22 05:36:50 EDT 2005


Nicholas Wieland wrote:
> Hi *, I want to subclass BaseDocTemplate as suggested in this list, to use handle_pageBegin and similar methods.
>  
>  
>

Your approach to subclassing is incorrect. You need to call your base methods eg
untested translation of your original with beforePage substituted for 
handle_pageBegin

NB we have only one fixed arg in __init__ so *args went. Also I changed the 
forwarding of kwds in the base class __init__. We need to add self explicitly as 
the first argument.

class IReportDocTemplate(BaseDocTemplate):
   def __init__ (self, ptf_data,**kwds):
     BaseDocTemplate.__init__(self,
       filename = ptf_data ['ptf_code'] + '_' + str (ptf_data 
['reference_date']).replace ('/', ''),
       **kwds)
     self.dossier_number = ptf_data ['dossier_number']
     self.reference_date = ptf_data ['reference_date']

   def beforePage(self):
     BaseDoctemplate.beforePage(self)
     self.canv.saveState ()
     logo = Image.open (_product_home + "/images/logompsam.gif")
     self.canv.drawInlineImage (logo, 6, defaultPageSize [1] - 30, width = 100, 
height = 25)
     canvas.setFont('Tahoma', 12)
     canvas.drawCentredString (defaultPageSize [0] // 2, defaultPageSize [1] - 25,
       "Dossier n.: " + self.dossier_number)
     y, m, d = str (self.reference_date).split ("/")
     d = datetime.date (int (y), int (m), int (d))
     self.canv.drawCentredString (defaultPageSize [0] - 60, defaultPageSize [1] 
- 25,
       d.strftime ("%B %d, %Y"))
     self.canv.restoreState ()

   def build (self, flowables):
     self._calc()
     self.build(self, flowables)


>  
> Obviously this doesn't work. I looked at the SimpleDocTemplate, but it doesn't subclass __init__, that's why I'm having troubles.
> Can someone post a simple example on how to subclass BaseDocTemplate ?
> I can't use SimpleDocTemplate because I have a different PageTemplate for every page. Now I'm using the onPage and onPageEnd methods of PageTemplates, but I don't like it, as I have to use a global variable to display what I want.
> I can of course use what John P. suggested in another post, but I'd like to subclass BaseDocTemplate directly, or at least try :)
>  
> TIA,
>   ngw

-- 
Robin Becker


More information about the reportlab-users mailing list