![](/img/trans.png)
[英]Loop prints multiple lines (as intended) but only writes one line to the file
[英]FPDF writes to only one line
以下代码中的变量“输出”是几段文字,我正尝试使用FPDF将其全部写为pdf。 但是,它只写入一行。 如何获取包含换行符的信息。 例如,文本在文本的多个位置都有“ \\ n”,但被忽略。 谢谢。
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')
变量dummytext在pdf中显示为:
line 1 line 2 line 3
使用multi_cell
将有助于FPDF
输出中的新行。
multi_cell
:此方法允许打印带有换行符的文本。
可以在multi_cell文档中找到详细信息。
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')
输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.