dompdf character encoding UTF-8

后端 未结 11 2025
我在风中等你
我在风中等你 2020-12-01 21:07

Im trying to create pdf with correct characters, but there are \"?\" chars. I created a test php file, where Im trying to fing the best solution. If Im open in the browser t

相关标签:
11条回答
  • 2020-12-01 21:30

    If you don't mind having only one charset you can change every charset in dompdf_font_family_cache.dist.php

    just like

    <?php
    $distFontDir = $rootDir . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'fonts' . DIRECTORY_SEPARATOR;
    return array(
        'sans-serif' =>
        array(
            'bold' => $distFontDir . 'DejaVuSans-Bold',
            'bold_italic' => $distFontDir . 'DejaVuSans-BoldOblique',
            'italic' => $distFontDir . 'DejaVuSans-Oblique',
            'normal' => $distFontDir . 'DejaVuSans'
        ),
        'times' =>
        array(
            'bold' => $distFontDir . 'DejaVuSans-Bold',
            'bold_italic' => $distFontDir . 'DejaVuSans-BoldOblique',
            'italic' => $distFontDir . 'DejaVuSans-Oblique',
            'normal' => $distFontDir . 'DejaVuSans'
        ),
        'times-roman' =>
        array(
            'bold' => $distFontDir . 'DejaVuSans-Bold',
            'bold_italic' => $distFontDir . 'DejaVuSans-BoldOblique',
            'italic' => $distFontDir . 'DejaVuSans-Oblique',
            'normal' => $distFontDir . 'DejaVuSans'
        ),
        'courier' =>
        array(
            'bold' => $distFontDir . 'DejaVuSans-Bold',
            'bold_italic' => $distFontDir . 'DejaVuSans-BoldOblique',
            'italic' => $distFontDir . 'DejaVuSans-Oblique',
            'normal' => $distFontDir . 'DejaVuSans'
        ),
        'helvetica' =>
        array(
            'bold' => $distFontDir . 'DejaVuSans-Bold',
            'bold_italic' => $distFontDir . 'DejaVuSans-BoldOblique',
            'italic' => $distFontDir . 'DejaVuSans-Oblique',
            'normal' => $distFontDir . 'DejaVuSans'
        ),
        'zapfdingbats' =>
        array(
            'bold' => $distFontDir . 'DejaVuSans-Bold',
            'bold_italic' => $distFontDir . 'DejaVuSans-BoldOblique',
            'italic' => $distFontDir . 'DejaVuSans-Oblique',
            'normal' => $distFontDir . 'DejaVuSans'
        ),
        'symbol' =>
        array(
            'bold' => $distFontDir . 'DejaVuSans-Bold',
            'bold_italic' => $distFontDir . 'DejaVuSans-BoldOblique',
            'italic' => $distFontDir . 'DejaVuSans-Oblique',
            'normal' => $distFontDir . 'DejaVuSans'
        ),
        'serif' =>
        array(
            'bold' => $distFontDir . 'DejaVuSans-Bold',
            'bold_italic' => $distFontDir . 'DejaVuSans-BoldOblique',
            'italic' => $distFontDir . 'DejaVuSans-Oblique',
            'normal' => $distFontDir . 'DejaVuSans'
        ),
        'monospace' =>
        array(
            'bold' => $distFontDir . 'DejaVuSans-Bold',
            'bold_italic' => $distFontDir . 'DejaVuSans-BoldOblique',
            'italic' => $distFontDir . 'DejaVuSans-Oblique',
            'normal' => $distFontDir . 'DejaVuSans'
        ),
        'fixed' =>
        array(
            'bold' => $distFontDir . 'DejaVuSans-Bold',
            'bold_italic' => $distFontDir . 'DejaVuSans-BoldOblique',
            'italic' => $distFontDir . 'DejaVuSans-Oblique',
            'normal' => $distFontDir . 'DejaVuSans'
        ),
        'dejavu sans' =>
        array(
            'bold' => $distFontDir . 'DejaVuSans-Bold',
            'bold_italic' => $distFontDir . 'DejaVuSans-BoldOblique',
            'italic' => $distFontDir . 'DejaVuSans-Oblique',
            'normal' => $distFontDir . 'DejaVuSans'
        ),
        'dejavu sans mono' =>
        array(
            'bold' => $distFontDir . 'DejaVuSansMono-Bold',
            'bold_italic' => $distFontDir . 'DejaVuSansMono-BoldOblique',
            'italic' => $distFontDir . 'DejaVuSansMono-Oblique',
            'normal' => $distFontDir . 'DejaVuSansMono'
        ),
        'dejavu serif' =>
        array(
            'bold' => $distFontDir . 'DejaVuSerif-Bold',
            'bold_italic' => $distFontDir . 'DejaVuSerif-BoldItalic',
            'italic' => $distFontDir . 'DejaVuSerif-Italic',
            'normal' => $distFontDir . 'DejaVuSerif'
        )
    )
    ?>
    

    I know it's not the best way, but it saves lot of time

    0 讨论(0)
  • 2020-12-01 21:39

    Lots of answers here, struggled to get any to provide cross-language support reliably. I believe that for those of us making distributed software, there is also server-setting blocks which stop some functionality such as @import and src:url() in pdfdom automatically working to embed a font.

    The following solution has worked across many servers & locally hosted sites, and requires no command line access:

    1. Retrieve font you want to use as a .ttf (for language support including Cyrillic, Greek, Devanagari, Latin, and Vietnamese, we used Noto Sans with all optional languages checked)
    2. Run/build-in the following script and fire PDFBuilder_install_font_family() ONCE only (singular install)

    Gist for PDFBuilder_install_font_family(): https://gist.github.com/woodyhayday/f8dc36cc7ec922bc1894f33eb2b0e928

    0 讨论(0)
  • 2020-12-01 21:42

    You should read over the Unicode How-to again. The main problem is that you don't specify a font that supports your characters. It looks like you've read the how-to, because you're using the font example from that document. However the example was not meant to apply globally to any document, dompdf doesn't include firefly (a Chinese character font) or Verdana by default.

    If you do not specify a font then dompdf falls back to one of the core fonts (Helvetica, Times Roman, Courier) which only support Windows ANSI encoding. So always be sure to style your text with a font that supports Unicode encoding and has the characters you need to display.

    With dompdf 0.6.0 you can use the included Deja Vu fonts. So the following should work (just the HTML):

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <style>
      body { font-family: DejaVu Sans, sans-serif; }
    </style>
    <title>č s š Š</title>
    </head>
    <body>
      <p>??????X Ponuka číslo € černý Češký <br></p>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-01 21:44

    Dompdf does not support fallback fonts, so you can't use your favorite font if it does not support your characters, and you also can't set another font to be the fallback font for those characters like droid sans fallback.

    What you can do instead is take advantage of regex unicode script ranges: https://www.regular-expressions.info/unicode.html to wrap those blocks of text into spans and give them the fallback font.

    Example:

    $body = 'test 简化字 彝語/彝语 test číslo € černý Češký';
    
    $cjk_scripts = 'Bopomofo|Han|Hiragana|Katakana';
    $cjk_scripts = preg_replace('/[a-zA-Z_]+/', '\\p{$0}', $cjk_scripts);
    
    // wrap the CJK characters into a span with it's own font
    $body = preg_replace("/($cjk_scripts)+/isu", '<span class="cjk">$0</span>', $body);
    
    // a font that supports CJK characters
    $cjk_font_path = APP_PATH.'/fonts/DroidSansFallbackFull.ttf';
    
    $html = <<<HTML
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <style type="text/css">
    @font-face {
        font-family: 'DroidSansFallbackFull';
        font-style: normal;
        font-weight: 400;
        src: url('$cjk_font_path') format('truetype');
    }
    body {
        font-family: DejaVu Sans, sans-serif;;
    }
    .cjk {
        font-family: DroidSansFallbackFull, sans-serif;
    }
    </style>
    </head>
    <body>$body</body>
    </html>
    HTML;
    
    $dompdf = new \DOMPDF();
    $dompdf->set_paper('A4');
    $dompdf->load_html($html);
    $dompdf->render();
    
    $dompdf->stream('test.pdf', ['Attachment'=>0]);
    

    Related: https://github.com/dompdf/dompdf/issues/1508

    0 讨论(0)
  • 2020-12-01 21:44

    Chinese characters are causing problems sometimes. The important part is to have good font here is a list you can download.

    I chose first named "Kai Bold Font" here is a download page

    Then put it on your hosting service in a public folder. I put it into

    http://192.168.10.10/fonts/pdf/wts11.ttf
    

    and here is my html example

    $html = <<<EOT
    <!DOCTYPE html>
    <html>
    <head>
       <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
       <style>
        @font-face {
          font-family: chinese;
            src: url('http://192.168.10.10/fonts/pdf/wts11.ttf') format('truetype');
        }
        .chineseLanguage { font-family: chinese; }
          body {font-family: DejaVu Sans, sans-serif;}
       </style>
    </head>
    <body>
        Chinese
        <div class='chineseLanguage'>
            忠烈祠
            中文 - 这工作<br> 
        </div>
        hello world <br> 
        Russian - русский текст <br>
        Greek - α,β,γ,δ,ε <br>
        chars - !@#$%^&* -=- €   <br><br>
        <br>
        Hebrew (iw)<br><br>
        דג סקרן שט בים מאוכזב ולפתע מצא לו חברה איך הקליטה<br>
        <br>    
    </body>
    </html>
    EOT;
    

    PS. there is a little chance you might need this set:

    ini_set("allow_url_fopen", true);
    
    0 讨论(0)
提交回复
热议问题