A multiline(paragraph) footer and header in reportlab

前端 未结 4 1765
一向
一向 2020-12-15 20:38

What is a best way to have a footer and header in reportlab, that not just a single line, that can be drawed with canvas.drawString in onPage function. Didn`t find a way to

4条回答
  •  悲哀的现实
    2020-12-15 20:54

    I know this is a bit old but I have encountered this problem and was able to solve it. When you have more than one page in your PDF and want to have the footer/header on every page, You have to use NextPageTemplate('template_id'). I am only writing the relevant code as the rest is shown in @jochen example above.

    In my case, I was using PageBreak() and it took me a while to understand why I was only getting the footer in the first page.

    from reportlab.platypus import Paragraph, PageBreak, PageTemplate, Frame, NextPageTemplate
    
    frame = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height, id='normal')
    template = PageTemplate(id='footer', onPage=footer, frames=[frame])
    
    
    # add a NextPageTemplate before a PageBreak to have the footer in the next page
    
    text.append(Paragraph('some text', style)),
    text.append(NextPageTemplate('footer')), # this will make the footer to be on the next page if exists
    text.append(PageBreak())
    doc.build(text)
    

提交回复
热议问题