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
pdf=FPDF(format='letter',unit='in')
# Add new page. Without this you cannot create the document.
pdf.add_page()

# 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.
pdf.ln(0.15)

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

pdf.output('margins.pdf','F')

pyfpdf margins

Advertisements

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 )

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