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

Robin Becker robin at reportlab.com
Sun Mar 12 06:35:59 EST 2006


Rasha Ebo wrote:
>   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:
>    --------------------------
>.....

I suggest you look first at the example/test test_platypus_xref.py in 
the reportlab/test folder. That illustrates how to get a cross reference 
or test_platypus_toc.py which is a bit simpler.

To make a the link you'll need to use canvas level operations with a 
flowable of your choice. To create the link target you'll need to use a 
bookmark operation.

###these are off the top of my head and untested
###try them out in a simple way first
class linkFlowable(Flowable):
	def __init__(self, target,
  		text=None,
		fontName='Times-Roman',fontSize=10):
		self._tgt = target
		self._text = text
		self._fontName=fontName
		self._fontSize=fontSize

	def wrap(self, w, h):
		c = self.canv
		fs = self._fontsize
		w = c.stringWidth(self._text,self._fontname,fs)
		self._width, self._height = w, 1.2*fs
		return (w,self._height)

	def draw(self):
		c = self.canv
		w, h = self._width, self._height
		c.drawString(0,h/6.,self._text)
        		c.linkURL(self._tgt,(0,0,w,h),
			relative=1,color=None,
			dashArray=None,
			kind='GoToR')

class bookmarkFlowable(Flowable):
	def __init__(self,linkName):
		self._linkName = linkName
	def wrap(self,w,h):
		return 1e-9,1e-9
	def draw(self):
		self.canv.bookmarkHorizontalAbsolute(self._linkName, 72)



-- 
Robin Becker


More information about the reportlab-users mailing list