[reportlab-users] KeepInFrame.drawOn() working?

Robin Becker robin at reportlab.com
Tue Oct 3 14:17:38 EDT 2006


Dinu Gherman wrote:
> Robin Becker:
> 
>> why not just use the 'preferred' route and call wrapOn with the
>> intended canvas, width and height. Then at least the shrink would
>> have a chance to do stuff.
> 
> Because the 'preferred' route does not always seem to work.
> I attach a sample generator file and PDF output and you can
> try to recreate the PDF without using drawOn()...
> 
> Dinu


Well I did this
########################################################
from reportlab.platypus.flowables import KeepInFrame, XBox
path = splitext(basename(sys.argv[0]))[0] + ".pdf"
canv = Canvas(path, pagesize=A4)
x, y, w, h = 10*mm, 10*mm, 100*mm, 50*mm
canv.rect(x, y, w, h)
	
box = XBox(w - 20*mm, 15*mm, "XBox")
# in next line only "truncate/overflow" seem to work as expected
# but not "shrink/error"
border = KeepInFrame(w, h, [box], mode="truncate")

# following three lines are needed to avoid
# AttributeErrors in KeepInFrame
#border.width = w
#border.height = h

aw,ah=border.wrapOn(canv,w,h)
print w/mm,h/mm, border.width/mm, border.height/mm

border.drawOn(canv, x, y) # THIS SEEMS TO BE NEEDED
print "consumed XBox space (mm):", aw/mm, ah/mm

canv.showPage()
canv.save()
print "generated", path
########################################################


output was
100.0 50.0 79.9999996472 14.9999996472
consumed XBox space (mm): 79.9999996472 14.9999996472
generated sss.pdf

so the border object was correctly sized as 80 x 15 mm
and then correctly drawn at the correct location (at the bottom left)
of the outer rectangle. In your drawing the xbox appears at the top left which 
to me seems wrong since you asked for the border (and presumably its contents) 
to be drawn at 10mm, 10mm. The keepinframe thing is variable sized up to the 
maximum specified in the creation call, but nothing tells it to draw at a 
particular place relative to the external rectangle.

When the frame adds, the KeepInFrame is added top down in the frame at the next 
available space. I originally wanted the make the KeepInFrame a special sort of 
frame, but Andy didn't like the idea so it became a sort of squashable container 
flowable.
-- 
Robin Becker


More information about the reportlab-users mailing list