Coloring multicell blocks

It is posible to paint the background of a multi_cell block by allowing filling each cell with a background color. However, there is an annoying padding space between cells that cannot (it seems) be removed easily. We will be using the same adjacent `multi_cell` code as before, but now with some `pdf.set_fill_color()` commands.

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

# Long meaningless piece of text
loremipsum_1 = """Lorem ipsum dolor sit amet, vel ne quando dissentias. Ne \
his oporteat expetendis. Ei tantas explicari quo, sea vidit minimum menandri \
ea. His case errem dicam ex, mel eruditi tibique delicatissimi ut. At mea wisi \
dolorum contentiones, in malis vitae viderer mel.

loremipsum_2 = """Vis at dolores ocurreret splendide. Noster dolorum repudiare \
vis ei, te augue summo vis. An vim quas torquatos, electram posidonium eam ea, \
eros blandit ea vel. Reque summo assueverit an sit. Sed nibh conceptam cu, pro \
in graeci ancillae constituto, eam eu oratio soleat instructior. No deleniti \
quaerendum vim, assum saepe munere ea vis, te tale tempor sit. An sed debet ocurreret \
adversarium, ne enim docendi mandamus sea.

effective_page_width = pdf.w - 2*pdf.l_margin

# Set background color light gray, text 'J'ustified and allow filling 
# the cell (fill=1)
pdf.set_fill_color(229, 229, 229)
pdf.multi_cell(effective_page_width, 0.15, loremipsum_2, fill=1, align='J')

# First save the y coordinate just before rendering the first multi_cell
ybefore = pdf.get_y()

# Set background color some light blue, centered text and allow filling 
# the cell (fill=1)
pdf.set_fill_color(173, 216, 230)
pdf.multi_cell(effective_page_width/2, 0.15, loremipsum_1, fill=1, align='C')

# Set background color some light red, right justified and allow filling 
# the cell (fill=1)
pdf.set_fill_color(255, 192, 203)

pdf.set_xy(effective_page_width/2 + pdf.l_margin, ybefore)
pdf.multi_cell(effective_page_width/2, 0.15, loremipsum_2, fill=1, align='R')




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