barryvdh / laravel-dompdf

A DOMPDF Wrapper for Laravel
MIT License
6.72k stars 966 forks source link

Encoding error for displaying of chinese characters #79

Closed bczm8703 closed 4 years ago

bczm8703 commented 9 years ago

hi. Firstly thanks for the awesome package, saved me alot of time for rendering html into pdf.

when i use the package to render pdf everything is working perfectly.. the only issue i have is that the chinese characters are being replaced with white spaces. need help on this.

i have already added the utf-8 meta tag in my template file already but it is still not working

barryvdh commented 9 years ago

Seems like encoding issue. Did you do this? https://github.com/barryvdh/laravel-dompdf#tip-utf-8-support

bczm8703 commented 9 years ago

yes. i have done that

ghost commented 9 years ago

You can try fonts support Chinese. I have been same issue using Turkish Language..

I talk about dompdf's AFM fonts..

aasthemes commented 9 years ago

@MURATSPLAT how to change dompdf font? i have same trouble display chinese characters too, please detail

ghost commented 9 years ago

Here is details :

https://code.google.com/p/dompdf/wiki/CPDFUnicode

Firstly You need Chinese AFM fonts, You can find fonts supports Chinese or create them by using some tools..

And than copy the fonts to into app/storage/fonts, if it is not existed, create fonts directory.

Other step is you should add defining the fonts to /vendor/dompdf/dompdf/lib/fonts/dompdf_font_family_cache.dist.php. I haven't could found a solution without editing dompdf native files.

Like that

// OpenSans-Regular fonts that I have created..
///vendor/dompdf/dompdf/lib/fonts/dompdf_font_family_cache.dist.php
 'opensans-regular' => 
    array (
      'normal' => DOMPDF_FONT_DIR . 'OpenSans-Regular',
      'bold' => DOMPDF_FONT_DIR . 'OpenSans-Bold',
      'italic' => DOMPDF_FONT_DIR . 'OpenSans-Italic',
      'bold_italic' => DOMPDF_FONT_DIR . 'OpenSans-BoldItalic',
  ),

Finally you should change app/config/packages/barryvdh/laravel-dompdf/config.php file with new font name

In our example:

//app/config/packages/barryvdh/laravel-dompdf/config.php
 /**
         * The default font family
         *
         * Used if no suitable fonts can be found. This must exist in the font folder.
         * @var string
         */
        "DOMPDF_DEFAULT_FONT" => "opensans-regular",

That's all..

raykwok commented 9 years ago

I had the same error, and not fixed.

anandselvarasu commented 9 years ago

I have question mark instead of Chinese language. Please help me any one.

chaiwei commented 7 years ago

Encounter similar issue as above: Chinese Character display as white space/white color font/square bracket,

After few hours trying, I have manage to get Chinese font to work with Laravel dompdf. Reference: https://github.com/dompdf/dompdf/wiki/UnicodeHowTo

laravel version: 5.3

  1. Look for your preference fonts
  2. Download the load_font.php
  3. Upload the load_font.php and the ChineseFontName.ttf to laravel folder (I uploaded to my laravel root directory)
  4. Create a folder in laravel/storage/fonts
  5. Edit load_font.php (point the require_once to your laravel vendor autoload.php and set the font directory, for my case it is as below)
<?php
// 1. [Required] Point to the composer or dompdf autoloader
- require_once "autoload.inc.php";
+ require_once "vendor/autoload.php";

// 2. [Optional] Set the path to your font directory
//    By default dopmdf loads fonts to dompdf/lib/fonts
//    If you have modified your font directory set this
//    variable appropriately.
//$fontDir = "lib/fonts";
+ $fontDir = "storage/fonts";

Go to terminal/putty and navigate to the directory where you upload the load_font.php earlier. Type below command

php load_font.php "Font Name" ChineseFontName.ttf

example below output

cw@ubuntu:/var/www/laravel# php load_font.php "Font Name" ChineseFontName.ttf
Unable to find bold face file.
Unable to find italic face file.
Unable to find bold_italic face file.
Copying ChineseFontName.ttf to storage/fonts/ChineseFontName.ttf...
Generating Adobe Font Metrics for storage/fonts/ChineseFontName...

Also I checked on my storage/fonts directory and found 3 files has been created.

dompdf_font_family_cache.php -> contains all the font name and the font location path
ChineseFontName.ttf
ChineseFontName.ufm

6 Edit my html code and directly set

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>body { font-family:  'chinese font name'; } </style>

The chinese font is now display properly in my pdf..

7 Delete the load_font.php and the font.ttf that I uploaded to my laravel root directory earlier.

ltrtuan commented 7 years ago

