Fix International Characters Encoding For FPDF Output

If you use the jQuery Autocomplete widget with GeoNames webservice for city names, you’ll find that city names use a bunch of non-ASCII, ISO 8859-x characters that don’t display properly in FPDF documents. I’m using FPDF to generate a PDF that uses city names from GeoNames. The following city names have international characters that output strange characters.

Gotenica, Kočevje, Slovenia outputs:

Gotenica, Kočevje, Slovenia

Sokal’, L’vivs’ka Oblast’, Ukraine outputs:

Sokal’, L’vivs’ka Oblast’, Ukraine

Litu, Läänemaa, Estonia outputs:

Litu, Läänemaa, Estonia

Gothenburg, Västra Götaland, Sweden outputs:

Gothenburg, Västra Götaland, Sweden

Plungė, Lithuania outputs:

PlungÄ—, Lithuania

Bucharest, BucureÅŸti, Romania outputs:

Bucharest, BucureÅŸti, Romania

So, to fix this, I do this before I output the city name in FPDF.


setlocale(LC_CTYPE, 'en_US');
$converted_cityName = iconv('UTF-8', 'ASCII//TRANSLIT', $cityName);

// now you can output the converted city name to pdf
$pdf->Cell(0, 0, $converted_cityName, 0, 1, C);

If you want more information on why this wrongful encoding of non-ASCII characters turns them into “garbage characters”, here is a good post explaining it: Fixing common Unicode mistakes with Python — after they’ve been made

See more:

We've One Response

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]