【发布时间】:2018-09-05 04:18:32
【问题描述】:
我现在正在尝试将使用 HTML 呈现的 pdf 文件保存到模型字段,它会引发此错误。
强制转换为 Unicode:需要字符串或缓冲区,找到实例
这是代码
def save_to_pdf(template_src, context_dict, pk):
import ipdb; ipdb.set_trace()
instance = get_object_or_404(
Project.objects.filter(pk=pk, is_deleted=False))
template = get_template(template_src)
context = Context(context_dict)
html = template.render(context)
result = StringIO.StringIO()
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result,link_callback=fetch_resources)
pdfnew=file(pdf)
instance.structural_info.save('structure.pdf',pdfnew)
return True
structural_info 是文件字段。 正确的做法是什么?
【问题讨论】:
标签: python django file pdf django-models