[reportlab-users] SimpleDocTemplate & Canvas Access

Schollnick, Benjamin reportlab-users@reportlab.com
Wed, 14 Apr 2004 12:33:30 -0400


Folks,

	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
application.

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[1]; PAGE_WIDTH=defaultPageSize[0]
styles = getSampleStyleSheet()

Title = "Hello world"
pageinfo = "platypus example"

def myFirstPage(canvas, doc):
	canvas.saveState()
	canvas.setFont('Times-Bold',16)
	canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-108, Title)
	canvas.setFont('Times-Roman',9)
	canvas.drawString(inch, 0.75 * inch, "First Page / %s" % pageinfo)
	canvas.restoreState()

def myLaterPages(canvas, doc):
	canvas.saveState()
	canvas.setFont('Times-Roman',9)
	canvas.drawString(inch, 0.75 * inch, "Page %d %s" % (doc.page,
pageinfo))
	canvas.restoreState()

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)
		Story.append(p)
	
	Story.append(Spacer(1,0.2*inch))
	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
myFirstPage/myLaterPages?
I did not see anything mentioned in the docs regarding the canvas
availablity...

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
defined.

(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