python - Is it possible to get a Flowable's coordinate position once it's rendered using ReportLab.platypus? -
my main goal have image flowables on page act though clickable links. in order this, create canvas.linkrect() , place on rendered image. here's example of how use canvas.linkrect():
canvas.linkurl( url='url_goes_here', rect=(x1, y1, x2, y2), #(x1, y1) bottom left coordinate of rectangle, (x2, y2) top right thickness=0, relative=1 )
after looking in basedoctemplate class, found method called afterflowable(self, flowable). overrode method , called dir() on flowable passed in, resulting in this:
['__call__', '__doc__', '__init__', '__module__', '_doctemplateattr', '_drawon', '_fixedheight', '_fixedwidth', '_framename', '_halignadjust', '_showboundary', '_traceinfo', 'action', 'apply', 'draw', 'drawon', 'encoding', 'getkeepwithnext', 'getspaceafter', 'getspacebefore', 'halign', 'height', 'identity', 'isindexing', 'locchanger', 'minwidth', 'split', 'spliton', 'valign', 'width', 'wrap', 'wrapon', 'wrapped']
it has width , height attribute use determine how big linkrect() should (what x2 , y2 should be), no information on flowable starts (what should x1 , y1 be?).
if else fails, thought of somehow pairing frame , image flowable since frame has information want create linkrect(). however, seems hassle know when , how order list of frames it's respective list of flowables, on top of having know put frames images. there way achieve or not possible?
thanks!
after working on day today, figured out nice way this! here's how did else feature of hyperlinked image flowables in pdfs.
basically, reportlab.platypus.flowables
has class called flowable
image
inherits from. flowable has method called drawon(self, canvas, x, y, _sw=0)
override in new class created called hyperlinkedimage
.
from reportlab.platypus import image class hyperlinkedimage(image, object): # variable added __init__() hyperlink. default none if statement use later. def __init__(self, filename, hyperlink=none, width=none, height=none, kind='direct', mask='auto', lazy=1): super(hyperlinkedimage, self).__init__(filename, width, height, kind, mask, lazy) self.hyperlink = hyperlink def drawon(self, canvas, x, y, _sw=0): if self.hyperlink: # if hyperlink given, create canvas.linkurl() x1 = self.halignadjust(x, _sw) # adjusting x coordinate according alignment given flowable (right, left, center) y1 = y x2 = x1 + self._width y2 = y1 + self._height canvas.linkurl(url=self.hyperlink, rect=(x1, y1, x2, y2), thickness=0, relative=1) super(hyperlinkedimage, self).drawon(canvas, x, y, _sw)
now instead of creating reportlab.platypus.image image flowable, use new hyperlinkedimage instead :)
Comments
Post a Comment