【发布时间】:2020-08-21 16:39:29
【问题描述】:
我正在尝试使用 Weasyprint python 包从 HTML 模板生成一个 pdf 文件,我需要使用它通过电子邮件发送。
这是我尝试过的:
views.py:
user_info = {
"name": 'nadjib',
}
html_string = render_to_string('Proformas/command.html')
html = HTML(string=html_string,base_url=request.build_absolute_uri())
# html.write_pdf(target='/tmp/mypdf.pdf');
fs = FileSystemStorage('/tmp')
with fs.open('mypdf.pdf') as pdf:
response = HttpResponse(pdf, content_type='application/pdf')
response['Content-Disposition'] = 'filename="Commande.pdf"'
pdf = weasyprint.HTML(string=html_string, base_url=request.build_absolute_uri()).write_pdf(
stylesheets=[weasyprint.CSS(string='body { font-family: serif}')])
to_emails = ['nadjzdz@gmail.com']
subject = "SH INFOR FACTURE"
email = EmailMessage(subject, body=pdf, from_email='attaazd@gmail.com', to=to_emails)
email.attach("Commande".format(user_info['name']) + '.pdf', pdf, "application/pdf")
email.content_subtype = "pdf" # Main content is now text/html
email.encoding = 'us-ascii'
email.send()
return response
pdf 渲染成功,但没有通过邮件发送?
【问题讨论】:
标签: django weasyprint