2

关于使用 django 中的报告实验室生成的 pdf,我有 3 个问题,我似乎无法找到一个好的答案。django 站点上的文档仅涵盖如何生成 pdf 以供下载。但是如何将生成的 pdf 附加到电子邮件中并发送而不是下载呢?如何将 pdf 保存到服务器上的目录而不是下载它?如何在浏览器窗口中显示 pdf 而不是下载它?只需使用 django 文档示例:

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

def some_view(request):
    # Create the HttpResponse object with the appropriate PDF headers.
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'

    # Create the PDF object, using the response object as its "file."
    p = canvas.Canvas(response)

    # Draw things on the PDF. Here's where the PDF generation happens.
    # See the ReportLab documentation for the full list of functionality.
    p.drawString(100, 100, "Hello world.")

    # Close the PDF object cleanly, and we're done.
    p.showPage()
    p.save()
    return response
4

2 回答 2

3

要在您的浏览器上显示 de PDF 而不是下载它,您只需从

response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'

你会有这样的东西

response['Content-Disposition'] = 'filename="somefilename.pdf"'
于 2014-04-15T21:47:24.107 回答
1

您可以将常规文件名传递给 Canvas() 而不是类似文件的对象。您通常应该将页面大小作为下一个参数传入 - 默认为 A4。

有关所有详细信息,请从 ReportLab 下载用户指南和参考指南:

http://www.reportlab.com/docs/reportlab-userguide.pdf

见第 10 页。

于 2013-10-27T03:34:43.370 回答