0

I am trying to generate a PDF with reportlab that has headers and footers starting on the second page.

I read the threads: Add page break to Reportlab Canvas object and Non-numbered pages in ReportLab

Using functions from these threads I can sucessfully close the title page with canvas.showPage() and can sucessfully start headers/ footers using doc.build(Story, onFirstPage=myFirstPage, onLaterPages=myLaterPages).

However, when I close the canvas.showPage(), my headers and footers begin on page THREE, not page TWO.

CODE:

def firstPage(canvas,doc):
    canvas.setFont('Times-Bold',40)
    canvas.drawString(inch, pageHeight-(3*inch), 'docTitle1') #w,h,txt
    canvas.drawString(2*inch, pageHeight-(4*inch), 'docTitle2')
    canvas.showPage() #force pagebreak

def laterPages(canvas, doc):
    canvas.saveState()
    canvas.setFont('Times-Roman',9)
    canvas.setStrokeColorRGB(2,2,2) #169,169,169)

    canvas.drawString(0.5*inch, pageHeight-(0.75*inch), 'header1')
    canvas.drawString(pageWidth-inch, pageHeight-(0.75*inch), 'header2')
    canvas.drawCentredString(pageWidth/2, 0.65*inch, 'footer1')
    canvas.drawCentredString(pageWidth/2, 0.5*inch, 'footer2')
    canvas.drawString(pageWidth-inch, 0.6*inch, `doc.page`) #page number
    canvas.restoreState()

def build_pdf(elements, doc_name):
    doc = SimpleDocTemplate(doc_name)
    doc.build(elements, onFirstPage=firstPage, onLaterPages=laterPages)

elements = [title, table1, spacer]
doc_name = 'myPDF.pdf'

build_pdf(elements, doc_name)

How can i stop drawing on the first page and still have onLaterPages begin on page TWO?

Community
  • 1
  • 1
dharol
  • 151
  • 2
  • 17
  • I think onLaterPages is meant to be used with flowables that automagically flow to subsequent pages... If you are manually adding page breaks I think you are better off just making a AddHeader / addFooter method and calling it after showPage – Joran Beasley Aug 06 '12 at 16:08

1 Answers1

0

Try:

doc.append(PageBreak())

for closing a page and opening the next one.

f p
  • 3,165
  • 1
  • 27
  • 35
  • ended up adding Title and PageBreak() to elements outside of the firstPage function. Working!! (btw for newbies, from reportlab.platypus.flowables import PageBreak). thx – dharol Aug 06 '12 at 16:44