john
john

Reputation: 131

Emailing a PDF Generated from HTML: expected bytes-like object, not HttpResponse

I am trying to email a PDF that has been generated from a HTML template.

I get the following error: 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 protected]"])
   msg.attach('my_pdf.pdf', pdf, 'application/pdf')
   msg.content_subtype = "html"
   msg.send()

   return HttpResponse(pdf, content_type='application/pdf')

Upvotes: 0

Views: 598

Answers (1)

Anthony
Anthony

Reputation: 959

You're returning a HttpResponse in your function.

Simply change return HttpResponse(result.getvalue(), content_type='application/pdf')

to return result.getvalue()

Upvotes: 1

Related Questions