pythonpython-3.xpdfpyfpdf

How to get MultiCells in Pyfpdf Side by side?


I am making a table of about 10 cells with headings in them. They will not fit accross the page unless I use multi_cell option. However I cant figure out How to get a multi_cell side by side. When I make a new one it autmatically goes to the next line

from fpdf import FPDF
import webbrowser

pdf=FPDF()
pdf.add_page()
pdf.set_font('Arial','B',16)
pdf.multi_cell(40,10,'Hello World!,how are you today',1,0)

pdf.multi_cell(100,10,'This cell needs to beside the other',1,0)

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


webbrowser.open_new('tuto1.pdf')

Solution

  • You will have to keep track of x and y coordinates:

    from fpdf import FPDF
    import webbrowser
    
    pdf=FPDF()
    pdf.add_page()
    pdf.set_font('Arial','B',16)
    
    # Save top coordinate
    top = pdf.y
    
    # Calculate x position of next cell
    offset = pdf.x + 40
    
    pdf.multi_cell(40,10,'Hello World!,how are you today',1,0)
    
    # Reset y coordinate
    pdf.y = top
    
    # Move to computed offset
    pdf.x = offset 
    
    pdf.multi_cell(100,10,'This cell needs to beside the other',1,0)
    
    pdf.output('tuto1.pdf','F')
    
    webbrowser.open_new('tuto1.pdf')