[reportlab-users] SimpleDocTemplate & Canvas Access
Wed, 14 Apr 2004 18:00:59 +0100
Schollnick, Benjamin wrote:
> I just realized that I don't have canvas access when using the
> SimpleDocTemplate? Or am I confused?
> Let me elaborate... I wish to make a "CheckBox", while using the
> Platypus / SimpleDocTemplate usage of Reportlab. The easiest way to make a
> checkbox, appears to be simply to make a Rectangle using Rect.
> This is a manual example, but is the same basic structure of my
> from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
> from reportlab.lib.styles import getSampleStyleSheet
> from reportlab.rl_config import defaultPageSize
> from reportlab.lib.units import inch
> PAGE_HEIGHT=defaultPageSize; PAGE_WIDTH=defaultPageSize
> styles = getSampleStyleSheet()
> Title = "Hello world"
> pageinfo = "platypus example"
> def myFirstPage(canvas, doc):
> canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-108, Title)
> canvas.drawString(inch, 0.75 * inch, "First Page / %s" % pageinfo)
> def myLaterPages(canvas, doc):
> canvas.drawString(inch, 0.75 * inch, "Page %d %s" % (doc.page,
> def go():
> doc = SimpleDocTemplate("phello.pdf")
> Story = [Spacer(1,2*inch)]
> style = styles["Normal"]
> for i in range(100):
> bogustext = ("This is Paragraph number %s. " % i) *20
> p = Paragraph(bogustext, style)
> doc.build(Story, onFirstPage=myFirstPage, onLaterPages=myLaterPages)
> For the check box code, I am planning on using:
> canvas.rect (x, y, x2, y2, stroke=1) where of course the coords would
> be set.
> But where/when is the canvas actually available outside of the
> I did not see anything mentioned in the docs regarding the canvas
> I am assuming that the doc.build process actually creates the canvas, so
> I'll have to add the check boxes to the myFirstPages/myLaterPages, but I am
> not sure if that would be usable
> due to the fact that I need the boxes to line up with text that is not
> (The text will be read in from a control file, which could be a "infinite"
> number of lines... So getting the two to line up could be difficult.
> Any suggestions?
> - Benjamin
The platypus layout stuff uses flowables. Packers normally set the
attribute canv onto each flowable when it is being wrapped, split or
drawn ie around the wrap, split and draw methods. Inside those methods
you have access to the canvas using the canv attribute of self.