Search code examples
phppdflib

PDFlib Greek is not output


I got a problem with display text in greek.

The "$text = '111 πορτοκαλί 111';" will only output the 111 and not the Text.

When I use the Font "Symbols" it's "ok". But how to mix it up? Sometimes its Greek, sometimes French and so on...

Any hints?

$pdf = PDF_new();
pdf_set_parameter($pdf, "licensefile", "/etc/licensekeys.txt");
pdf_set_parameter($pdf, "compatibility", "1.6");
pdf_set_parameter($pdf, "textformat", "utf8");
PDF_begin_document($pdf, "", "");

$fontUnicode = PDF_load_font($pdf, "Helvetica",  "unicode", NULL);

PDF_begin_page_ext($pdf, 0, 0, "width=200 height=200");

pdf_setcolor($pdf, "both","cmyk",0.53,0.43,0.43,0.28);


PDF_setfont($pdf, $fontUnicode, 8);
$text = '111 πορτοκαλί 111';
PDF_show_xy($pdf, $text, 100, 100);


PDF_end_page_ext($pdf, '');
PDF_end_document($pdf, '');

$buf = pdf_get_buffer($pdf);
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=test.pdf");
print $buf;
PDF_delete($pdf);

Solution

  • The problem here is that the PDF Core Font Helvetica used contains very few glyphs and, for example, glyphs for Greek.

    In order for the PDF to display the text correctly, you must use a font that contains the appropriate characters. Not every font contains a glyph vor every character. Thus, when choosing a font, you must consider both the appearance (that is, whether you like the font face) and whether the font contains the characters you want to use.

    For example, the current PDFlib packages include the NotoSerif-Regular font which contains many glyphs, including Greek. This could be a good approach. This is shown e.g. in the supplied starter_basic.php example which is also included in the PDFlib Cookbook.

    https://www.pdflib.com/pdflib-cookbook/general/starter_basic/php/

    This sample shows various ways how you can apply a string:

    /* Plain ASCII text */
    $p->fit_textline("en: Hello!", 50, 700, $optlist);
    
    /* Unicode code points */
    $p->fit_textline("gr: \u{0393}\u{03B5}\u{03B9}\u{03AC}!", 50, 650, $optlist);
    
    /* UTF-8 text */
    $p->fit_textline("ru: Привет!", 50, 600, $optlist);
    
    /* PDFlib character reference */
    $p->fit_textline("es: ¡Hola!", 50, 550, $optlist . " charref=true");
    

    You can see the greek glyphs in the output: enter image description here

    I can only recommend that you start with the provided examples and then look for more sample code in the PDFlib Cookook. This usually leads to much better code in your application, because this code will then also match the PDFlib version you are using. Outdated code which you might find somewhere can cause problems when updating.

    Another recommendation:

    in this example code use the deprecated functional PDFlib API (PDF_). This is no longer available with PDFlib 10 (the current version) and thus not recommended for new projects. (Please see PDFlib 10 Mingration Guide for details on this)

    If you are starting from scratch, I would recommend to start with the latest PDFlib 10.