[reportlab-users] Draw images with canvas and use SimpleDocTemplate

Robin Becker robin at reportlab.com
Tue Mar 22 05:50:06 EDT 2011

On 21/03/2011 21:05, Tim Roberts wrote:

> slothy Rulez a lot wrote:

>> Ummm, it's much simpler than that, the image axys positioned over

>> everything, something like drawing with the canvas drawImage method.

>> ¿Is there any flowable? a water mark could be valid too.


> When you call "build" on your SimpleDocTemplate, you can specify

> onFirstPage and onLaterPages functions. Those are called at the

> beginning of each page, and they are passed a canvas for the whole

> page. They are called before drawing begins. If you need a callback

> after drawing ends, you'll have to create your own page templates with

> an "afterDrawPage" method. That means using BaseDocTemplate instead of

> SimpleDocTemplate.


I think there's a choice. The existing page templates call
onPageEnd(canv,doctemplate) and immediately afterwards the doctemplate calls
afterPage on itself.

So if you want you can inherit from SimpleDocTemplate and mess with either of
those two mechanisms.

For the first you can override handle_documentBegin eg

class MyDocTemplate(SimpleDocTemplate):
def handle_documentBegin(self):
do stuff to self.pageTemplates to add onPage end etc etc

alternatively and probably easier use the afterPage method

class MyDocTemplate(SimpleDocTemplate):
def afterPage(self):
do stuff to self.canv to draw your watermark

then just use your derived class instead of SimpleDocTemplate
Robin Becker

More information about the reportlab-users mailing list