Reportlab in Heroku - Word error with accent

Asked

Viewed 140 times

0

I’m generating reports with reportlab

But when there are words accentuated, I’m taking 500 Internal Server Error in the Heroku.

Localhost works just fine. I tried the next one didn’t work out

reports.py

# -*- coding: utf-8 -*-

LOG HEROKU ERROR

    [ERROR] Error handling request /laudo/16/paciente/5/imprimir/
    Traceback (most recent call last):
    File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/workers/sync.py", line 135, in handle
        self.handle_request(listener, req, client, addr)
    File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/workers/sync.py", line 182, in handle_request
        resp.write(item)
    File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/http/wsgi.py", line 342, in write
        self.send_headers()
    File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/http/wsgi.py", line 338, in send_headers
        util.write(self.sock, util.to_bytestring(header_str, "ascii"))
    File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/util.py", line 511, in to_bytestring
        return value.encode(encoding)
    UnicodeEncodeError: 'ascii' codec can't encode character '\xf3' in position 220: ordinal not in range(128)

Django 1.10+

Python 3.5.1

1 answer

1


I found the answer

The mistake was in the filename that is, is how will "call" the file Voce will download in PDF

BEFORE

def gerar_laudo(request, laudo_id, paciente_id):

    filename = "laudo_{}".format(paciente.nome)
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="{}.pdf"'.format(filename)

    c.setTitle("Laudo de {}".format(paciente.nome))
    c.showPage()
    c.save()

AFTERWARD

Import normalize and create a method to remove accents

from unicodedata import normalize

def gerar_laudo(request, laudo_id, paciente_id):

    # nova variavel para guardar o nome sem acento
    nome_sem_acento = remover_acentos(paciente.nome)
    filename = "laudo_{}".format(nome_sem_acento)
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="{}.pdf"'.format(filename)

    c.setTitle("Laudo de {}".format(paciente.nome))
    c.showPage()
    c.save()

def remover_acentos(txt):
    """ metodo que remove os acento das palavras """
    return normalize('NFKD', txt).encode('ASCII','ignore').decode('ASCII')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.