Generating PDF document with unicode characters stored in the database

后端 未结 1 1255
渐次进展
渐次进展 2021-01-29 05:26

I want to generate PDF document with unicode characters. I stored the using utf8_unicode_ci in the db.

Here is my table:

language(word_id,en         


        
相关标签:
1条回答
  • 2021-01-29 05:59

    First some general advice ...

    In addition to storing your data as UTF8 you need to ensure your database connection is in UTF8. How you do this depends on your data access library. I can't quite tell what data access library you're using, but I see some classic mysql functions. If that's what your using you would use the following just after connecting to your database:

    mysql_query("SET NAMES 'utf8'");
    

    You also should ensure that PHP is working natively in UTF8. There are two things here that you will need. First, dompdf requires the MBString extension for handling multi-byte characters correctly. Second, you probably want to tell PHP to treat your character data as UTF8 with the following code:

    mb_internal_encoding('UTF-8');
    

    Finally, in order to display characters that fall outside the Windows ANSI character set in a PDF you need a font that supports those characters. dompdf v0.6.x include the DejaVu fonts by default, but those do not support tamil and so you will have to load a font into dompdf. The easiest way to do this is using @font-face. You should read over the dompdf Unicode How-To (it's a bit out of date, but still has useful info). Then check out this answer to the question "dompdf and set different font-family".


    Now some specific advice ...

    1) Use UTF8 all the way through. You are working with a UTF8-based character set and you should leave it in that character set. Older versions of dompdf (0.5.x and earlier) only understood Windows-ANSI. Newer versions work natively in UTF8 and even if you're not using any "special" characters UTF8 is the preferred document encoding.

    2) Don't convert from UTF8 to a lesser encoding. By lesser encoding I mean converting from an inclusive encoding like UTF8 which supports a large character set to a limited encoding like iso-8859-x or Windows-12XX. Again, if the target encoding does not support your characters you will lose information. You converted your document string from UTF8 to Windows-1250. Does that encoding even support the characters you're using?

    3) Your document should always specify the correct encoding. You specify in your document meta tag that the document is encoded in UTF8 and so dompdf will assume that's the appropriate encoding to use. If you convert to another encoding your characters may not be represented correctly.

    4) As indicated above, you need a font that supports the characters used in your document. You do not specify any font at all and so a PDF core font will be used. These fonts only support text encoded with Windows ANSI. Read this post on the dompdf issue tracker about displaying Tamil characters: https://github.com/dompdf/dompdf/issues/838#issuecomment-47415806

    With the above in mind your code should look more like this:

    include_once 'common/dompdf/dompdf_config.inc.php';
    $date=date("Y/m/d");
    $value=  mysql_fetch_assoc($result);
    
    $html = '
      <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <style>
        @font-face {
          font-family: latha;
          font-style: normal;
          font-weight: 400;
          src: url(http://yourfontprovider.com/latha.ttf) format("true-type");
        }
        </style>
      </head>
      <body>
        Word Details<br/>
        <div style="float:left;width:96%">
        <table border="0" width="100%">
          <tr>
            <th>English Word : </th>
            <td><input type="text" name="enhlish" value="'.$value['english'].'"/></td>
          </tr>
          <tr>
            <td colspan="2"><hr/></td>
          </tr>
          <tr>
            <th>Tamli Word : </th>
            <td><input type="text" name="tamli" value="'.$value['tamil'].'" style="font-family: latha, sans-serif;" /></td>
          </tr>
        </table>
      </body>
      </html>
    ';
    
    $dompdf = new DOMPDF();
    $dompdf->load_html($html,'UTF-8');
    $dompdf->render();
    $dompdf->stream("dompdf_out.pdf", array("Attachment" => false));
    
    0 讨论(0)
提交回复
热议问题