2

尝试使用 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

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

4

3 回答 3

2

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

output = open(output.name, 'rb')
于 2018-11-02T04:45:08.553 回答
0

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

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

于 2018-09-21T05:08:20.783 回答
0

如果您只想将生成的 PDF 作为 http-response 返回,以下解决方案对我有用。我不知道你的模板。我使用{{ 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())
于 2018-10-01T12:57:18.683 回答