i have a problem for adjust the height of cell depending on height of multicell, this multicell changes its height by the number of words and width.My code that I am testing is the following.
datos = ["hola","Esto es el texto que determina el tamaño"]
pdf = FPDF()
pdf.add_page()
pdf.set_font('Arial', 'B', 8)
line_height = pdf.font_size * 2.5
t = 0
lh_list = [] #list with proper line_height for each row #flag
a = []
#create lh_list of line_heights which size is equal to num rows of data
for row in datos:
x = row.split()
f = len(x)
a.append(f)
if f>1:
alto = line_height
lh_list.append(alto)
else:
lh_list.append(line_height)
print(lh_list)
print(a)
for j,row in enumerate(datos):
if a[j]>1:
pdf.multi_cell(40,lh_list[j],row,1,'J')
else:
pdf.cell(40,lh_list[j]*2,row,1,0,'J')
pdf.output('table_with_cells.pdf')
The result is: Result
and the desired result is: expected result
You use the split_only argument of multi_cell to calculate the needed lines per cell for the text. With this information you can properly calculate the exact position of each cell. Be aware that the max_line_height argument has to be understand as line height of each line of a cell. Meaning that a value of 3 will result in a cell with 3, 6 or 9 mm heigth if the content need 1-3 lines within the cell to be printed.
The borders of the cell needed to be drawn separately. In my example I only draw the horizontal lines. To understand why you have to do this, just change border=0 to border=1 in my example.