I want to create PDF file and write text that I read from file. The text in the file looks like:
Hello World. This is test text.
ASDFASDFAAAAAAAAAAAAAAAAAAAAAAA
Hello World. This is test text.
ASDFASDFAAAAAAAAAAAAAAAAAAAAAAA
Hello World. This is test text.
ASDFASDFAAAAAAAAAAAAAAAAAAAAAAA
I created PDF with FPDF library. The code:
pdf = FPDF()
pdf.add_page()
pdf.set_font('Arial', 'B', 14)
pdf.text(0, 0 , text)
pdf.output(fileNameBase + ".pdf", "F")
The issue is the text is rendered in one line. So the text is not fully shown. I want to write text with the same format in text file. How can I create PDF file with the same format in the text file?
Use multi_cell() method instead of
text()method.multi_cellorcellare directly recommended by the text() method documentation:To test your program I have created a text file called
file.txtwhich contains your example text:Changing your code to use
multi_cell()and read the text message to write to the PDF file, from the text filefile.txt, your program becomes:By the execution of the function
main()I get a PDF file calledquestion_so.pdfwith the content showed by the following image: