DOMPDF problem with Cyrillic characters

后端 未结 7 1271
故里飘歌
故里飘歌 2020-12-08 17:48

I am using the DOMPDF library to create an invoice in PDF. This document can be in French, Russian or English, but I am having trouble printing Russian characters.

F

相关标签:
7条回答
  • 2020-12-08 18:17

    In accepted answer link is broken and it contained old version of DOMPDF.

    To work with unicode symbols in DOMPDF 0.6 you have two alternatives: use existed fonts or create your own font.

    • Use existed font (applied for DOMPDF 0.6):

      1. Download archive and extract.
      2. Copy extracted files in your dompdf fonts folder /dompdf/lib/fonts/.
      3. Edit dompdf_font_family_cache.dist.php with snippet 1.
      4. In CSS use font-family: times;.

    Snippet 1:

    /* ... */
    'times' => array (
        'normal' => DOMPDF_FONT_DIR . 'times',
        'bold' => DOMPDF_FONT_DIR . 'timesbd',
        'italic' => DOMPDF_FONT_DIR . 'timesi',
        'bold_italic' => DOMPDF_FONT_DIR . 'timesbi'
    ),
    'times-roman' => array (
        'normal' => DOMPDF_FONT_DIR . 'times',
        'bold' => DOMPDF_FONT_DIR . 'timesbd',
        'italic' => DOMPDF_FONT_DIR . 'timesi',
        'bold_italic' => DOMPDF_FONT_DIR . 'timesbi'
    ),
    /* ... */
    

    • If you want to use your own TTF font (say, Arial.ttf):

      1. Run: ttf2afm -o Arial.afm Arial.ttf. (I did it in Ubuntu.)
      2. Run: ttf2ufm -a -F Arial.ttf. (I did it in Windows using exe from UFPDF, but I guess you can use /dompdf/lib/ttf2ufm/bin/ttf2ufm.exe.)
      3. Copy Arial.* files in /dompdf/lib/fonts/.
      4. Add to dompdf_font_family_cache.dist.php snippet 2.
      5. In CSS use font-family: arial;.

    Snippet 2:

    /* ... */
    'arial' => array (
        'normal' => DOMPDF_FONT_DIR . 'Arial',
        'bold' => DOMPDF_FONT_DIR . 'Arial',
        'italic' => DOMPDF_FONT_DIR . 'Arial',
        'bold_italic' => DOMPDF_FONT_DIR . 'Arial'
    )
    /* ... */
    
    0 讨论(0)
  • 2020-12-08 18:20

    Problem is with fonts default dompdf uses (that is it doesn't have all unicode characters, whick are by now over 5000). Usually arialuni.ttf is what you need. You can download localized russian version at http://chernev.ru/dompdf.rar {broken link}

    Updated link: https://code.google.com/p/ipwn/downloads/detail?name=arialuni.ttf

    0 讨论(0)
  • 2020-12-08 18:20

    For me the 4 steps above didn't resolve the issue. Besides that, dompdf converts created pdf to ANSI (ISO) You need to disable this on the options page http://domain.com/admin/settings/print/pdf

    Tick the checkbox Use dompdf's Unicode Mode. This will force to create files in UTF-8/Unicode.

    Please note that web settings override settings in dompdf_config.inc.php by default.

    0 讨论(0)
  • 2020-12-08 18:20

    In your html, use this style :

    <style type="text/css">
    * {
      /*font-family: Helvetica, sans-serif;*/
      font-family: "DejaVu Sans", sans-serif;
    }
    </style>
    
    0 讨论(0)
  • 2020-12-08 18:21

    if you will use DejaVu font you can see cyrillic characters

    The DejaVu TrueType fonts have been pre-installed to give dompdf decent Unicode character coverage by default. To use the DejaVu fonts reference the font in your stylesheet, e.g. body { font-family: DejaVu Sans; } (for DejaVu Sans).

    DOMPDF include DejaVu font be default

        $html = "<html><head><style>body { font-family: DejaVu Sans }</style>".
            "<body>А вот и кириллица</body>".
            "</head></html>";
    
        $dompdf = new \DOMPDF();
        $dompdf->load_html($html);
        $dompdf->render();
        echo file_put_contents('cyrillic.pdf', $dompdf->output());
    

    You can also set change def for font by default in dompdf_config.inc.php

    def("DOMPDF_DEFAULT_FONT", "DejaVu Sans");
    
    0 讨论(0)
  • Download arialuni.ttf Run php load_font.php 'Arial' arialuni.ttf in dompdf directory, set font to arial It works ;)

    0 讨论(0)
提交回复
热议问题