我在一个使用 Django 的站点中工作,并使用 Repotlab 打印一个 .pdf 文件。
现在,我希望文件有多个页面,我该怎么做?
我的代码:
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
提前致谢。
最佳答案
showPage()
,尽管它的名字令人困惑,但实际上会结束当前页面,所以你在调用它后在 Canvas 上绘制的任何东西都会进入下一页。
在您的示例中,您可以在每个 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
关于python - 使用 Reportlab 的多个页面 - Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18664226/