0

I want to align cell and multi-cell in the same row.

i = 0
for col in row:
    if i == 0:
        self.multi_cell(col_widths[i], 6, col, 1, new_x=XPos.LMARGIN, new_y=YPos.NEXT, align=alignments[i], fill=True)
    else:
        self.cell(col_widths[i], 6, col, 1, new_x=XPos.LMARGIN, new_y=YPos.NEXT, align=alignments[i], fill=True)
    i+=1

I show the result by an image of the PDF created:

enter image description here

Thank you

frankfalse
  • 1,553
  • 1
  • 4
  • 17
Pypax
  • 83
  • 10

2 Answers2

0

Why not using multi_cell in both cases? Maybe just have a look to my answer I gave in another thread.

In short, build a table with multi_cell and make sure you align them central in vertical direction.

Kallisto
  • 1
  • 2
  • Link-only answers have risk of link going dead, if the post is deleted. Please consider providing summary/steps from link to solve the problem. – Azhar Khan Nov 14 '22 at 10:57
0

The following code place the second and the third cell in the same row of the multi_cell. The only thing to watch out for is:

After place the multicell you need to set the position for the second cell inside the PDF.

from fpdf import FPDF
import webbrowser

col_widths = [80, 20, 20]
row = ["hsdfadfafdgadsaghfsjafshfahsfafsafsasfajfsjafsjfasfa" \
       "jfsjafsjfajsfjafsjafjfajsfjafsjfajrtwerqyterqwtyrety" \
       "dhefhcjhwefjkwsfjkldjfvwsmfmreteygcdfchowfhiopwjhfop" \
       "ejwofjeowpfjowjofjcoewjfoewjfowjfojowepjfoejwfojewor",
       "08.2022",
       "0.02"]

def main():
    pdf=FPDF()
    pdf.add_page()
    pdf.set_font('Arial','',8)

    i = 0
    for col in row:
        if i == 0:
            # save the position before the multi_cell insertion
            y = pdf.get_y()
            x = pdf.get_x()
            pdf.multi_cell(w=col_widths[i], h=6, txt=col, border=1, align='L', fill=False)
            # after place the multicell you need to set the position inside the PDF
            pdf.set_y(y)
            pdf.set_x(x + col_widths[0])
        else:
            pdf.cell(w=col_widths[i], h=6, txt=col, border=1, align='R', fill=False)
        i += 1

    pdf.output('question_so.pdf','F')
    webbrowser.open_new('question_so.pdf')

main()

This is the PDF file created:

enter image description here


Note. To have an example for the setup of the correct position for a cell in a PDF file also see this post.

frankfalse
  • 1,553
  • 1
  • 4
  • 17