我是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)
谢谢