Search code examples
phpfpdf

Serbian characters in FPDF


I have problem with Serbian language in FPDF http://www.fpdf.org/

$mystring = "Запослени у рачуноводству/сродном радном месту";

I used $pdf->Write and MultiCell... all are same result.

utf8_decode($mystring) -> ????????? ? ?????????????/??????? ?????? ?????

iconv('UTF-8', 'utf-8//TRANSLIT', $mystring) -> ЗапÐ3⁄4ѕлÐμÐ1⁄2Ð ̧ у рачуÐ1⁄2Ð3⁄4Ð2Ð3⁄4Ð ́Ñ•Ñ‚Ð2у/Ñ•Ñ€Ð3⁄4Ð ́Ð1⁄2Ð3⁄4Ð1⁄4 раР́Ð1⁄2Ð3⁄4Ð1⁄4Ð1⁄4Ðμѕту

thank so much


Solution

  • Personally I would not use UTF-8 in my PDFs because the file size will big for me. I use font embedding in this case to avoid big file size.

    The FPDF class can produce documents in many languages other than the Western European ones: Central European, Cyrillic, Greek, Baltic and Thai, provided you own TrueType or Type1 fonts with the desired character set. UTF-8 support is also available.

    For the UTF-8 support you have to use tFPDF which is based on FPDF. tFPDF accepts UTF-8 encoded text. Please read all this instructions and then download the ZIP file on the bottom from this site.

    Convertation to UTF-8:

    You do not need a convertation to UTF-8 if you use instructions above (from link) and load a UTF-8 string from a file like follows:

    // this file file must be saved in UTF-8 before:
    $str = file_get_contents('Text-saved-in-UTF-8.txt');
    

    In other case:

    You have to use mb_convert_encoding and not iconv.

    For Serbian (Latin) this is either 'iso-8859-2' or 'windows-1250'. For Serbian (Cyrillic) this is 'iso-8859-5' or 'windows-1251'. These encodings will provide correct display of diacritic Latin characters such as žćđš.

    In yor case for:

    $mystring = "Запослени у рачуноводству/сродном радном месту";
    

    You have to write:

    $str = mb_convert_encoding($mystring, 'UTF-8', 'iso-8859-5');
    

    or

    $str = mb_convert_encoding($mystring, 'UTF-8', 'windows-1251');
    

    Your PHP file must be saved in iso-8859-5 or in windows-1251 before (but not in UTF-8).

    And then you use this in tFPDF.