简体   繁体   中英

How to add a new line in the txt field in fpdf cell

I'm trying to use fpdf to create a pdf file in python, but I have trouble in the "txt" filed in cell function, because I would like some new lines in the text. Here is what I do, but this doesn't work.

pdf = FPDF()
pdf.add_page()
pdf.set_xy(0, 0)
pdf.set_font('arial', 'B', 13.0)
text = 'Engagement Summary:' + "\n" + 'opened by: ' + str(shareURL.contact)
pdf.cell(ln=0, h=5.0, align='L', w=0, txt=text, border=0)
pdfName = request.user.username + ".pdf"
pdf.output(pdfName, 'F')

ALso, "\\n" doesn't work in the email I would like to send.

I'm not as familiar with the python version as I am with the PHP version, but assuming the library works the same, the issues is that cell does not allow for new lines within it. A cell is a essentially just a predefined rectangle with hard stops on the height and width entered.

What you'll need to use instead to add the new lines is: multi_cell multi cell will hard stop at the defined width, but the height will be dynamic based on content. Then you hard part becomes keeping track of positioning for other blocks.

Phython FPDF Multi Cell

pdf.cell(ln=1, h=5.0, align='L', w=0, txt=text, border=0)

如果你想要换行,ln 应该是 1 而不是 0

试试这个

pdf.multi_cell(0, 5, 'text' + '\n' +'test asdfaf')

您可以使用 pdf.multi_cell(ln=0, h=5.0, align='L', w=0, txt=text, border=0) 方法代替 pdf.cell(ln=0, h=5.0, align= 'L', w=0, txt=text, border=0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM