I'm using Reportlab to generate a four page PDF document. I have different methods generating each of the pages, but the second page is problematic. Unless I completely remove FrameBreaks from the code a new page is generated for every element in the method. Here's the code I have for the second page:
def page_two():
yld_header_height = 0.71 * inch
f_yld_header = Frame(doc.leftMargin, height - doc.topMargin - yld_header_height,
doc.width, yld_header_height,
leftPadding=0, rightPadding=0,
topPadding=0, bottomPadding=0,
id='yld_header')
yld_text_height = 3.59 * inch
yld_text_width = 1.75 * inch
f_yld_text = Frame(doc.leftMargin, height - doc.topMargin - yld_header_height - yld_text_height,
yld_text_width, yld_text_height,
leftPadding=0, rightPadding=0,
topPadding=0, bottomPadding=0,
id='yld_text')
yld_graph_height = yld_text_height
yld_graph_width = 4.95 * inch
f_yld_graph = Frame(doc.leftMargin + yld_text_width - 0 * inch,
height - doc.topMargin - yld_header_height - yld_text_height,
doc.width - yld_text_width, yld_graph_height,
leftPadding=0, rightPadding=0,
topPadding=0, bottomPadding=0,
id='yld_graph')
dash_buffer = 20
daly_header_height = 1.0 * inch
daly_cap_height = 4.15 * inch
f_daly_header = Frame(doc.leftMargin,
doc.bottomMargin + footer_height + daly_cap_height,
doc.width, daly_header_height,
leftPadding=0, rightPadding=0,
topPadding=0, bottomPadding=0,
id='daly_header')
daly_graph_width = 5.8 * inch
daly_cap_width = yld_text_width
f_daly_cap = Frame(doc.leftMargin,
doc.bottomMargin + footer_height,
daly_cap_width, daly_cap_height,
leftPadding=0, rightPadding=0,
topPadding=0, bottomPadding=0,
id='daly_cap')
f_daly_title = Frame(width - doc.rightMargin - daly_graph_width + 0.2 * inch,
doc.bottomMargin + footer_height + 7,
daly_graph_width, daly_cap_height,
leftPadding=0, rightPadding=0,
topPadding=0, bottomPadding=0,
id='daly_title')
f_daly_graph = Frame(width - doc.rightMargin - daly_graph_width,
doc.bottomMargin + footer_height,
daly_graph_width, daly_cap_height,
leftPadding=0, rightPadding=0,
topPadding=0, bottomPadding=0,
id='daly_graph')
post_header_space = 3
elements.append(Paragraph("TITLE)"
, styles['body_heading']))
elements.append(Spacer(1, post_header_space))
yld_para = "description"
elements.append(Paragraph(yld_para
, styles['body_text']))
# elements.append(FrameBreak())
elements.append(Spacer(1, 40))
elements.append(Paragraph("Lorem Ipsum", styles['justified']))
# elements.append(FrameBreak())
elements.append(Paragraph("Sub description"
, styles['fig_cap']))
# elements.append(FrameBreak())
elements.append(Paragraph("DISABILITY-ADJUSTED LIFE YEARS (DALYs)"
, styles['body_heading']))
elements.append(Spacer(1, post_header_space))
daly_para = "another paragraph"
elements.append(Paragraph(daly_para
, styles['body_text']))
# elements.append(FrameBreak())
elements.append(Spacer(1, 10))
elements.append(Paragraph("Yet another paragraph",
styles['justified']))
# elements.append(FrameBreak())
elements.append(Paragraph("Another sub paragraph", styles['fig_cap']))
# elements.append(FrameBreak())
elements.append(NextPageTemplate('ThirdPage'))
elements.append(PageBreak())
doc.addPageTemplates(PageTemplate(id='SecondPage', frames=[f_yld_header, f_yld_text, f_yld_graph, f_daly_header, f_daly_cap, f_daly_title, f_daly_graph], onPageEnd=foot_dash_2))
This is how the whole document is generated:
elements = []
page_one()
page_two()
page_three()
page_four()
doc.build(elements)
As the method is currently (FrameBreaks commented out), the elements are generated on the page, but all of the the padding and Frame Styling disappears. I've scanned this method extensively, and it's not significantly different than my other methods that generate whole pages just fine.
Is there anything in my code that could be causing the FrameBreak to act as a PageBreak or something similar?
This is not clear in the reportlab docs, but if when using simple doc template you need to add a NextPageTemplate in order to start new Page Object to contain all frames. So in this case you would just add elements.append(NextPageTemplate('SecondPage'))
to the end of the method that generates the first page.