Hindi Font with DOMPDF in Codeigniter

删除回忆录丶 提交于 2019-12-02 11:16:30

问题


I have used DOMPDF library in codeigniter to fetch data from mysql. But there are some fields in my table is in Hindi (unicode form of Hindi) when I am trying to generate PDF for hindi fonts it is not displaying correct thing. Please help me to get out from this.

Here is my controller

public function genrate_pdf($benef_detial,$count){
  $data['repo_result'] = $benef_detial;
  $data['total_benif'] = $count;
  $data['page_title'] = "Benificiary Details";
  $this->load->library('pdf');  
  $html = utf8_decode($this->pdf->load_view('public_reports/pdf_view',$data));
  $dompdf = new DOMPDF();
  $dompdf->load_html($html);
  $dompdf->set_paper("a4", "landscape" );
  $this->template->set_template('default');
  $this->pdf->render();
  $this->pdf->stream("public_reports/pdf_view.pdf"); }

I have added the meta tag

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

in pdf_view and in template also

If I make the table field data in english then is working perfactly but in case of hindi unicode it showing result like

सिंगरà¥OEली

Please Help me.

Thanks


回答1:


In order to correctly render Hindi characters in the PDF you'll need to check a few things:

1. Make sure PHP is using MBString (enabled by default)

2. Make sure you are using Dompdf 0.6.0 or higher

3. Check your settings

If using Dompdf 0.6.x set DOMPDF_ENABLE_UNICODE to true. Dompdf 0.7.0 and higher have this enabled by default.

You will need read/write access to the font directory and font cache directory. By default these directories are under the dompdf directory (dompdf/lib/fonts for both). In Dompdf 0.6.x that would be the DOMPDF_FONT_DIR and DOMPDF_FONT_CACHE configuration constants. In Dompdf 0.7.0 and higher be sure to set the value of the fontDir and fontCache options (e.g. $dompdf->set_option("fontDir", "/my/font/dir");).

If loading a font via @font-face make sure you also have read/write access to the temporary directory. In Dompdf 0.6.0 check the DOMPDF_TEMP_DIR configuration constant. In Dompdf 0.7.0 and higher set the tempDir option.

Also, generally speaking, if you're loading any resources via HTTP make sure you have remote resource loading enabled. In Dompdf 0.6.0 check the DOMPDF_ENABLE_REMOTE configuration constant. In Dompdf 0.7.0 and higher set the isRemoteEnabled option.

4. Load a supporting font into Dompdf

Easiest and recommended method for loading a font is using the @font-face CSS declaration.

By far the easiest way to get started would be to use Google fonts since all you have to do is link to the stylesheet. For the Hind font you would just add the following to your document head:

<link href="https://fonts.googleapis.com/css?family=Hind:400,700&amp;subset=devanagari,latin-ext" rel="stylesheet">

Unfortunately Dompdf does not support numeric weights for fonts. Since this is how Google defines the font weight you can't really use this with Dompdf up to and including 0.8.1. You can hack around the issue, but the easiest method is to download the fonts.

In your document stylesheet:

@font-face {
  font-family: Hind;
  font-style: normal;
  font-weight: normal;
  src: url(http://example.com/hind.ttf) format('truetype');
}

Note: Don't forget to define the various weights and styles you want to support.

5. Set the font-family style attribute for your text to the font you loaded

In your document stylesheet style the relevant container with the appropriate font family. For example, if you want the entire document to use the font you could just add the following to your document stylesheet:

* { 
  font-family: Hind, DejaVu Sans, sans-serif;
}

This is all covered in the Unicode How-to.



来源:https://stackoverflow.com/questions/18183558/hindi-font-with-dompdf-in-codeigniter

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