如何在ReportLab platypus中使用BaseDocTemplate渲染多个页面?

时间:2013-09-25 10:34:00

标签: python pdf-generation reportlab platypus

我是ReportLab的新手并尝试使用Platypus生成PDF,其中每个部分都有不同的页面模板(以及不同的页眉和页脚)。如何在不使用SimpleDocTemplate的情况下使用BaseDocTemplate执行此操作?

我正在尝试下面的代码但我无法获取要显示的帧的内容,除了我直接绘制到画布的第一页。为了简洁起见,我删除了每个PageTemplate的额外格式和页眉和页脚代码。

from reportlab.platypus import (BaseDocTemplate, Paragraph, Spacer,
                            PageBreak, Frame, PageTemplate, NextPageTemplate)
from reportlab.pdfgen import canvas
from reportlab.lib import pagesizes, units, styles, enums

class Report(object):
    def __init__(self, stream, sections):
        self.stream = stream
        self.sections = sections
        w, h = pagesizes.A4
        self._width = w
        self._height = h
        self._story = []
        self._doc = None
        self._canvas = canvas.Canvas(self.stream)
        self._stylesheet = styles.getSampleStyleSheet()

    def generate(self):
        '''Generate the report'''
        self._doc = BaseDocTemplate(self.stream,
                                    pagesize=(self._width, self._height),
                                    showBoundary=True
                                    )
        # Start with the coverpage, then create a new page for each section.
        self.coverpage()
        for i, p in enumerate(self.sections):
            self.render_section(i, p)
        self._doc.build(self._story)
        self._canvas.save()

    def coverpage(self):
        '''Draw the cover page'''
        frame = Frame(0, 0, self._width, self._height)
        self._doc.addPageTemplates(PageTemplate(id='cover', frames=[frame]))
        self._story.append(PageBreak())
        # The cover page just has some drawing on the canvas.
        self._canvas.saveState()
        self._canvas.setFont('Helvetica', 16)
        self._canvas.drawCentredString(self._width / 2.0, self._height - 108,
                                       "This is the first page")
        self._canvas.restoreState()

    def render_section(self, num, text):
        '''Put stuff on the canvas that belong to this section.'''
        frame = Frame(0, 0, self._width, self._height, showBoundary=1)
        self._doc.addPageTemplates(PageTemplate(id='section-%d' % num,
                                                frames=[frame]))
        h1 = self._stylesheet['Heading1']
        h1.alignment = enums.TA_CENTER
        frames = [NextPageTemplate('section-%d' % num),
                  Paragraph(self.sections[num], h1),
                  Spacer(1, units.inch * 0.2),
                  PageBreak()]
        self._story.extend(frames)


if __name__ == '__main__':
    Report('report.pdf', "Why is this not showing?".split(" ")).generate()

1 个答案:

答案 0 :(得分:3)

你的答案是here。给定链接上的示例代码非常清楚如何使用BaseDocTemplate显示多个帧。