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.

# Remember to always put one of these at least once.

# 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")
        pdf.cell(2.0, text_height, "My text")



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s