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

Viewed 12166

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.

4 Answers

try this

pdf.multi_cell(0, 5, 'text' + '\n' +'test asdfaf')
pdf.cell(ln=1, h=5.0, align='L', w=0, txt=text, border=0)

ln should be 1 instead of 0 if you want a line break

you can use pdf.multi_cell(ln=0, h=5.0, align='L', w=0, txt=text, border=0) method instead of pdf.cell(ln=0, h=5.0, align='L', w=0, txt=text, border=0)

Related