Xhtml2pdf проблема с конвертированием html-файла с польскими символами
Я пытаюсь сделать счета-фактуры, создавая html файл и конвертируя его в pdf, а затем отправляя как http ответ. Проблема в том, что эти счета содержат польские символы, которые UTF-8 не отображает пример. Я пытался использовать ISO-8859-2 для их отображения, но получаю ошибку: ('charmap' codec can't encode characters in position 1159-1163: character maps to ) error.
utils.py:
from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template
from xhtml2pdf import pisa
def render_to_pdf(template_src, context_dict={}):
template = get_template(template_src)
html = template.render(context_dict)
result = BytesIO()
pdf = pisa.pisaDocument(
src=BytesIO(html.encode('ISO-8859-2')),
dest=result,
encoding='UTF-8'
)
if pdf.err:
return HttpResponse('We had some errors <pre>' + html + '</pre>')
return HttpResponse(result.getvalue(), content_type='application/pdf')
views.py:
class GeneratePDF(View):
def get(self, request, pk=None):
"getting data here"
pdf = render_to_pdf("invoice.html", data)
if pdf:
response = HttpResponse(pdf, content_type='application/pdf')
filename = "Sales_Invoice_%s.pdf" % ("name")
content = "inline; filename=%s" % (filename)
download = request.GET.get("download")
if download:
content = "attachment; filename='%s'" % (filename)
response['Content-Disposition'] = content
return response
return Response(status=rest_status.HTTP_404_NOT_FOUND)
Замораживание труб:
appdirs==1.4.4
arabic-reshaper==2.1.3
asgiref==3.4.1
autopep8==1.5.7
backcall==0.2.0
backports.entry-points-selectable==1.1.0
beautifulsoup4==4.10.0
certifi==2021.5.30
cffi==1.14.6
charset-normalizer==2.0.5
colorama==0.4.4
cryptography==3.4.8
debugpy==1.5.0
decorator==5.1.0
defusedxml==0.7.1
distlib==0.3.2
Django==3.2.7
django-allauth==0.45.0
django-cors-headers==3.8.0
django-rest-auth==0.9.5
djangorestframework==3.12.4
entrypoints==0.3
filelock==3.0.12
future==0.18.2
html5lib==1.1
idna==3.2
ipykernel==6.4.1
ipython==7.28.0
ipython-genutils==0.2.0
jedi==0.18.0
jupyter-client==7.0.6
jupyter-core==4.8.1
matplotlib-inline==0.1.3
mysqlclient==2.0.3
nest-asyncio==1.5.1
oauthlib==3.1.1
parso==0.8.2
pickleshare==0.7.5
Pillow==8.3.2
platformdirs==2.3.0
prompt-toolkit==3.0.20
psycopg2==2.9.1
pycodestyle==2.7.0
pycparser==2.20
Pygments==2.10.0
PyJWT==2.1.0
PyPDF2==1.26.0
python-bidi==0.4.2
python-dateutil==2.8.2
python3-openid==3.2.0
pytz==2021.1
pywin32==301
pyzmq==22.3.0
reportlab==3.6.1
requests==2.26.0
requests-oauthlib==1.3.0
six==1.16.0
soupsieve==2.2.1
sqlparse==0.4.2
toml==0.10.2
tornado==6.1
traitlets==5.1.0
urllib3==1.26.6
virtualenv==20.7.2
wcwidth==0.2.5
webencodings==0.5.1
xhtml2pdf==0.2.5