Reportlab in Heroku - Error of words with accent

0

I am generating reports with reportlab

But when you have words with an accent, I'm taking 500 Internal Server Error no Heroku .

Localhost works perfectly. I tried the following did not work

reports.py

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

HEROKU LOG 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

    
asked by anonymous 17.03.2017 / 01:14

1 answer

1

I found the answer

The error was in filename ie, how will you "call" the file that you 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()

AFTER

Import the normalize and create a method to take the 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')
    
17.03.2017 / 02:03