【发布时间】:2018-05-22 19:30:09
【问题描述】:
我正在尝试通过电子邮件发送 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 pdf
return None
在我的观点中:
pdf = render_to_pdf('expenses/pdf_report.html', data)
mail.attach_file(pdf)
mail.send()
我收到此错误:
预期的 str、字节或 os.PathLike 对象,而不是 pisaContext
如何获取 str 或 bytes 而不是 pisaContext?
【问题讨论】:
-
您必须在您的 render_to_pdf() 中返回结果:
return HttpResponse(result.getvalue(), content_type='application/pdf')。 -
@Borut 预期 str、字节或 os.PathLike 对象,而不是 HttpResponse
-
基于 Django 文档:link,您应该使用 attach() 而不是 attach_file()。那么它应该与 return result.getvalue() 一起使用。