[reportlab-users] Adjust Frame Heights at Runtime?

Steve Schwarz steve at agilitynerd.com
Sun Mar 16 12:55:33 EDT 2008


Hi,
I very new to reportlab and I'm trying to solve a problem with my
layout and I'm wondering what the best way to solve it is.

I'm creating a script to accept input of two tables and some text. I'd
like the two tables to be arranged side by side (a two column layout)
with the text filling the remainder of the page immediately after the
tables and spanning the entire width of the page.

My initial approach is to use three frames with two side by side at
the top and the third frame filling the remainder of the page below
the two frames. But since I don't know the size of the tables until
they are rendered I don't know how to set the height of the bottom
Frame so it will fill the entire space below the tables.

I'm wondering if:
1. I'm not thinking within the framework and there is a better solution
2. I can obtain the remaining page height after rendering the final
top frame and set the size of the bottom frame (and if so how?)
3. I need to write my own Frame subclass that handles this layout.

Here is my current code that simply defines the bottom Frame to take
1/2 the page to give you an idea of my layout:

from reportlab.pdfgen import canvas
from reportlab.platypus import BaseDocTemplate, Frame, Paragraph,
NextPageTemplate, PageBreak, PageTemplate
from reportlab.platypus import KeepInFrame, FrameBreak, Flowable
from reportlab.platypus.tables import Table
from reportlab.lib.units import inch
from reportlab.lib import colors
from reportlab.lib.styles import getSampleStyleSheet

styles=getSampleStyleSheet()
Elements=[]
PAGESIZE = [ 8.5 * inch, 11.0 * inch ]
SPACER = 6
doc = BaseDocTemplate('book3.pdf', showBoundary=0,
pagesize = PAGESIZE,
topMargin = 0.5*inch,
bottomMargin = 0.75*inch,
leftMargin = 0.5*inch,
rightMargin = 0.5*inch,
)

class BlankLine(Flowable):
'''Class to draw a single line just above the bottom of the height'''
def __init__(self, width, height, lineWidth=0.01*inch, color=colors.black):
Flowable.__init__(self)
self.width = width
self.height = height
self.lineWidth = lineWidth
self.color = color

def draw(self):
self.canv.saveState()
self.canv.setLineWidth(self.lineWidth)
self.canv.setStrokeColor(self.color)
self.canv.line(0, 0, self.width, 0)
self.canv.restoreState()


class RuledFlowable(KeepInFrame):
'''Create a paragraph of lines that fills the specified area and
is truncated to fit the page.'''
LINE_HEIGHT = 0.21 * inch
def __init__(self, width, height):
content = [BlankLine(width, RuledFlowable.LINE_HEIGHT,
color=colors.gray) for x in range(int(height /
RuledFlowable.LINE_HEIGHT))]
KeepInFrame.__init__(self, width, height, content=content,
mode='truncate')

# Three Column
# Top is two column taking half the page.
# Bottom is full width
threeColumnLeftFrame = Frame(doc.leftMargin,
doc.bottomMargin+doc.height/2, doc.width/2-SPACER, doc.height/2,
leftPadding=0, bottomPadding=0,
rightPadding=0, topPadding=0,
id='left')
threeColumnRightFrame = Frame(doc.leftMargin+doc.width/2+SPACER,
doc.bottomMargin+doc.height/2, doc.width/2-SPACER, doc.height/2,
leftPadding=0, bottomPadding=0,
rightPadding=0, topPadding=0,
id='right')
threeColumnBottomFrame = Frame(doc.leftMargin, doc.bottomMargin,
doc.width, doc.height/2-SPACER,
leftPadding=0, bottomPadding=0,
rightPadding=0, topPadding=0,
id='bottom')

doc.addPageTemplates(PageTemplate(id='3 Column',
frames=[threeColumnLeftFrame, threeColumnRightFrame,
threeColumnBottomFrame]))

data = [['','','','','','','','','','']] * 10
table = Table(data, colWidths=[inch, inch, inch, inch, inch, inch,
inch, inch, inch, inch],
rowHeights=[inch, inch, inch, inch, inch, inch, inch,
inch, inch, inch],
style=[('GRID',(0,0),(9,9),1, colors.black)]
)
Elements.append(KeepInFrame(doc.width/2-SPACER, doc.height/2, [table],
mode="shrink"))
Elements.append(FrameBreak())
Elements.append(KeepInFrame(doc.width/2-SPACER, doc.height/2, [table],
mode="shrink"))
Elements.append(FrameBreak())
Elements.append(RuledFlowable(doc.width, doc.height))

doc.build(Elements)

Any suggestions would be greatly appreciated,
Best Regards,
Steve


More information about the reportlab-users mailing list