python - 使用 Reportlab 的多个页面 - Django

标签 python django reportlab

我在一个使用 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/

相关文章:

python - Beautiful Soup 过滤功能无法找到表的所有行

python - 将 django sorl-thumbnail 与 django form-utils 一起使用

python - 哪些工具用于编写 Python Web 应用程序(django)的验收测试?

django - 如何在 Django 模型的 EmailField 中添加多封电子邮件?

python - 您如何在reportlab Canvas 上取得进展?

python - dict_items 对象没有属性 'sort'

python - 区别 np.where 和这个解决方法?

python - 如果设置为 null,如何创建具有默认值的 Django 模型字段

python - GAE 模型 : How to list child nodes in parent

python - reportlab 使用 registerFont 添加字体