Unicode Decode for Weasy Print



尝试使用 Django 构建一个简单的 Weasy 打印应用程序

在 views.py 做了一个小功能:

def generate_pdf(request):
# Model data
students = Student.objects.all().order_by('last_name')
context = {
'invoice_id': 18001,
'street_name': 'Rue 76',
'postal_code': '3100',
'city': 'Washington',
'customer_name': 'John Cooper',
'customer_mail': 'customer@customer.at',
'amount': 1339.99,
'today': 'Today',
}
# Rendered
html_string = render_to_string('pdf/invoice.html', context)
html = HTML(string=html_string)
result = html.write_pdf()
# Creating http response
response = HttpResponse(content_type='application/pdf;')
response['Content-Disposition'] = 'inline; filename=list_people.pdf'
response['Content-Transfer-Encoding'] = 'binary'
with tempfile.NamedTemporaryFile(delete=True) as output:
output.write(result)
output.flush()
output = open(output.name, 'r')
response.write(output.read())
return response

运行它后,我得到一个 UnicodeDecodeError 为"response.write(output.read((("行 这是我第一次遇到这样的问题,我该如何解决? 谢谢!

通过简单地将"r"更改为"rb"来修复它:

output = open(output.name, 'rb')

你使用哪个版本的Python?2.x 还是 3.x?

您是否尝试过 encode(( 并导入以下模块:from __future__ import unicode_literals

如果您只想将生成的 PDF 作为 http 响应返回,以下解决方案对我有用。我不知道你的模板。例如,我使用{{ content.invoid_id }}来访问模板中的上下文值。

def generate_pdf(request):
# Model data
students = Student.objects.all().order_by('last_name')
context = {
'invoice_id': 18001,
'street_name': 'Rue 76',
'postal_code': '3100',
'city': 'Washington',
'customer_name': 'John Cooper',
'customer_mail': 'customer@customer.at',
'amount': 1339.99,
'today': 'Today',
}
content = Context()
content.update(context)
template = loader.get_template('yourtemplate.html')
# render and return
html = template.render(context={'content':content}, request=request)
response = HttpResponse(content_type='application/pdf')
HTML(string=html, base_url=request.build_absolute_uri()).write_pdf(response)
return response

不需要临时文件。希望这有帮助!

编辑:如果您需要文件字节,您可以这样做:

def generate_pdf(request):
# Model data
students = Student.objects.all().order_by('last_name')
context = {
'invoice_id': 18001,
'street_name': 'Rue 76',
'postal_code': '3100',
'city': 'Washington',
'customer_name': 'John Cooper',
'customer_mail': 'customer@customer.at',
'amount': 1339.99,
'today': 'Today',
}
content = Context()
content.update(context)
template = loader.get_template('yourtemplate.html')
html = template.render(context={'content':content})
with TemporaryFile() as pdf:
HTML(string=html, base_url=url).write_pdf(pdf)
pdf.seek(0)
# do what every you want with pdf.read())

最新更新