Search code examples
phpcharacter-encodingfpdf

Fpdf and special characters


I am trying to write out some special characters with built in fonts, is there any way to do this?

$str = 'ščťžýáíéäúň§ôúőűáéóüöűú'; $str = iconv('UTF-8', 'windows-1252', $str);

the result is one letter Š, not too good. :)


Solution

  • I know it's an old thread, but I've faced the issue this weekend and spent a longtime Googling and playing, so here's a time saver. http://fpdf.org/en/script/script92.php is the way to go to use diacritics (accented characters). But you need to add some code to it... Slot this in at line 617

     /* Modified by Vinod Patidar due to font key does not match in dejavu bold.*/
        if ( $family == 'dejavu' && !empty($style) && ($style == 'B' || $style == 'b') ) {
            $fontkey = $family.' '.strtolower($style);
        } else {
            $fontkey = $family.$style;  
        }
        /* Modified end here*/
    

    Then change

    if($family=='arial')
            $family = 'helvetica';
    

    To

     if($family=='arial'||$family='dejavu')
            $family = 'helvetica';
    

    Then don't use the font in the example "DejaVu Sans Condensed" because Condensed seems to mean the Bold version doesn't contain all the characters

    You may also need to add the getPageWidth and getPageHeight methods from the normal fpdf.php script as it is newer than tfpdf.php!

    With the changes above

    $pdflabel->AddFont('DejaVu','','DejaVuSans.ttf',true);  
    $pdf->AddFont('DejaVu','B','DejaVuSans-Bold.ttf',true);
    

    Works a good 'un with European languages