django reportlab 将pdf附加到电子邮件,将pdf保存到服务器,在浏览器中显示pdf

RuS*_*uSs 2 django pdf-generation reportlab

我有 3 个关于使用 django 中的报告实验室生成的 pdf 的问题,我似乎找不到一个好的答案。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
Run Code Online (Sandbox Code Playgroud)

Car*_*ile 5

要在浏览器上显示 PDF 文件而不是下载它,您只需从中删除单词附件即可

response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
Run Code Online (Sandbox Code Playgroud)

你会有这样的东西

response['Content-Disposition'] = 'filename="somefilename.pdf"'
Run Code Online (Sandbox Code Playgroud)