Unfortunately, i practice step by step as @chaiwei but it is not worked. I downloaded Roboto ttf format and run load_font.php, the code append 'roboto' => array( 'normal' => $fontDir . '/Roboto-Medium', 'bold' => $fontDir . '/Roboto-Medium', 'italic' => $fontDir . '/Roboto-Medium', 'bold_italic' => $fontDir . '/Roboto-Medium', ), but when i include the font into view blade file body { font-family: 'roboto', sans-serif; } And view pdf file, text is not shown

nagihiko93 commented 7 years ago

Tried @chaiwei suggestion but still ain't working. @ltrtuan how did it went for you?

tinfot commented 7 years ago

@chaiwei Thanks guy, I fixed Chinese characters problem.

cloudinskywith commented 6 years ago

thanks @chaiwei,it work for me on laravel5.6.

I use simhei.ttf

  1. from https://github.com/dompdf/utils download load_font.php; 2.copy simhei.ttf and load_font.php to laravel project root folder; 3.create a foloder name fonts in storage/fonts; 4.add code to line 10 $fontDir = 'storage/fonts'; 5.in terminal,run: php load_font.php "simhei" simhei.ttf 6.now it works.
bernierayxu commented 6 years ago

I had the same problem and followed the above instructions.

Please note that you might need an .afm version of the font. Go convert ttf to afm online and download and extract. Copy the file into storage/fonts. Everything should work.

AND NOTICE THAT:

YOUR PDF MAY NEED 10 SECONDS TO SHOW ITS CONTENT.

If it's blank at first glance, just wait. Should you follow the above instructions, wait until you see characters.

BTW, I work on mac. Not sure if the waiting issue happens on other computers or systems.

nicole929chan commented 5 years ago

Thank you, @chaiwei , it works well on Windows. But on Linux, It seems to need more. $dompdf = new Dompdf; $dompdf->set_option('fontDir', storage_path('fonts')); // let dompdft know where the wt001.ttf is

blade file also needs to know the path of wt001.ttf @font-face { font-family: 'wt001'; font-style: normal; src: url({{ storage_path('fonts/wt001.ttf') }}) format('truetype'); }

Maquilaweb commented 5 years ago

@ nicole929chan hello !, I have the same problems of the characters and I have it in windows, can you help me? telling me if you just uploaded the font wt001.ttf and adding storage_path ('fonts') in the file you're going to print? for example I am in the root, and there I have the folder dompdf and in another I have the PDF file that is going to print, but I get an error with $dompdf->set_option

nicole929chan commented 5 years ago

@Maquilaweb I did the steps which mentioned by chaiwei and two items what I said above. It works fine on Windows and Linux. Furthermore, storage link. Those files, wt001.ttf, wt001.ufm, dompda_font_family_cache.php, are located at storage/fonts.

goforwork commented 5 years ago

Encounter similar issue as above: Chinese Character display as white space/white color font/square bracket,

After few hours trying, I have manage to get Chinese font to work with Laravel dompdf. Reference: https://github.com/dompdf/dompdf/wiki/UnicodeHowTo

laravel version: 5.3

  1. Look for your preference fonts
  2. Download the load_font.php
  3. Upload the load_font.php and the ChineseFontName.ttf to laravel folder (I uploaded to my laravel root directory)
  4. Create a folder in laravel/storage/fonts
  5. Edit load_font.php (point the require_once to your laravel vendor autoload.php and set the font directory, for my case it is as below)
<?php
// 1. [Required] Point to the composer or dompdf autoloader
- require_once "autoload.inc.php";
+ require_once "vendor/autoload.php";

// 2. [Optional] Set the path to your font directory
//    By default dopmdf loads fonts to dompdf/lib/fonts
//    If you have modified your font directory set this
//    variable appropriately.
//$fontDir = "lib/fonts";
+ $fontDir = "storage/fonts";

Go to terminal/putty and navigate to the directory where you upload the load_font.php earlier. Type below command

php load_font.php "Font Name" ChineseFontName.ttf

example below output

cw@ubuntu:/var/www/laravel# php load_font.php "Font Name" ChineseFontName.ttf
Unable to find bold face file.
Unable to find italic face file.
Unable to find bold_italic face file.
Copying ChineseFontName.ttf to storage/fonts/ChineseFontName.ttf...
Generating Adobe Font Metrics for storage/fonts/ChineseFontName...

Also I checked on my storage/fonts directory and found 3 files has been created.

dompdf_font_family_cache.php -> contains all the font name and the font location path
ChineseFontName.ttf
ChineseFontName.ufm

6 Edit my html code and directly set

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>body { font-family:  'chinese font name'; } </style>

The chinese font is now display properly in my pdf..

7 Delete the load_font.php and the font.ttf that I uploaded to my laravel root directory earlier.

thanks a lot

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Any issues with PDF rendering itself that are not directly related to this package, should be reported on https://github.com/dompdf/dompdf instead. When having doubts, please try to reproduce the issue with just dompdf. If you believe this is an actual issue with the latest version of laravel-dompdf, please reply to this issue so we can investigate further. Thank you for your contribution! Apologies for any delayed response on our side.