I'm working on a PDF generator on the Django back-end. I use reportlab. It seems to work, but the encoding is not correct. When I use diacritic characters, it gets the wrong characters/signs.
The problem is very similar to: Django - pdf response has wrong encoding - xhtml2pdf
But I use reportlab, which allows font adding. I registered in reportlab the font that supports Polish diacritics: "Aleo".
pdfmetrics.registerFont(TTFont('Aleo', './resources/fonts/Aleo/Aleo-Light.ttf'))
pdfmetrics.registerFont(TTFont('AleoBd', './resources/fonts/Aleo/Aleo-Bold.ttf'))
pdfmetrics.registerFont(TTFont('AleoIt', './resources/fonts/Aleo/Aleo-Italic.ttf'))
pdfmetrics.registerFont(TTFont('AleoBI', './resources/fonts/Aleo/Aleo-BoldItalic.ttf'))
registerFontFamily('Aleo', normal='Aleo', bold='AleoBd', italic='AleoIt', boldItalic='AleoBI')
Example outputting pdf in djagno:
file_response = Album.pdf_generator(request.user, request.data.get('album_id'))
# Make copy to save local pdf file and send via django
binary_copy = deepcopy(file_response)
with open('test.pdf', 'wb') as f:
f.write(binary_copy.read())
content_type = {'pdf': 'application/pdf'}
response = HttpResponse(file_response, content_type=content_type)
response['Content-Disposition'] = 'attachment; filename=moja_nazwa.pdf'
# response = FileResponse(file_response, as_attachment=True, filename='hello.pdf')
return response
Example two files generate from the same bytesIO:
A. Local file B. Shared FileResponse or HttpResponse file:
What's strange is if I use the "open with" option after clicking on the link "download" in swagger and I choose some other program, e.g. "wps pdf" I will get other characters in generated pdf..
Analyzing the problem, I finally came to the cause of creating pdfs with wrong encoding. The problem does not lie with reportlab, but with Swagger, who treats his content as text data and not binary data when sharing the file.