[reportlab-users] Passing canvas of SimpleDocTemplate as argument
Robin Becker
robin at reportlab.com
Thu Mar 9 05:51:10 EST 2006
Rasha Ebo wrote:
> I make pdf files using the 'SimpleDocTemplate', and I want to make bookmarks to link to the content of the pdf pages.
>
> The following function is the function that sets the layout of every pdf page and makes the bookmarks and it sould take 5 arguments, beside the canavs and doc arguments, other 3 arguments that are used to supply the title of the bookmarks
>
> def myLaterPages(canvas, doc, title, yGroup , clsName):
> global closeit
> titlelist.append(title)
> pageinfo = yGroup + clsName
>
> canvas.saveState()
> canvas.setFont('Courier-Oblique',10)
> canvas.drawString(inch, 0.75 * inch, "Page %d %s" % (doc.page, pageinfo))
>
> canvas.drawString(inch, 10.5 * inch, title)
> canvas.bookmarkHorizontalAbsolute( title , 10.8*inch)
>
> #newsection(title)
> canvas.addOutlineEntry(title +" section", title , level=0, closed=closeit)
>
> closeit = not closeit # close every other one
> makesubsection(canvas, "caps and joins", 8.5*inch)
>
> canvas.restoreState()
> """
>
> The following function makes the subsections of bookmarks
> def makesubsection(canvas, title, horizontal):
> canvas.bookmarkHorizontalAbsolute(title, horizontal)
> #newsubsection(title)
> canvas.addOutlineEntry(title+" subsection", title, level=1)
>
> The following line creates an instance of the DocTemplate
> doc = SimpleDocTemplate("phello.pdf")
>
> The following line builds up the doc and in which I pass the input arguments that are used in the first function 'myLaterPages' which makes up the bookmarks and here comes the problem
> doc.build(Story, onFirstPage=myFirstPage, onLaterPages=myLaterPages(canvas, doc=doc, title='rasha', yGroup ='y1', clsName='y1a'))
>
> When I run the code, the following error occurs:
> 'module' object has no attribute 'saveState'
> refering to line 5 in the first function 'myLaterPages'
>
> I do not know how to pass the arguments of 'myLaterPages' in the building line without generating an error and I think the error is with the canvas argument but the problem is that I use 'SimpleDocTemplate' and not normal canvas so I do not know what to pass, Please help
>
......
First off you don't need to pass arguments into the call back functions on the
build line. They are functions not calls of functions. This is a python problem
not anything to do with the framework.
so your build call should look like
doc.build(Story, onFirstPage=myFirstPage, onLaterPages=myLaterPages)
Second the signature of the callbacks is
def myFirstPage(canvas,doc):
....
they are called by the framework with the required values. So inside these
functions you have access only to the canvas and the DocTemplate instance.
If you want to pass information through to these functions without major changes
to the framework you can do so in several ways.
Make them default argument values at the point of definition.
def myLaterPages(canvas, doc, title='rasha', yGroup ='y1', clsName='y1a'):
.....
or pass them on the doc
eg before the build do
doc.title='rasha'
doc.yGroup='y1'
etc
then in
def myLaterPages(canvas, doc):
.....doc.title...... doc.yGroup etc
--
Robin Becker
More information about the reportlab-users
mailing list