Change font family and style

Using the method `pdf.set_font(font_family,font_style,font_size)` we can change font attributes at any moment through out the document.

# 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()

# Set font family to Times, regular style, size 10.0 pt
pdf.set_font('Times','',10.0) 
pdf.cell(1.0,0.0,'Hello World!')
pdf.ln(0.25)

# Set font family to Arial, 'B'old, size 14.0 pt
pdf.set_font('Arial','B',14.0)   
pdf.cell(1.0,0.0,'Hello World!')
pdf.ln(0.25)

# Set font family to Courier, 'I'talic, size 16.0 pt
pdf.set_font('Courier','I',16.0)   
pdf.cell(1.0,0.0,'Hello World!')
pdf.ln(0.25)

# Set font family to Symbol, regular text, size 24.0 pt
pdf.set_font('Symbol','',24.0)   
pdf.cell(1.0,0.0,'Hello World!')
pdf.ln(0.25)

# Add font from system. Font file path must be specified if it is not
# in an accesible path to pyFPDF.
# Second parameter is always empty for backward compatibility.
# uni=True enables Unicode
pdf.add_font('Comic Sans','','/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS.ttf', uni=True)

# Set font family to Comic Sans, 'U'nderlined, size 14.0 pt
pdf.set_font('Comic Sans','U',14.0)   
pdf.cell(1.0,0.0,'Hello World!')
pdf.ln(0.25)

# output content into a file ('F') named 'hello5.pdf'
pdf.output('hello5.pdf','F')

helloworld5

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