Python Forum

Full Version: fpdf star character issue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I have a database with a number of fields and 1 of the fields is Rating. This field contains the word, One, Two, Three, Four or Five.

Each time I loop through a record, I want the Rating string to be converted to x number of stars based on the string passed to it.

I have written a function that does this. The function displays the correct # of stars if I am sending the output to my VSC Terminal. When I try convertting it to a pdf (using fpdf) I receive an error:

# partial code in my fpdf function
tmp = star_rating(row[7])
pdf.cell(25, 0, tmp, align="L", border=0)

# partial code in star_rating()
star = "\u2B50"
if string == "Three":
        times = 3
etc.
return star * times
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 1275-1278: ordinal not in range(256)
The font I am using is Arial.
You define your asterisk as unicode "\u2B50" But the message says something about using latin-1. What happens if you define star = "*" ?
You are asking for a unicode character \u2B50 that is outside the latin-1 encoding used by fpdf. I think the fonts it uses don't have characters outside latin-1.

Either change it to an image and stick an image there every time, or switch to a latin-1 compatible character.
I think I will do the image thing. Much easier
Thanks for your help.