Error downloading PDF with python3

I’ve just used bench migrate-env python3 to upgrade my python version but now I cannot download PDF files. It shows an internal server error with the following error in the logs

[2019-04-07 01:03:42 +0300] [19874] [ERROR] Error handling request /api/method/frappe.utils.print_format.download_pdf?doctype=Sales%20Invoice&name=%CE%99%CE%9F-%CE%A4%CE%A0%CE%A5-00048&format=Invoice%20-%20Greek%20ORIGINAL&no_letterhead=1&_lang=en
Traceback (most recent call last):
  File "/home/frappe/frappe-bench/env/lib/python3.5/site-packages/gunicorn/workers/", line 135, in handle
    self.handle_request(listener, req, client, addr)
  File "/home/frappe/frappe-bench/env/lib/python3.5/site-packages/gunicorn/workers/", line 182, in handle_request
  File "/home/frappe/frappe-bench/env/lib/python3.5/site-packages/gunicorn/http/", line 333, in write
  File "/home/frappe/frappe-bench/env/lib/python3.5/site-packages/gunicorn/http/", line 329, in send_headers
    util.write(self.sock, util.to_bytestring(header_str, "ascii"))
  File "/home/frappe/frappe-bench/env/lib/python3.5/site-packages/gunicorn/", line 507, in to_bytestring
    return value.encode(encoding)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 160-163: ordinal not in range(128)

Any ideas how to fix?

Can you say what version bench and ERPNext, and also the output of: locale from a terminal? Is there a non standard character in the form ? Perhaps post a screenshot of the form before trying to convert to PDF.

Just a couple things that might help us help…Preformatted text

Hi of course I can provide any info needed.

bench: 4.1.0
ERPNext: v12.x.x-develop () (develop)
Frappe Framework: v12.x.x-develop () (develop)

Here’s the form

And as soon as I press on PDF the error appears due to

which forces ascii conversion.
util.write(self.sock, util.to_bytestring(header_str, "ascii"))

If I hardcode utf8 here it works but pretty much any form containing anything other than english breaks. Even standard forms.

Output of locale

If gunicorn is suspect, I wonder if installing python3-gunicorn would help. Otherwise , a google search of relevant parts of the UnicodeEncodeError message + ERPNext turns up a few posts that might help. .

Thanks for trying to help. Unfortunately installing python3-gunicorn made no difference as the code is still there converting utf8 characters to ascii.
I think this will need a lot more info from the devs to troubleshoot.

While waiting for devs or others to notice, perhaps try a couple things to see if the cause can be narrowed.

If Letter Head is unchecked , and/or if Settings, Print without letterhead - does it change anything?

If a new form is created, without any vestiges of the old ones, can it be printed as PDF?

“pretty much any form containing anything other than english breaks. Even standard forms.”

Please report this on Issues · frappe/erpnext · GitHub and link here.

For example this notes gotchas best resolved IMHO with a test environment to identify the correct fix

I’ve opened an issue regarding this problem with python3 and unicode characters in PDF · Issue #17536 · frappe/erpnext · GitHub.