简体   繁体   中英

Two columns and footer with reportLab

I am trying to generate a document with two columns and footer. Currently I have the document split into two columns, However when I try to make paragraphs inside the footer it just follows the pattern of the column, if that make sense. It does not actually create a footer. I'm new to reportLab and don't have a good grasp of the library.

document = SimpleDocTemplate("test.pdf", pagesize=letter,rightMargin=72,leftMargin=72,topMargin=72,bottomMargin=18 ) 

#Two Columns
frame1 = Frame(document.leftMargin, document.bottomMargin, document.width/2-6, document.height, id='col1')
frame2 = Frame(document.leftMargin+document.width/2+6, document.bottomMargin, document.width/2-6, document.height, id='col2')

document.addPageTemplates([PageTemplate(id='TwoCol',frames=[frame1,frame2])])

story = []

ptext = '* Example blah blah'
story.append(Paragraph(ptext, styles["Normal"]))

ptext = '* Example blah blah'
story.append(Paragraph(ptext, styles["Normal"]))


footer = Frame(document.leftMargin, document.bottomMargin, document.width, document.height, id="footer")
document.addPageTemplates([PageTemplate(id='Footer',frames=[footer])])

ptext = '* Example blah blah'
story.append(Paragraph(ptext, styles["Normal"]))

ptext = '* Example blah blah'
story.append(Paragraph(ptext, styles["Normal"]))


document.build(story)

There is a possibility to add footer and header elements on the Page Template through the " onPage "

given your code line:

document.addPageTemplates([PageTemplate(id='TwoCol',frames=[frame1,frame2])])

you could eg do the following (pls note the onPage addition):

styles = getSampleStyleSheet()
styleN = styles['Normal']

def footer(canvas, doc):
    canvas.saveState()
    P = Paragraph("This is a multi-line footer.  It goes on every page.  " * 5, styleN)
    w, h = P.wrap(doc.width, doc.bottomMargin)
    P.drawOn(canvas, doc.leftMargin, h)
    canvas.restoreState()


document.addPageTemplates([PageTemplate(id='TwoCol', onPage=footer, frames=[frame1,frame2])])

See a similar question here: A multiline(paragraph) footer and header in reportlab

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM