dompdf character encoding UTF-8

后端 未结 11 2024
我在风中等你
我在风中等你 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:20

    Only Add

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

    before </head> It is working for me.

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

    I had the same problem and I solved it very simple. Just import google fonts with required language subset in your CSS file which is used when generating HTML. Specify utf-8 in your HTML file and it's working...

    @import url('https://fonts.googleapis.com/css?family=Roboto:400,700&subset=latin-ext');
    body {font-family: 'Roboto', sans-serif;}
    
    0 讨论(0)
  • 2020-12-01 21:25

    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

    <?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');
    
    ?>
    
    0 讨论(0)
  • 2020-12-01 21:26

    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.

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

    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 <head> tag

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

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

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

    utf8_decode() did the trick for me with some German translations like ä and ü.

    echo utf8_decode('X Ponuka číslo € černý Češký <br>');
    
    0 讨论(0)
提交回复
热议问题