Putting two adjacent multicell blocks

There are times when we need to render a multi-column document with multi-line content. Here is an idea on how to do that with `multi_cell`, although it may require lots of tweaking to make it work for each use case. This example shows again the importance of take into account the dimensions of the page, including its margins.

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

# Remember to always put one of these at least once.
pdf.set_font('Times','',10.0) 

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

# Even more meaningless text
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

# Some text full page width for position reference

pdf.multi_cell(effective_page_width, 0.15, loremipsum_2)
pdf.ln(0.5)

# First save the y coordinate just before rendering the first multi_cell
ybefore = pdf.get_y()
pdf.multi_cell(effective_page_width/2, 0.15, loremipsum_1)

# Now use ybefore to position the cursor at the same level of the first 
# multi_cell.
# Notice the use of "effective_page_width/2 + pdf.l_margin" as x to position 
# the cursor horizontally just beyond the first multi_cell

pdf.set_xy(effective_page_width/2 + pdf.l_margin, ybefore)
pdf.multi_cell(effective_page_width/2, 0.15, loremipsum_2)
pdf.ln(0.5)

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

multicell_adjacent

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