Using the effective page width to distribute content evenly

The effective page width is the area between the left and right margin of the document. If those margins are 1.0 inch wide and the paper format is letter (8.5×11 in) with portrait orientation then the effective page width (or epw) is 8.5 – 1.0 – 1.0 = 6.5 inches. That is the area available to write or draw.

Of course we have the entire page surface at our disposition, but it is within that region that users expect the content.

# 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.

# Text in cell 2.5 inches wide, automatic height (0.0)
# starting at left margin
pdf.cell(2.5,0.0,'Hello World!')

# Let's put 5 more of this so text goes beyond page margins. Ugly result.
# To avoid this you must always be aware of your page dimensions.
for i in range(5):
    pdf.cell(2.5,0.0,'Hello World!')


# Let us use the page dimensions to better position those cells.
# The full page width is stored in pdf.w and the full height in pdf.h
# We will substract from pdf.w both left and right margins. Here we are
# infering both are the same dimension.

effective_page_width = pdf.w - 2*pdf.l_margin

pdf.cell(1.0, 0.0, 'Using effective page width of %s inches to adjust cells \
width to make them fit inside page dimensions' % effective_page_width, 'U')


# Using page width we will adjust cell width to make them all fit
# inside the page. The length of the text will determine if this
# will work or not

for i in range(5):
    pdf.cell(effective_page_width/5.0,0.0,'Hello World!')
# output content into a file ('F') named 'hello08.pdf'



Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s