2013-09-06 11 views
15

私はDjangoを使用しているサイトで作業していますが、Repotlabを使用して.pdfファイルを印刷しています。Reportlabを使用する複数のページ - Django

今、ファイルに複数のページがあるようにしたいのですが、どうすればいいですか?

マイコード:事前に

from reportlab.pdfgen import canvas 
from django.http import HttpResponse 

def Print_PDF(request): 
    response = HttpResponse(content_type='application/pdf') 
    response['Content-Disposition'] = 'attachment; filename="resume.pdf"' 

    p = canvas.Canvas(response) 

    p.drawString(100, 100, "Some text in first page.") 
    p.drawString(200, 100, "Some text in second page.") 
    p.drawString(300, 100, "Some text in third page") 

    p.showPage() 
    p.save() 
    return response 

感謝。

答えて

28

​​実際には現在のページが終了するので、それを呼び出した後にキャンバスに描画するものは次のページに移動します。

あなたの例では、それぞれのp.drawStringの後にp.showPage()を使用するだけで、すべてが自分のページに表示されます。

def Print_PDF(request): 
    response = HttpResponse(content_type='application/pdf') 
    response['Content-Disposition'] = 'attachment; filename="resume.pdf"' 

    p = canvas.Canvas(response) 

    p.drawString(100, 100, "Some text in first page.") 
    p.showPage() 

    p.drawString(200, 100, "Some text in second page.") 
    p.showPage() 

    p.drawString(300, 100, "Some text in third page") 
    p.showPage() 

    p.save() 
    return response 
+1

私はすでにそれを持っていましたが、私の評判には答えられませんでした。あなたの答えは**完璧です**、ありがとうございます。 – Andres

関連する問題