Codeigniter中带有DOMPDF的北印度语字体 [英] Hindi Font with DOMPDF in Codeigniter

查看:114
本文介绍了Codeigniter中带有DOMPDF的北印度语字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在codeigniter中使用了DOMPDF库来从mysql获取数据.但是当我尝试为印地语字体生成PDF时,表中的某些字段为印地语(印地语的unicode形式),但未显示正确的内容.请帮助我摆脱困境.

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.

这是我的控制人

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"); }

我已经添加了元标记

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

在pdf_view和模板中

in pdf_view and in template also

如果我用英语制作表格字段数据,则可以正常工作,但是在印地语unicode的情况下,其显示结果如下

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

सिंगरà¥OEली

सिंगरà¥OEली

请帮助我.

谢谢

推荐答案

为了在PDF中正确呈现印地语字符,您需要检查以下几件事:

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

1.确保PHP使用的是MBString(默认情况下启用)

2.确保您使用的是Dompdf 0.6.0或更高版本

3.检查您的设置

如果使用Dompdf 0.6.x,请将DOMPDF_ENABLE_UNICODE设置为true. Dompdf 0.7.0和更高版本默认情况下启用了此功能.

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

您将需要对字体目录和字体缓存目录的读/写访问权限.默认情况下,这些目录位于dompdf目录下(两个目录均为dompdf/lib/fonts).在Dompdf 0.6.x中,这将是DOMPDF_FONT_DIR和DOMPDF_FONT_CACHE配置常量.在Dompdf 0.7.0及更高版本中,请确保设置fontDir和fontCache选项的值(例如$dompdf->set_option("fontDir", "/my/font/dir");).

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");).

如果通过@font-face加载字体,请确保您还具有对临时目录的读/写权限.在Dompdf 0.6.0中,检查DOMPDF_TEMP_DIR配置常量.在Dompdf 0.7.0及更高版本中,设置tempDir选项.

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.

一般来说,如果要通过HTTP加载任何资源,请确保已启用远程资源加载.在Dompdf 0.6.0中,检查DOMPDF_ENABLE_REMOTE配置常量.在Dompdf 0.7.0及更高版本中,设置isRemoteEnabled选项.

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.将支持字体加载到Dompdf

最简单和推荐的字体加载方法是使用@font-face CSS声明.

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

到目前为止,最简单的入门方法是使用Google字体,因为您要做的就是链接到样式表.对于查找字体您只需将以下内容添加到文档头:

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">

不幸的是, Dompdf不支持字体的数字权重.由于这是Google定义字体粗细的方式,因此您无法真正将Dompdf包括在0.8.1(含)以内.您可以解决这个问题,但是最简单的方法是下载字体.

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.

在文档样式表中:

@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.将文本的font-family样式属性设置为您加载的字体

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;
}


Unicode操作方法中对此进行了介绍.


This is all covered in the Unicode How-to.

这篇关于Codeigniter中带有DOMPDF的北印度语字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