Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

dynamic cell height using FPDF in python

I am very new to python and I want to generate a PDF file using the FPDF library. with this library, I can create a table cell by cell so when I put two cells next to each other and one of them has more text than the other, the borders don't show correctly. I want something like an excel sheet so when one cell grows up the cell next to it also adjust its height. I wrote the following method to calculate the height of text and add ' ' to the shorter text so they become equal in height but it doesn't work well, especially when cell content crosses end of the page and goes to a new page.

def heightFix(firstText, secondText):
    global height

    # I try to use the length of text to find out how many lines it is
    numberOfLine = len(firstText)
    numberOfLine2 = len(secondText)

    # I considered every 59 character as one line
    if numberOfLine > 59:
        numberOfLine = math.floor(numberOfLine/59)
    else:
        numberOfLine = 0

    if numberOfLine2 > 59:
        numberOfLine2 = math.ceil(numberOfLine2/59)
    else:
        numberOfLine2 = 0
    
    # I count the number of enters in text, sometimes I have a text which is short in length but
    # it has many enter. in this case I will use number of enters as my indicator
    enters = secondText.count('
') + 1

    value = numberOfLine2 - numberOfLine

    value = int(value)

    if enters > value:
        newLine = ''

        for x in range(enters):
            newLine += '
'
        height = newLine
    elif value > 1:
        newLine = ''

        for x in range(value):
            newLine += '
'
        height = newLine
    else:
        height = ""  

and I use this method in the following way to create a row of values with the same height:

ybefore = pdf.get_y()
    
specialTrainingTitle = '2. I have special training in the following: '
specialTraining = 'this is a text that can be very long in multiple lines, I should also consider 
enters in this text'

heightFix(specialTrainingTitle, specialTraining)
pdf.multi_cell(effective_page_width/2, 7, specialTrainingTitle + height, 1, 0, 'L')
pdf.set_xy(effective_page_width/2 + pdf.l_margin, ybefore)
pdf.multi_cell(effective_page_width/2, 7, specialTraining, 1, 0, 'L')
pdf.ln(0.15)

following are screenshot of the form that I print

enter image description here enter image description here

question from:https://stackoverflow.com/questions/65907115/dynamic-cell-height-using-fpdf-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...