[reportlab-users] Newbie - platypus or not?

Tim Roberts timr at probo.com
Thu Sep 7 13:57:07 EDT 2006


Andres Luga wrote:

>
> I have attached the desired output, but I don't know the best way to
> achieve it.
>
> 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?


In my opinion, yes.  Platypus is a big win when you have "flowing" text
that might be of unknown size, but in most other situations, I prefer to
draw directly to the canvas.  Canvas even has a "grid" method that can
draw the table for you.

You can put the drawing code for a single business card in a procedure,
and then call that multiple times with the various offsets.

    self.drawBusinessCard( self.baseX + 1 * inch, self.baseY )
    self.drawBusinessCard( self.baseX + 3 * inch, self.baseY )
    self.drawBusinessCard( self.baseX + 5 * inch, self.baseY )
  


> 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?


Something like this, maybe:

    name = ' '.join(( firstname, lastname ))
    width = self.stringWidth( name, baseFont, baseFontSize )
    if width <= nameSize:
        canvas.setFont( baseFont, baseFontSize )
        canvas.drawString( x, y, name )
    else:
        width = self.stringWidth( name, baseFont, baseFontSize - 2 )
        if width <= nameSize:
            canvas.setFont( baseFont, baseFontSize - 2 )
            canvas.drawString( x, y, name )
        else:
            canvas.setFont( baseFont, baseFontSize )
            canvas.drawString( x, y, firstName )
            canvas.drawString( x, y+12, lastName )

Of course, as soon as you do this, you'll have to deal with someone who
uses their middle name in all correspondance, or who insists on having
their middle initial used, or needs the "Sr." or "Jr." suffix, etc. 
Names are a pain in the rear.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the reportlab-users mailing list