Django、ReportLab PDF 生成附加在电子邮件中
使用 Django 和 ReportLab 生成 PDF 并将其附加到电子邮件的最佳方法是什么?
我正在使用 SimpleDocTemplate,并且可以将生成的 PDF 附加到我的 HttpResponse - 这很棒,但我无法找到如何将相同的附件准确添加到电子邮件中:
# Create the HttpResponse object with the appropriate PDF headers.
response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=invoice.pdf'
doc = SimpleDocTemplate(response, pagesize=letter)
Document = []
...通过将表格附加到文档...
doc.build(Document)
email = EmailMessage('Hello', 'Body', '[email protected]', ['[email protected]'])
email.attach('invoice.pdf', ???, 'application/pdf')
email.send()
我只是不确定如何将我的 pdfdocument 翻译为 blob,以便 email.attach 可以接受它并且 email.send 可以发送它。
有什么想法吗?
What's the best way to use Django and ReportLab to generate PDFs and attach them to an email message?
I'm using a SimpleDocTemplate and can attach the generated PDF to my HttpResponse - which is great, but I'm having trouble finding out how to exactly add that same attachment to an email:
# Create the HttpResponse object with the appropriate PDF headers.
response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=invoice.pdf'
doc = SimpleDocTemplate(response, pagesize=letter)
Document = []
... make my pdf by appending tables to the Document...
doc.build(Document)
email = EmailMessage('Hello', 'Body', '[email protected]', ['[email protected]'])
email.attach('invoice.pdf', ???, 'application/pdf')
email.send()
I'm just not sure how to translate my pdfdocument as a blob so that email.attach can accept it and email.send can send it.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 ReportLab
效果非常好!
Using ReportLab
Works perfectly!!
好的 - 我根据将一些事情拼凑在一起找到了它 -
首先 - 我的要求:
- 我只想在内存中创建 PDF - 我不希望文件闲置,因为它们占用空间,而且我不希望敏感数据在服务器上不受保护地闲置。
所以 - 我选择了 ReportLab 和 Platypus 功能来生成我的文档。我现在已经投入了足够的时间,这很容易。因此,这是我的方法,允许我使用 ReportLab 中的 DocTempates,允许我使用 Django 的电子邮件功能来发送电子邮件。
我是这样做的:
从网络生成转向电子邮件生成,我的问题是获取可以“附加”到电子邮件的正确对象。创建一个缓冲区,然后从缓冲区中获取数据对我来说是这样......
OK - I figured it out based on piecing a few things together -
First off - my requirements:
- I only wanted to create the PDFs in memory - I don't want the files hanging around, as they take up space, and I don't want what might be sensitive data hanging around unprotected on the server.
So - I picked ReportLab and Platypus functionality for generating my documents. I've invested enough time into it now, that's it's easy. So here's my approach that lets me use the DocTempates in ReportLab, allows me to use Django's email capabilities to send emails.
Here's how I'm doing it:
My issue from moving from web generation to email generation was getting the right object that could be "attached" to an email. Creating a buffer, then grabbing the data off the buffer did it for me...
我看不到你的 blob 在哪里渲染,所以我无法建议你如何导入它。我使用 Pisa 和 StringIO 获得了很好的结果:
也就是说,如果您的 PDF 作为独立且持久的文档存在于您的文件系统上,您难道不能:
I don't see where your blob is rendered, so I can't advise you on how to import it. I've gotten great results using Pisa and StringIO:
That said, if your PDF exists as an independent and persistent document on your filesystem, couldn't you just: