Reportlab new line in a long line

Reportlab new line in a long line

我需要一个新行,所以我可以在 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

如果您的文本输入中有 \n,您可以使用 textLines()

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.''')

如果您的文本是一行,您可以使用 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)