Hindi Font with DOMPDF in Codeigniter

后端 未结 2 2085
[愿得一人]
[愿得一人] 2021-01-26 20:25

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 fo

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-26 21:06

    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:

    
    

    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.

提交回复
热议问题