dompdf character encoding UTF-8

后端 未结 11 2063
我在风中等你
我在风中等你 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: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", '$0', $body);
    
    // a font that supports CJK characters
    $cjk_font_path = APP_PATH.'/fonts/DroidSansFallbackFull.ttf';
    
    $html = <<
    
    
    
    
    
    $body
    
    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

提交回复
热议问题