Search code examples
pythonreportlabplatypus

Platypus - Using Multiple Frames in a PageTemplate


I'm working on writing some PDF generation code for a Django based website using ReportLab / Platypus to generate the PDF.

I've subclassed PageTemplate so I can have some consistent page trim and have included code to generate multi-column layouts for me to suit requirements. I've currently got showBoundary=1 on for debugging.

However, I'm only seeing the first frame boundary appearing when I render a two column layout. What might be going wrong?

class ReportPageTemplate(PageTemplate):
    def __init__(self, id='basic', columns=1, pagesize=A4, leftMargin=(2*cm), bottomMargin=(2.1*cm), colmargin=(0.5*cm)):
        (right, top) = pagesize
        right -= leftMargin
        top -= bottomMargin

        height = top - bottomMargin
        width = (right - leftMargin)
        # Subtract out blank space between columns
        colwidth = (width - ((columns - 1) * colmargin)) / columns

        frames = []
        for col in range(columns):
            left = leftMargin + (col * (colwidth + colmargin))
            frames.append(Frame(left, bottomMargin, colwidth, height, showBoundary=1))

        PageTemplate.__init__(self, id=id, frames=frames, pagesize=pagesize)

    def beforeDrawPage(self, canvas, doc):
        print self.id
        (width, height) = canvas._pagesize
        canvas.setLineWidth(0.2 * cm)
        canvas.line(0.5*cm, height - (2*cm), width - (0.5*cm), height - (2*cm))
        canvas.line(0.5*cm, (2*cm), width - (0.5*cm), (2*cm))

Solution

  • Ah, well I feel foolish.

    The second frame is only rendered if the first one fills up. For testing purposes I needed to include a FrameBreak object to force it to draw both columns.

    The code is actually already working.