Kroitus
Kroitus

Reputation: 61

Template2pdf, django incorrect non-ascii characters

I am trying to generate report with template2pdf.dj for my django site. Because I am from Lithuania, I must use some non-ascii characters (ąčęėįšųūž). But when I get my pdf from rml, some characters are displayed correctly (š,ž), but others are jus black squares. I tried to change fonts in rml file. If I use thos,e that Repotlab suggests, I get same output. But if I try to use some other (like Arial), it gives me an error, that it cannot determine family/bold/italic.

Where could be the problem? Do I have to put encoding somewhere, or change font?

Upvotes: 0

Views: 529

Answers (2)

Kroitus
Kroitus

Reputation: 61

The problem was with default fonts. They were not unicode. I added custom font with , and it works perfectly.

Upvotes: 1

Gordon Seidoh Worley
Gordon Seidoh Worley

Reputation: 8068

You need to force the use of UTF-8 characters rather than Unicode characters. ReportLab expects UTF-8 so you have to make sure everything is correctly converted. What tends to happen in Python, in my experience, is that when you read in text from a file or stream it comes in Unicode encoded and you have to jump through some hoops to make it come out UTF-8 or whatever you want. This matters for non-ASCII characters because although Unicode represents them with a single byte using the 128 extra characters you get from using 8 bits rather than the 7 ASCII uses, UTF-8 uses two bytes to represent non-ASCII Latin characters and even more bytes for other characters. Everything in Latin-1 that is not in ASCII is not, on a bit-for-bit level, in UTF-8, hence the need for conversion and the reason you get blocks rather than characters in your PDF.

I'm not sure why some of the characters are displaying correctly for you; I would expect none of them to work. Regardless, though, making sure to convert to UCF-8 should fix it.

Upvotes: 1

Related Questions