简体   繁体   English

Dompdf-Laravel Unicode 字符无法正确呈现

[英]Dompdf-Laravel Unicode character not rendering correctly

I am using barryvdh/laravel-dompdf to load PDF's in my laravel application.我正在使用barryvdh/laravel-dompdf在我的 laravel 应用程序中加载 PDF。 I need to load custom unicode fonts(sinhala) in to the pdf.我需要将自定义unicode 字体(僧伽罗语)加载到 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 .为此,我下载了我的外部 fonts 并复制了字体文件夹,然后通过load_font.php将其加载到storage/fonts目录。 I have used css in my blade file as follows:我在刀片文件中使用了 css,如下所示:

@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我还设置了元标记 UTF-8

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

HTML part: HTML 部分:

<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: 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.当我正常加载我的 html 页面时,字体工作正常。 However, when I am trying to load the font in the PDF, the font does not display correctly.但是,当我尝试在 PDF 中加载字体时,字体无法正确显示。 Could somebody please guide me on how to load the font correctly.有人可以指导我如何正确加载字体。

On a normal page(HTML view), it shows as follows:在普通页面(HTML 视图)上,它显示如下:

HTML view - correct text HTML 视图 - 正确的文本

When I generate the PDF, it shows as follows:当我生成PDF时,显示如下:

PDF view - incorrect text PDF 视图 - 不正确的文本

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.问题是 PDF 上的僧伽罗文本显示不正确,但是当我将其复制并粘贴到某处时,文本正确显示。

Any ideas on how I can solve this issue?关于如何解决这个问题的任何想法? Please help..请帮忙..

We found a solution for that with Mpdf , we changed library to Mpdf .我们使用Mpdf找到了解决方案,我们将库更改为Mpdf The Mpdf has much Unicode fonts and also include Sinhala Unicode font - ( Kaputaunicode ). Mpdf有很多Unicode fonts并且还包括 僧伽罗 Unicode 字体- ( 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 . 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.但是fonts渲染不完美,一些复杂的组合渲染不正确。

  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.无论如何,在我的情况下, Mpdf文本渲染比Dompdf好。

The CSS Styling different with comparing Dompdf. CSS 样式与比较 Dompdf 不同。
And Mpdf rendering time less than Dompdf并且 Mpdf 的渲染时间比 Dompdf 少

In Controller:在 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 laravel 的更好更简单的 package 是vatttan/apdf ,将由

composer require vatttan/apdf

See This in github请参阅github

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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