[英]FPDF writes to only one line
The variable "output" in the code below is a couple of paragraphs of text and I am trying to write it all to a pdf using FPDF. 以下代码中的变量“输出”是几段文字,我正尝试使用FPDF将其全部写为pdf。 But, it writes to only one line. 但是,它只写入一行。 How do I get it to include line breaks. 如何获取包含换行符的信息。 For example, the text has "\\n" in several locations of the text but its ignored. 例如,文本在文本的多个位置都有“ \\ n”,但被忽略。 Thank you. 谢谢。
dummytext = "line 1" + "\n"
dummytext += "line 2" + "\n"
dummytext += "line 3"
pdf = FPDF()
pdf.add_page()
pdf.set_xy(0,0)
pdf.set_font('arial', 'B', 13.0)
pdf.cell(ln=0, h=5.0, align='L', w=0, txt=dummytext, border=0)
pdf.output('testpdf.pdf', 'F')
The variable dummytext shows up in the pdf as: 变量dummytext在pdf中显示为:
line 1 line 2 line 3
Using multi_cell
will facilitate the new lines in FPDF
output. 使用multi_cell
将有助于FPDF
输出中的新行。
multi_cell
: This method allows printing text with line breaks.multi_cell
:此方法允许打印带有换行符的文本。
Details can be found in Documentation of multi_cell . 可以在multi_cell文档中找到详细信息。
code.py
: code.py
:
from fpdf import FPDF
dummytext = "line 1" + "\n"
dummytext += "line 2" + "\n"
dummytext += "line 3"
pdf = FPDF()
pdf.add_page()
pdf.set_xy(0,0)
pdf.set_font('arial', 'B', 13.0)
pdf.multi_cell(0, 5, dummytext)
pdf.output('testpdf.pdf', 'F')
Output: 输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.