使用reportlab SimpleDocTemplate和Django的两个不同页面

时间:2022-10-04 19:19:11

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.

我正在使用django并在此示例后生成报告,我需要生成最后一页但没有页眉或页脚和不同的内容。

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?

如何生成包含不同内容的最后一页?

1 个解决方案

#1


4  

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

我不认为使用SimpleDocTemplate是可能的,但您可以通过使用BaseDocTemplate并定义自己的模板来实现这一点。

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.

这些都在用户指南中,但很难找到您要找的内容。

#1


4  

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

我不认为使用SimpleDocTemplate是可能的,但您可以通过使用BaseDocTemplate并定义自己的模板来实现这一点。

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.

这些都在用户指南中,但很难找到您要找的内容。