[reportlab-users] rotated text (90?) in a table cell

Robin Becker robin at reportlab.com
Wed Oct 18 05:35:00 EDT 2006


Paulino wrote:
> Maybe it's better to send the entire script:
> 
> ----------------------
> from reportlab.platypus.flowables import Flowable
> from reportlab.platypus import SimpleDocTemplate, Paragraph, Table
> from reportlab.lib.units import cm
> from reportlab.lib import colors
> from reportlab.lib.styles import getSampleStyleSheet
> stylesheet=getSampleStyleSheet()
> normal = stylesheet['Normal']
> 
> class TTR(Flowable): #TableTextRotate
>    '''Rotates a tex in a table cell.'''
>    def __init__(self, text ):
>        Flowable.__init__(self)
>        self.text=text
>    def draw(self):
>        canvas = self.canv
>        canvas.rotate(90)
>        canvas.drawString( 0, -1, self.text)
> 
> doc=SimpleDocTemplate('vertical.pdf')
> 
> cli=Paragraph( 'Clientes de cobranca muito duvidosa', style=normal)
> 
> data=[ [ 'codigo', TTR('nivel'), 'descricao'],
>        [ '11', '0', 'Caixa'],
>        [ '21', '1', cli ] ]
> 
> st=[( 'ALIGN',(1,0), (1, -1), 'CENTER' ),
>    ('INNERGRID', (0,0), (-1,-1), 0.25, colors.blue ),
>    ('BOX', (0,0), (-1,-1), 0.25, colors.blue ),
>    ('VALIGN', (0,0), (-1,-1), 'MIDDLE' ) ]
> 
> tb=Table(data, colWidths=(40, 40, 100), style=st )
> 
> story=[tb]
> 
> doc.build(story)
> 
......

Flowables need to report the width and height of the wrapped contents; so you 
need to override the wrap method. In this simple case I guess you need something 
like

     def wrap(self,aW,aH):
         canv = self.canv
         return canv._leading, canv.stringWidth(self.text)

because width and height are interchanged.
-- 
Robin Becker


More information about the reportlab-users mailing list