pyFPDF Default margins

When you create an empty PDF document with `pyFPDF` it has already default margins. The following code demonstrates how to get those margins and how to draw them.

# Import FPDF class
from fpdf import FPDF

# Create instance of FPDF class
# Add new page. Without this you cannot create the document.

# Get default margins
left = pdf.l_margin
right = pdf.r_margin
top = pdf.t_margin
bottom = pdf.b_margin

# Effective page width and height
epw = pdf.w - left - right
eph = pdf.h - top - bottom

# Draw margins for our viewing pleasure
pdf.rect(left, top, w=epw, h=eph)

# Draw margin sizes
# Need this line break, otherwise top line crosses text. Have not figure
# out why yet.

# Remember to put at least one of this font declarations when you are going
# to render text.  Not needed if not drawing text.
pdf.cell(0,0, 'Left: %s in, Right: %s in, Top: %s in, Bottom: %s in' % (left, \
right, top, bottom), align= 'C')


pyfpdf margins


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 )

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