I’m trying to generate a pdf file from an HTML template using Weasyprint python package and I need to send it via email using.
Here’s what i have tried:
JavaScript
x
23
23
1
def send_pdf(request):
2
minutes = int(request.user.tagging.count()) * 5
3
testhours = minutes / 60
4
hours = str(round(testhours, 3))
5
user_info = {
6
"name": str(request.user.first_name + ' ' + request.user.last_name),
7
"hours": str(hours),
8
"taggedArticles": str(request.user.tagging.count())
9
}
10
html = render_to_string('users/certificate_template.html',
11
{'user': user_info})
12
response = HttpResponse(content_type='application/pdf')
13
response['Content-Disposition'] = 'filename=certificate_{}'.format(user_info['name'] + '.pdf')
14
pdf = weasyprint.HTML(string=html).write_pdf(response, )
15
from_email = 'our_business_email_address'
16
to_emails = ['Reciever1', 'Reciever2']
17
subject = "Certificate from INC."
18
message = 'Enjoy your certificate.'
19
email = EmailMessage(subject, message, from_email, to_emails)
20
email.attach("certificate.pdf", pdf, "application/pdf")
21
email.send()
22
return HttpResponse(response, content_type='application/pdf')
23
But it returns an error as
TypeError: expected bytes-like object, not HttpResponse
How can I generate and send a pdf file to an email from HTML template?
Update: With this updated code now it’s generating pdf and sending an email but when I open attached pdf file from recieved email it says
unsupported file formate data
.
Here’s the updated Code:
JavaScript
1
25
25
1
def send_pdf(request):
2
minutes = int(request.user.tagging.count()) * 5
3
testhours = minutes / 60
4
hours = str(round(testhours, 3))
5
user_info = {
6
"name": str(request.user.first_name + ' ' + request.user.last_name),
7
"hours": str(hours),
8
"taggedArticles": str(request.user.tagging.count())
9
}
10
html = render_to_string('users/certificate_template.html',
11
{'user': user_info})
12
response = HttpResponse(content_type='application/pdf')
13
response['Content-Disposition'] = 'filename=certificate_{}'.format(user_info['name']) + '.pdf'
14
pdf = weasyprint.HTML(string=html).write_pdf()
15
from_email = 'arycloud7@icloud.com'
16
to_emails = ['abdul12391@gmail.com', 'arycloud7@gmail.com']
17
subject = "Certificate from Nami Montana"
18
message = 'Enjoy your certificate.'
19
email = EmailMessage(subject, body=pdf, from_email=settings.EMAIL_HOST_USER, to=to_emails)
20
# email.attach("certificate.pdf", pdf, "application/pdf")
21
email.content_subtype = "pdf" # Main content is now text/html
22
email.encoding = 'ISO-8859-1'
23
email.send()
24
return HttpResponse(pdf, content_type='application/pdf')
25
Help me, please!
Thanks in advance!
Advertisement
Answer
Here’s the complete working version of above code:
JavaScript
1
23
23
1
user_infor = ast.literal_eval(ipn_obj.custom)
2
if int(user_infor['taggedArticles']) > 11:
3
# generate and send an email with pdf certificate file to the user's email
4
user_info = {
5
"name": user_infor['name'],
6
"hours": user_infor['hours'],
7
"taggedArticles": user_infor['taggedArticles'],
8
"email": user_infor['email'],
9
}
10
html = render_to_string('users/certificate_template.html',
11
{'user': user_info})
12
response = HttpResponse(content_type='application/pdf')
13
response['Content-Disposition'] = 'filename=certificate_{}'.format(user_info['name']) + '.pdf'
14
pdf = weasyprint.HTML(string=html, base_url='http://8d8093d5.ngrok.io/users/process/').write_pdf(
15
stylesheets=[weasyprint.CSS(string='body { font-family: serif}')])
16
to_emails = [str(user_infor['email'])]
17
subject = "Certificate from Nami Montana"
18
email = EmailMessage(subject, body=pdf, from_email=settings.EMAIL_HOST_USER, to=to_emails)
19
email.attach("certificate_{}".format(user_infor['name']) + '.pdf', pdf, "application/pdf")
20
email.content_subtype = "pdf" # Main content is now text/html
21
email.encoding = 'us-ascii'
22
email.send()
23