2015-09-28 42 views
1

enter image description hereReportLab的中長線新線

我需要一個新的生產線,所以我可以看到在PFD的格式,我嘗試做添加頁面寬度,但它沒工作,我使用其他的事情與/ n,它並沒有工作。這是我的代碼。 我可以添加一個格式manualy因爲我需要顯示的信息,從數據庫獲取,我得到的信息在一個長長的一行。

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 
+0

你可能想看看'textwrap'庫(我想那就是它所謂的...) –

回答

3

您可以使用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.''') 

如果你的文本是一條線,你可以使用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) 
+0

我沒有任何\ n那我的問題我從我的數據庫中獲取信息se並且是一條很長的線,沒有任何\ n – GioBot

+0

結帳我編輯的答案。 – ahmed

+0

謝謝,這是一個完美的解決方案。 – GioBot