使用邮件django-pisa附加动态生成的pdf

时间:2017-07-11 06:47:20

标签: email pdf django-1.5 xhtml2pdf pisa

在我的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 view

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

0 个答案:

没有答案