Sunday, May 21, 2017

"Unable to display chinese character in dompdf laravel 5.3" FIXED!

Tags



I spent quite some time to discover the problem of this and the solution is so simple and make me look silly.

The reason why you unable to display chinese word is because dompdf default font not support chinese. Yes include that devaju font!

The font that i found support these chinese character is firefly sung. There might be other fonts that support chinese fonts too, you just have to google it. :)

So in order to include custom fonts into your dompdf, you have to follow these steps.

1) Download load_font.php and place in your project root. load_font.php
2) Next, edit load_font.php





  1. download load_font.php and place in ur project root
  2. Edit load_font.php
  • Change the line require_once "autoload.inc.php";
    to require_once "vendor/autoload.php"
  • Add $fontDir = "storage/fonts"; Create a folder named fonts in your laravel storage folder.
  1. Download your desire chinese supported font and place at your project root. Here one of the website you can download chinese font. Cooltext
  2.  Open Cmd. Navigate to your laravel project and run this command. php load_font.php xxxfont xxxfont.ttf
    eg: you have download simsun.ttf you will do php load_font.php simsun simsun.ttf then you font will be installed to storage/fonts directory. 
  3. The syntax would accept until 4 type of fonts. (Normal, Bold, Italic, Bold-Italic) . If you want to specify font for each of them, you can type php load_font.php xxxfont xxxfont.ttf xxxfont.ttf xxxfont.ttf xxxfont.ttf
  4. Change your fonts using style in html and you should be good to go. 
Thanks for watching my blog. Do follow me on google+ or like my facebook page for more! :)
Obstrum Facebook Page
 

1 comments so far

I have some errors when running the command php.....
errors are as follows:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\User>cd C:\xampp\htdocs\pdffile

C:\xampp\htdocs\pdffile>php load_font.php myanmar3 myanmar3.ttf
PHP Fatal error: Uncaught Dompdf\Exception: Unable to read 'myanmar3.ttf'. in C
:\xampp\htdocs\pdffile\load_font.php:68
Stack trace:
#0 C:\xampp\htdocs\pdffile\load_font.php(169): install_font_family(Object(Dompdf
\Dompdf), 'myanmar3', 'myanmar3.ttf')
#1 {main}
thrown in C:\xampp\htdocs\pdffile\load_font.php on line 68

Fatal error: Uncaught Dompdf\Exception: Unable to read 'myanmar3.ttf'. in C:\xam
pp\htdocs\pdffile\load_font.php:68
Stack trace:
#0 C:\xampp\htdocs\pdffile\load_font.php(169): install_font_family(Object(Dompdf
\Dompdf), 'myanmar3', 'myanmar3.ttf')
#1 {main}
thrown in C:\xampp\htdocs\pdffile\load_font.php on line 68

C:\xampp\htdocs\pdffile>


EmoticonEmoticon