Two different pages with reportlab SimpleDocTemplate and Django

Braulio Soncco picture Braulio Soncco · Mar 4, 2015 · Viewed 8.7k times · Source

I'm using django and generating reports following this example, I need to generate a last page but without headers or footers and different content.

I'm trying to do this:

def print_example(self):
    buffer = self.buffer
    doc = SimpleDocTemplate(buffer,
                            rightMargin=72,
                            leftMargin=72,
                            topMargin=72,
                            bottomMargin=72,
                            pagesize=self.pagesize)

    elements = []
    elements.append(Paragraph('Content for all pages'), my_custom_style)
    # ...

    doc.build(elements, onFirstPage=self._header_footer, onLaterPages=self._header_footer,
                  canvasmaker=NumberedCanvas)

    doc2 = SimpleDocTemplate(buffer,
                        rightMargin=72,
                        leftMargin=72,
                        topMargin=72,
                        bottomMargin=72,
                        pagesize=self.pagesize)

    elements2 = []
    elements2.append(Paragraph('Content for the last page only'), my_custom_style)

    doc2.build(elements2, canvasmaker=NumberedCanvas)


    # Get the value of the BytesIO buffer and write it to the response.
    pdf = buffer.getvalue()
    buffer.close()
    return pdf

Then only the last content appears and previous content dissapears.

How can I generate the last page with different content?

Answer

Razzildinho picture Razzildinho · Mar 13, 2015

I don't think it's possible using SimpleDocTemplate but you can achieve this by using BaseDocTemplate and defining your own templates.

Basic example

from reportlab.platypus import PageTemplate, BaseDocTemplate, NextPageTemplate, PageBreak

def headerFooterLayout(canvas, doc):
    canvas.saveState()
    canvas.setPageSize(self.pagesize)
    # add header/footer
    canvas.restoreState()

def emptyLayout(canvas, doc):
    canvas.saveState()
    canvas.setPageSize(self.pagesize)
    canvas.restoreState()

pHeight, pWidth = self.pagesize
myFrame = Frame(0, 0, pHeight, pWidth, id='myFrame')

headerFooterTemplate = PageTemplate(id='headerFooterTemplate',
                                    frames=[myFrame],
                                    onPage=headerFooterLayout)

emptyTemplate = PageTemplate(id='emptyTemplate',
                             frames=[myFrame],
                             onPage=emptyLayout)

elements = []
elements.append(Paragraph('blah', style))
elements.append(NextPageTemplate('emptyTemplate'))
elements.append(PageBreak())
elements.append(Paragraph('last page', style))

doc = BaseDocTemplate(buffer,
                      rightMargin=72,
                      leftMargin=72,
                      topMargin=72,
                      bottomMargin=72)

doc.addPageTemplates([headerFooterTemplate, emptyTemplate])

doc.build(elements)

It's been quite a while since I used this so there may well be some issues but comment if something doesn't work.

This is all in the user guide but can be hard to find what you're looking for.