Gio*_*Bot 2 python pdf newline reportlab
我需要一个新行,所以我可以在PFD中看到一种格式,我尝试添加页面宽度,但它没有工作,我使用/ n的其他东西,它不起作用.这是我的代码.我可以手动添加一个格式,因为我需要显示从数据库获取的信息,并在一个长行中获取信息.
def PdfReportView(request):
print id
# Create the HttpResponse object with the appropriate PDF headers.
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'filename="PatientReport.pdf"'
c = canvas.Canvas(response, pagesize=letter)
t = c.beginText()
t.setFont('Helvetica-Bold', 10)
t.setCharSpace(3)
t.setTextOrigin(50, 700)
t.textLine("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.")
c.drawText(t)
c.showPage()
c.save()
return response
Run Code Online (Sandbox Code Playgroud)
您可以使用textLines(),如果你有\n在你的文字输入:
t.textLines('''Lorem Ipsum is simply dummy text of the printing and
typesetting industry. Lorem Ipsum has been the industry's standard dummy text
ever since the 1500s, when an unknown printer took a galley of type and
scrambled it to make a type specimen book.''')
Run Code Online (Sandbox Code Playgroud)
如果你的文字是一行,你可以使用textwrap模块将它分成几行:
from textwrap import wrap
text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."
wraped_text = "\n".join(wrap(text, 80)) # 80 is line width
t.textLines(wraped_text)
Run Code Online (Sandbox Code Playgroud)