[reportlab-users] Re: Passing canvas of SimpleDocTemplate as argument

Rasha Ebo rshzky at yahoo.com
Sun Mar 12 05:58:28 EST 2006


  Hi Robin, peace be upon you
   
  Thanks for your help, It has worked with me to pass the arguments on the doc as you have sugessted:
   --------------------------
  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
  -------------------------------------
   
  but what I exactly do is that I retieve student's data from a database and display data of each student in a page and I want to make bookmarks by students names such that when I click a student name, I go to his/her page.
   Now when I use the 'doc.argument name'  solution, the value set to the argument variable is the data of the last student in the recordset only, meaning that during the loop of the database recordset, data of each next student overwrites the previous one and data of the last student only is stored. Thus ALL bookmarks is labeled with the name of the LAST student only.
  I want to send the arguments to the bookmarks function by student name  and not by doc
   
   Can you help me and please notice that I am a newbebi in both python and reportlab.

   
  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






		
---------------------------------
Relax. Yahoo! Mail virus scanning helps detect nasty viruses!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://two.pairlist.net/pipermail/reportlab-users/attachments/20060312/5c243911/attachment.html


More information about the reportlab-users mailing list