ReportLab创建带有页眉和页脚的多页表

2024-06-02 18:30:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图做一个简单的报告,它有一个页眉/页脚,中间有一行。p>

我是ReportLab的新手,我从WeasyPrint切换到ReportLab的原因是我希望引擎能够很好地处理分页

下面是我的代码:

    def print_users(self):
        buffer = self.buffer
        doc = BaseDocTemplate(buffer,
                                rightMargin=20,
                                leftMargin=20,
                                topMargin=20,
                                bottomMargin=20,
                                pagesize=landscape(self.pagesize))
        frame = Frame(
            doc.leftMargin,
            doc.bottomMargin,
            doc.width,
            doc.height - inch * 0.5,
            id='normal',
            showBoundary=1)

        template = PageTemplate(id='all_pages', frames=frame, onPage=self._header_footer)
        doc.addPageTemplates([template])


        styles=getSampleStyleSheet()
        # Our container for 'Flowable' objects
        elements = []
        elements.append(Spacer(1, 3*units.cm))
        title = self.event.name
        elements.append(Paragraph(self.event.name, styles["Normal"]))
        elements.append(Spacer(1, 1*units.cm))

        data = []
        titles = ['First name', 'Last name', 'Position', 'Institution']

        data.append(titles)
        for invitation in self.invitations:
            line = []
            line.append(invitation.person.firstname)
            line.append(invitation.person.lastname)
            line.append(invitation.person.position)
            line.append(invitation.institution.name)

            data.append(line)

        t=Table(data)
        t.setStyle(TableStyle(
                               [('LINEBELOW',(0,0),(-1,-1),1, colors.gray)]))

        table_story = [t]
        elements.append(t)
        # t_keep = KeepInFrame(0, 0, table_story, hAlign='CENTER', vAlign='MIDDLE', fakeWidth=False)
        # write the document to disk
        # Draw things on the PDF. Here's where the PDF generation happens.
        # See the ReportLab documentation for the full list of functionality.
        # elements.append(t_keep)

        doc.build(elements)

页眉/页脚显示良好,表格也显示良好,但当我们转到第二页时,表格将覆盖页眉

我试图添加一个带有KeepInFrame的框架,但是表格变得非常小,无法在一个页面中放入框架

这似乎是一个简单的任务,但我找不到一个简单的方法来考虑所有页面上的标题。(或者我采取了错误的方法?)


Tags: thenameselffordatadocbufferline