python - 通过电子邮件发送从 HTML 生成的 PDF:预期的类似字节的对象,而不是 HttpResponse
问题描述
我正在尝试通过电子邮件发送从 HTML 模板生成的 PDF。
我收到以下错误:expected bytes-like object, not HttpResponse
generate_pdf
:
def generate_pdf(template_src, context_dict={}):
template = get_template(template_src)
html = template.render(context_dict)
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
if not pdf.err:
return HttpResponse(result.getvalue(), content_type='application/pdf')
return None
view for pdf emailing
:
def pdfView(request):
data = {'test':'test',
'mylist': 'test'
}
pdf = generate_pdf('main/test.html', data)
msg = EmailMessage("title", "content", to=["email@email.com"])
msg.attach('my_pdf.pdf', pdf, 'application/pdf')
msg.content_subtype = "html"
msg.send()
return HttpResponse(pdf, content_type='application/pdf')
解决方案
您HttpResponse
在函数中返回 a 。
简单地改变return HttpResponse(result.getvalue(), content_type='application/pdf')
到return result.getvalue()
推荐阅读
- opencv - OpenCV 4.5 对应 OpenCV 2.4 cvCreateImageHeader 方法
- protocol-buffers - 使用 grpc-gateway 返回一个 json 映射值
- groovy - 如何使用命名参数并防止使用默认构造函数?
- java - 创建新文件时触发android
- visual-studio-code - VSCode:如何在上部选项卡上仔细检查时防止编辑器扩展
- python - 函数 add_time(time1,time2)
- javascript - 如何在节点 js 中找到本周的星期一?
- wpf - WPF 将 SelectedItem 绑定到另一个对象的属性
- javascript - 嵌套的 AngularJS 指令未从 Angular 组件获取表单
- uwp - UWP ApplicationData.Current.LocalFolder 在版本中为空