Tip: Check if new page will be added

To check in pyfpdf if a new page will be added after a cell of text, compare the current vertical position of the document to the “page_break_trigger” property.

# Import FPDF class
from fpdf import FPDF

# Create instance of FPDF class
# Letter size paper, use inches as unit of measure
pdf=FPDF(format='letter', unit='in')

# Add new page. Without this you cannot create the document.
pdf.add_page()

# Remember to always put one of these at least once.
pdf.set_font('Times','',10.0) 

# Effective page width, or just epw
epw = pdf.w - 2*pdf.l_margin

# You should know the cell text height in the document's unit

text_height = 0.15 # inches in this case

for i in xrange(100):
    if (pdf.y + text_height > pdf.page_break_trigger):
        # If you write the cell you will jump
        # to a new page

        pdf.cell(2.0, text_height, "I am in a new page")
    else:
        pdf.cell(2.0, text_height, "My text")
    
    pdf.ln(0.25)

pdf.output('detect-new-page-1.pdf','F')