我是reportlab的初学者,当我构建我的pdf时,我遇到了一个奇怪的问题。 MyFirstPage模板会覆盖myLaterPages。
def myFirstPage(canv, doc):
canv.saveState()
canv.line(left_margin, top_margin, right_margin, top_margin)
canv.setFont('Times-Italic',12)
canv.drawString(right_margin - 100, top_margin + 10, "Report")
canv.drawImage('logo.jpg', left_margin, top_margin, width = 90, height= 30)
canv.setFont("Times-Bold", 36)
canv.drawCentredString(0.5 * A4[0], 7 * inch, "Rapport journalier d activite")
canv.line(left_margin, top_margin, right_margin, top_margin)
canv.line(left_margin, bottom_margin, right_margin, bottom_margin)
canv.drawCentredString(0.5*A4[0], 0.5 * inch,"Page %d" % canv.getPageNumber())
canv.restoreState()
def myLaterPages(canv, doc):
canv.saveState()
canv.line(left_margin, top_margin, right_margin, top_margin)
canv.setFont('Times-Italic',12)
canv.drawString(right_margin - 100, top_margin + 10, "Report")
canv.drawImage('logo.jpg', left_margin, top_margin, width = 90, height= 30)
canv.line(left_margin, top_margin, right_margin, top_margin)
canv.line(left_margin, bottom_margin, right_margin, bottom_margin)
canv.drawCentredString(0.5*A4[0], 0.5 * inch,"Page %d" % canv.getPageNumber())
canv.restoreState()
def create_pdf_report(pdfname = 'report.pdf'):
code = [200, 404]
app = ['webserver01, webserver02']
doc = SimpleDocTemplate(pdfname,rightMargin=5,leftMargin=5,topMargin=0,bottomMargin=5)
result = []
for a in app:
result.append(Spacer(1, 70))
result.append(create_table_website_ref(a))
result.append(create_pi_graphic(a))
result.append(Spacer(1, 30))
for i in code:
result.extend(create_table_graph_row(a, i))
result.append(Spacer(1, 30))
result.append(PageBreak())
doc.build(result, onFirstPage=myFirstPage , onLaterPages=myLaterPages)
由于
答案 0 :(得分:0)
之后
doc = SimpleDocTemplate(pdfname,rightMargin=5,leftMargin=5,topMargin=0,bottomMargin=5)
您必须使用frame
添加template
和doc
部分
例如
frame = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height, id='normal')
template = PageTemplate(frames=frame, onPage=header)
doc.addPageTemplates([template])
这将在渲染pdf时添加您的第一页。现在,要添加以后的页面模板插入
result.append(NextPageTemplate('header'))
在呈现第二页的内容之前。