Dompdf character encoding UTF-8

I am trying to create a pdf with the correct characters, but is there ?? characters. I created a test php file where Im trying to hide the best solution. If Im open html in browser, then I look like ok

UTF-8 --> UTF-8 : X Ponuka číslo € černý Češký 

But when I look in pdf, I see it

 UTF-8 --> UTF-8 : X Ponuka ?íslo € ?erný ?ešký 

Here is my code:

 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>č s š Š</title> </head> <body> <?php require_once("dompdf/dompdf_config.inc.php"); $tab = array("UTF-8", "ASCII", "Windows-1250", "ISO-8859-2", "ISO-8859-1", "ISO-8859-6", "CP1256"); $chain = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <style></style><title>č s š Š</title></head><body>'; foreach ($tab as $i) { foreach ($tab as $j) { $chain .= "<br> $i --> $j : ".iconv($i, $j, 'X Ponuka číslo € černý Češký <br>'); } } $chain .= '<p style="font-family: firefly, verdana, sans-serif;">??????X Ponuka číslo € černý Češký <br></p></body></html>'; echo $chain; echo 'X Ponuka číslo € černý Češký <br>'; $filename = 'pdf/_1.pdf'; $dompdf = new DOMPDF(); $dompdf->load_html($chain, 'UTF-8'); $dompdf->set_paper('a4', 'portrait'); // change these if you need to $dompdf->render(); file_put_contents($filename, $dompdf->output()); ?> </body> </html> 

What am I doing wrong? I tried many options that I found :( Any idea?

+13
source share
11 answers

You should read Unicode How-to again. The main problem is that you are not specifying a font that supports your characters. It looks like you read the instructions because you are using the sample font from this document. However, this example is not intended to be applied globally to any document; dompdf does not include firefly (Chinese character font) or Verdana by default.

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

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

 <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> 
+30
source

I have UTF-8 characters working with this combination. Before passing html to DOMpdf, make the encoding hidden:

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

Use the DejaVu font in your CSS

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

Make sure you set utf-8 encoding in the HTML <head>

 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

Now all special characters work "ľ š č ť ž ý á í é"

+25
source

Add only

  <style> *{ font-family: DejaVu Sans !important;} </style> 

up to </head> It works for me.

+9
source

utf8_decode () helped me with some German translations like ä and ü.

 echo utf8_decode('X Ponuka číslo € černý Češký <br>'); 
+2
source

None of the answers mentioned helped me. After several hours of struggle, I switched to niklasravnsborg / laravel-pdf, it has almost the same syntax and usage, and everything works fine.

+1
source

Dompdf does not support backup fonts, so you cannot use your favorite font if it does not support your characters, and you also cannot set another font as a backup font for characters such as droid sans fallback .

Instead, you can take advantage of the Unicode regex script ranges: https://www.regular-expressions.info/unicode.html to wrap these blocks of text in spaces and give them a spare 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 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

+1
source

If you do not mind having only one encoding, you can change each encoding in dompdf_font_family_cache.dist.php

as

 <?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 this is not the best way, but it saves a lot of time

0
source

Chinese characters sometimes cause problems. It's important to have a good font, here is a list that you can download.

I chose the first name "Kai Bold Font", here is the download page

Then put it on your hosting in a shared folder. I put it in

 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 small chance that you may need this kit:

 ini_set("allow_url_fopen", true); 
0
source

I had the same problem and solved it very simply. Just import the Google fonts with the required language subset into your CSS file, which is used when creating HTML. Point utf-8 in your HTML file and it works ...

 @import url('https://fonts.googleapis.com/css?family=Roboto:400,700&subset=latin-ext'); body {font-family: 'Roboto', sans-serif;} 
0
source
0
source

I had a similar problem and ended up using tcpdf . Hope this can be helpful. http://www.tcpdf.org/ The problem was in the font that I used. I was able to get the correct result using this "freeserif" font. Perhaps you can get the same result using this font with dompdf .

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

Here is an example that I used. tcpdf utf-8 sample

 <?php header('Content-type: text/html; charset=UTF-8') ;//chrome require_once('tcpdf_include.php'); // create new PDF document $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->setFontSubsetting(true); $pdf->SetFont('freeserif', '', 12); $pdf->AddPage(); $utf8text = ' <html><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body> <b>Ponuka číslo € černý Češký </b></br> සිංහල </br> <u>தேமல </u> </br> </body></html>'; $pdf->SetTextColor(0, 63, 127); $pdf->writeHTML($utf8text, true, 0, true, true); $pdf->Output('example_008.pdf', 'I'); ?> 
-one
source

All Articles