我使用Reportlab生成一个四页的PDF文档。我有不同的方法生成每个页面,但第二页是有问题的。除非我从代码中完全删除FrameBreaks,否则会为方法中的每个元素生成一个新页面。这是我第二页的代码:
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))
这就是整个文档的生成方式:
elements = []
page_one()
page_two()
page_three()
page_four()
doc.build(elements)
由于该方法是当前的(FrameBreaks已注释掉),因此在页面上生成元素,但所有填充和帧样式都会消失。 我已经广泛地扫描了这种方法,并且与我生成整页的其他方法没有太大的区别。
我的代码中是否有任何内容可能导致FrameBreak充当PageBreak或类似的东西?
答案 0 :(得分:0)
这在reportlab文档中并不清楚,但是如果使用简单的doc模板,则需要添加NextPageTemplate以启动新的Page Object以包含所有帧。因此,在这种情况下,您只需将elements.append(NextPageTemplate('SecondPage'))
添加到生成第一页的方法的末尾。