[reportlab-users] Newbie - platypus or not?

Andy Robinson andy at reportlab.com
Thu Sep 7 06:50:42 EDT 2006


Andres Luga wrote:
> Hi,
> 
> I have attached the desired output, but I don't know the best way to 
> achieve it.

Many thanks for starting your question with a clear example - I wish 
everyone did ;-)
> 
> With Platypus it is easy to draw the table (grid), but I don't know
> how to precise-position objects. The only Platypus-object that accepts
> coordinates as parameters seems to be Frame, but it isn't Flowable and
> can't be inside the table cell.
> 
> Am I right that in this situation Platypus doesn't fit and the best
> approach would be using Canvas directly?

You can mix them.  Essentially, you subclass Flowable to create a 
rectangular object of the size you want, and implement its draw method 
to do the graphics; when this gets fired you are drawing on a canvas.
you can then use Platypus for the higher level stuff, and add your own 
widgets into the story or the table cell content.    For example, if 
these are conference badges, you could define a Badge class with 
attributes like firstName, lastName, imageFile, and the wrap(..) and 
draw(...) methods you need.







> 
> Another question - in the document it's preferable that the first and
> last name are on the same line. The font size can be 1 unit smaller,
> if needed. But if the name doesn't fit on the one line even with
> smaller font, then it has to be on two lines with regular font. How to
> achieve that?

Two approaches.  First is to use stringwidth and size it yourself, then 
have an if-statement to handle the multiline case.  The second is to 
"switch modes again" into flowing-content mode, and in your drawing 
code, explicitly create and wrap a paragraph.  Just give it the width 
you need, and a height figure tall enough.

p = Paragraph(text, style)
p.wrap(width, height)
p.drawOn(canvas, x, y)

Hope this helps,


Andy Robinson


More information about the reportlab-users mailing list