在我的应用程序中,django
我正在尝试生成 pdf 并发送附有 pdf 的邮件。在我的邮件中,view
我收到以下错误
'HttpResponse' object has no attribute 'getvalue'
我xhtml2pdf
用来生成 pdf 电子邮件视图
def invoice_email_view(request):
user = UserProfile.objects.get(user__id=request.user.id)
email = user.user.email
body = '''Dear User, \n Reciept of payment.
\n \n OTP : %s \n \n Thanks,\n'''
email = EmailMessage('Course Ware OTP', body, to=[email])
file_to_be_sent = invoice_view(request).getvalue()
email.attach("invoice.pdf", file_to_be_sent, "application/pdf")
email.send()
return HttpResponse("success")
pdf查看
def invoice_view(request, *args, **kwargs):
template = get_template('reciept_acknowledment.html')
context = Context({ })
html = template.render(context)
pdf = render_to_pdf('reciept_acknowledment.html', context)
if pdf:
response = HttpResponse(pdf, content_type='application/pdf')
filename = "Invoice_%s.pdf" % ("12341231")
content = "inline; filename='%s'" % (filename)
download = request.GET.get("download")
if download:
content = "attachment; filename='%s'" % (filename)
response['Content-Disposition'] = content
return response
return HttpResponse("Not found")
render_to_pdf 视图
def render_to_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