[reportlab-users] Table and document widths

Robin Becker reportlab-users@reportlab.com
Wed, 7 Jan 2004 21:07:07 +0000


In article <20040107085934.GA23227@gbdirect.co.uk>, Will Newton
<will@gbdirect.co.uk> writes
>On Tue, Jan 06, 2004 at 05:32:14PM +0000, Robin Becker wrote:
>
>> well you can assume that the doc width is 12 points too large if you
>> want to do percentages. Unfortunately we don't yet allow setting the
>> widths as percentages so you'd have to do this yourself.
>
>Is there any way to get the current frame so I can do frame.width
>instead? It would seem less fragile. You say SimpleDocTemplate doesn't
>create the frames until build time, but I'm not sure how to get the
>current frame (or PageTemplate) with
>BaseDocTemplate either. 

The frame sizes and positions are a user choice. The frames in a page
template are also a user choice. So you define your templates by adding
specified frames to them. A typical sequence might be

doc = BaseDocTemplate('dingo.pdf')
frame1 = Frame(self.leftMargin, self.bottomMargin, self.width,
                self.height, id='normal')
doc.addPageTemplates([PageTemplate(id='First',frames=frame1,
                pagesize=doc.pagesize)])

Here frame1 has a specified width and height which you set up front.

If you don't know in advance which frame you'll be using then there are
more complex ways to do things. A relatively simple way to do things
would be to create an intelligent table class.

from reportlab.platypus import Table
class SpecialTable(Table):
    def wrap(self,aW,aH):
        '''when called aW is the full width of the container'''
        n = len(self._argW)
        # equal column widths
        self._argW = self._colWidths = [float(aW)/n]*n
        # now really wrap
        Table.wrap(self,aW,aH)

Does this help?
-- 
Robin Becker