简体   繁体   中英

Dompdf-Laravel Unicode character not rendering correctly

I am using barryvdh/laravel-dompdf to load PDF's in my laravel application. I need to load custom unicode fonts(sinhala) in to the pdf. To do this, I downloaded my external fonts and copied the font folder and then it load to storage/fonts directory by load_font.php . I have used css in my blade file as follows:

@font-face {
    font-family: Noto-2;
    font-display: block;
    src: url({{ asset('fonts/NotoSansSinhala-Light.ttf') }}) format("truetype");
}
body {
    font-family: Noto-2 !important;
    font-style: normal;
    font-weight: 500;
    font-size: 14px!important;
}

I also set meta tag UTF-8

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

HTML part:

<td class="td_cus" rowspan="5">
   <span class="title">මික් ආහාර සේවය</span>
   <p class="company_address">
      2 වැනි මහළ,<br>
      <span style="font-family: helvetica !important;">M I C </span>සුපර් සෙන්ටර්,<br>
      මොරටුව,<br>
      ශ්‍රී ලංකාව.
   </p>
</td>

Laravel controller:

 $path = Storage::disk('local')->path("letters(".$letterCount."-letters).pdf");
 $pdf = PDF::loadview('templates.letterSinhala', ['details' => $details])
        ->setPaper('a4', 'portrait')
        ->save($path);

When I load my html page normally, the font works perfectly. However, when I am trying to load the font in the PDF, the font does not display correctly. Could somebody please guide me on how to load the font correctly.

On a normal page(HTML view), it shows as follows:

HTML view - correct text

When I generate the PDF, it shows as follows:

PDF view - incorrect text

NOTE: Please see upper two images, that can be easy to understand the issue;

The problem is sinhala text displaying incorrectly on PDF, but when i copy & paste that somewhere, the text show in correctly.

Any ideas on how I can solve this issue? Please help..

We found a solution for that with Mpdf , we changed library to Mpdf . The Mpdf has much Unicode fonts and also include Sinhala Unicode font - ( Kaputaunicode ).

In sinhala case you can use only Kaputaunicode font, because other sinhala unicode fonts doesn't support with Mpdf , other sinhala unicode fonts rendering same as Dompdf .

But fonts rendering not perfect, some complex combinations not rendering correctly.

  1. Text diff. image
  2. Paragraph text
  • you can get idea from this images

Anyway Mpdf text rendering is good than Dompdf in my case.

The CSS Styling different with comparing Dompdf.
And Mpdf rendering time less than Dompdf

In Controller:

use Mpdf\Mpdf;

$path = Storage::disk('local')->path("_letters(".$letterCount."-letters).pdf");
$view = View::make('templates.letterSinhala', ['details' => $details]);
$html = $view->render();
$mpdf = new Mpdf(['mode' => 'UTF-8', 'format' => 'A4-P', 'autoScriptToLang' => true, 'autoLangToFont' => true]);
$mpdf->WriteHTML($html);
$mpdf->Output($path);

Better and more simple package for laravel is vatttan/apdf that will be install by

composer require vatttan/apdf

See This in github

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM