[reportlab-users] Subclassing BaseDocTemplate
Robin Becker
robin at reportlab.com
Mon Aug 22 06:07:46 EDT 2005
Robin Becker wrote:
> 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)
obviously the above should really be self.canv which I just noticed.
> 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)
>
>
......
--
Robin Becker
More information about the reportlab-users
mailing list