dompdf character encoding UTF-8

匿名 (未验证) 提交于 2019-12-03 02:31:01

问题:

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 the html I looks like ok

But when I look into the pdf I see this

Here is my all code:

What Im doing wrong? I tried many many options which I found :( Any idea?

回答1:

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):



回答2:

I got UTF-8 characters working with this combination. Before you pass html to DOMpdf, make encoding covert with this:

$html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'); 

Use DejaVu font in your css

*{ font-family: DejaVu Sans; font-size: 12px;} 

Make sure you have set utf-8 encoding in HTML tag



回答3:

Only Add

  

before It is working for me.



回答4:

Nothing out of mentioned answers helped me. After hours of struggle I switched to niklasravnsborg/laravel-pdf has nearly exactly the same syntax and usage, and everything is working allright.



回答5:

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

just like

     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



回答6:

I had similar problem and ended up using tcpdf.Hope this could be helpful. http://www.tcpdf.org/
Problem was the font i was using.I was able to get the correct output using this font 'freeserif'.I guess it might be possible to get the same output using this font with dompdf.

$pdf->SetFont('freeserif', '', 12); 

Here is the sample i have used. tcpdf utf-8 sample



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!