Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

laravel - Dompdf problem with displaying Nepali character

I am using Dompdf to create a pdf for my Nepali content. I have written following code in my controller:

public function generatePDF(Request $request){
  $data = $this->news->where('slug', $request->slug)->first();
  $html = "<!DOCTYPE html>
  <html>
  <head>
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
    <meta charset='UTF-8'>
      <title>". $data->title ."</title>
      <style>
      @font-face {
        font-family: preeti;
        font-style: normal;
        font-weight: 400;
        src: url('{{ asset('/frontend/fonts/preeti.ttf') }}') format('truetype');
      }
      </style>
  </head>
  <body style='font-family: preeti;'>
      <img src='".$data->image ."' style='width: 100%; height: auto'>
      ". $data->description ."
  </body>
  </html>";
  $html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
  $pdf = PDF::setOptions(['dpi' => 150, 'defaultFont' => 'preeti']);
  $pdf = PDF::loadHTML($html);      
  return $pdf->download('sample.pdf');
}

But this gives ???? as an output of the Nepali content inside the canvas. However, <title>". $data->title ."</title> displays right character. What can I do to fix this?

As you can see I have kept UTF-8 in my meta also have included CSS for the font but it does not seem to work. I have also included mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'); but it made no change. Does anyone has fixed this kind of issue?

The output of $data->description is

<p>????? ????????????? ????? ????? ??????? ????? ????????? ??????? ??? ????? ? 

?????????????? ??? ? ??, ??? ????? ??????????? ???????? ?? ???????? ??? ?
<p>??????? ?????? ????? ?????, ???? ????? ?&ndash;????? ???????? ?????? ????? ?? ? ??????? ?? ???????????? ????? ?????? ????????? ?? ??????? ?????? ? ??? ??? ?? ?
<p>??????? ????? ????? ??? ?????? ?????? ?, ???? ??? ????? ????? ?????? ????? ???????? ? ????????? ????? ???? ?????? ??????? ???? ??????????? ??????? ????? ? ?? ?
<p>????????? ??? ???? ????????? ??? ? ??????? ??????? ??????? ????? ???????? ?????? ????????????? ?????? ???????? ???, ?????? ? ?????? ?????? ???? ?????? ?????? ?
<p><strong>???????? ????? ??????? ????</strong><br />????????? ??????????? ????? ????? ????????? ??????? ??????????? ? ????????? ??????? ???? ??? ? ????? ?????? ?
<p>????? ?????? ??? ??? ??????? ?? ???? ?????? ? ? ?????? ??? ???????? ???????? ? ????????? ???????? ??? ????; ???? ??? ???????? ?????? ????? ?????? ? ???? ???? ?
<p>??? ???, ?????????? ????? ?????????? ??????? ?? ????? ??? ? ????????? ?????????? ????? ?????? ???? ???? ???? ????? ???????? ????? ?? ????? ? ???????????????? ?
<p>?? ????????? ????? ?????? ?????? ????????? ??????????? ????? ????? ? ??????, ????? ???????? ???? ???? ????? ????? ?????? ??? ??? ?????? ????? ???? ? ???????  ?
<p>???????? ???? ????? ? ????? ?????? ??&OElig;?????? ???? ?????? ? ?????, ??????????? ????? ??????? ???????? ?????? ???????? ????? ? ?????? ??? ?, ?????? ????? ?
<p>????????? ????????? ???????? ?????? ???, ?????? ? ??????? ???? ?????? ???????? ??????????? ????????? ???? ????????? ???? ?????? ???? ? ??? ??????? ?????? &ls ?
<p><strong>????????</strong> ??? ?????? ??????????? ??????????? ???? ???? ???? ? ??????????? ????? ?????? ???? ???????? ???? ??????? ??? ? ??????????? ????? ??? ?
<p>??????? ?? ??????????? ????? ????? ????? ?????????? ????? ?????? ? ?????, ?????? ???????? &lsquo;???????????? ????? ???? ?????????&rsquo; ? ? ??????, ???? ?? ?
<p><strong>????????? ???????????? ????? ??????</strong><br />??? ? ? ??? ?????? ????? ??????? ????? ? ? ??????? ???????? ??????? ? ???? ????????? ? ???? ???? ?? ?
<p>??????? ????????????? ????????? ??????&ndash;? ?? ????? ? ?????? ?????? ??? ??? ???????? ???? ??? ???? ?????? ??????????? ???????? ????? ??? ???? ????????? ? ?
<p>??????????? ???? ????? ??? ?????? ????? ?????? ????? ?????? ? ??????? ??????????? ???? ??? ???? ????????? ??? ??????????? ????? ???????? ?????????? ??&yen;?? ?
<p><strong>???????? ???? &lsquo;??????&rsquo;</strong><br />?????? ????? ?????? ? ?????????? ?????? ?????????? ??? ???? ????? ??????? ? ???????? ???? ????? ???? ?
<p>????? ????????? ??? ?? ????????? ????????? ??????? ??? ? ??? ??? ????????? ??????? ????? ????????? ??????? ???? ??????? ??, ??&divide;??? ????? ?????????? ?? ?
<p>????? ??? ???????? ?????????? ???? ???????? ? ????????? ???????????? ??????? ???? ????? ?????? ????? ? ? ?????????? ??? ???????? ??????? ???? ?????? ??? ???  ?
<p>???????? ?????? ????? ???? ????? ???????? ???? ??? ???? ????????? ???????? ???? ???? ?? ? ??, ????? ????? ????? ?? ?????? ???? ??????? ?????? ????? ???? ???? ?
<p>???????; ??????? ????????? ??? ???????? ????, ????? ????? ????????? ??????? ???? ????? ? ?????&mdash; ??????&ndash;? ?? &lsquo;???? ????, ???? ????&rsquo; ?? ?
<p>????????? ???????????? ?????? ???? ????? ????? ??????????? ???? ??? ????? ? ? ?????? ???? ????? ????? ?? ?? ? ?????? ?????? ???? ????????? ? ?????, ?????? ?? ?
<p>???????????? ?????????? ???? ???????? ??????? ???? ??????? ?????? ??? ??? ???????, ??? ??? ??????? ? ??? ??? ????????????? ?????????? ?????? ??????? ????? ?? ?
<p><strong>??????? ?????? ?????</strong><br />????? ??????, ?? &lsquo;???? ??????&rsquo; ?? ??????? &lsquo;??????&rsquo; ????? ???? ???????? ??????? ? ????, ??? ?
<p>????? ???????? ??????????? ?????????? ?????? ????? ???? ????? ??????????? ? ?????????? ???? ? ???????? ???? ?????????? ??? ??????? ????????? ????? ?????? ??? ?
<p>?????? ?????????????? ?????, ?????????????? ??????? ???? ? ? ?? ?????? ??&OElig;?????? ???? ?? ? ??????? ????????????? ??????? ????? ????? ????? ???????? ??  ?
<p>??????? ????????????? ????? ???? ???? ?????? ? ????? ????? ??? ?????? ??? ?????? ??? ???? ??????? ????? ????? ? ? ????? ??????? ????? ???? ??????????? ?????  ?
<p><em><strong>??????????????? ????????? ???????? ??????</strong></em></p>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As Mentioned in the comments, dompdf/domPDF after following the steps as mentioned above does not give the desired results, see this,

I suggest the use of TCPDF. elibyy / tcpdf-laravel provides a way to add tcpdf in laravel.
Install it using
composer require elibyy/tcpdf-laravel.

Add this to your provider in config/app.php

ElibyyTCPDFServiceProvider::class,

Then In your controller add the lines

use ElibyyTCPDFFacadesTCPDF;

public function generatePDF(){
        $data = new stdClass(); //can be your model, I have just used empty object
        $public_path = public_path();
        $data->title = "<p>??????????? ???????? ??</p>";
        $data->description =  "<p>????? ????????????? ????? ????? ??????? ????? ????????? ??????? ??? ????? ? 

        ?????????????? ??? ? ??, ??? ????? ??????????? ???????? ?? ???????? ??? ?
        <p>??????? ?????? ????? ?????, ???? ????? ?&ndash;????? ???????? ?????? ????? ?? ? ??????? ?? ???????????? ????? ?????? ????????? ?? ??????? ?????? ? ??? ??? ?? ?
        <p>??????? ????? ????? ??? ?????? ?????? ?, ???? ??? ????? ????? ?????? ????? ???????? ? ????????? ????? ???? ?????? ??????? ???? ??????????? ??????? ????? ? ?? ?
        <p>????????? ??? ???? ????????? ??? ? ??????? ??????? ??????? ????? ???????? ?????? ????????????? ?????? ???????? ???, ?????? ? ?????? ?????? ???? ?????? ?????? ?
        <p><strong>???????? ????? ??????? ????</strong><br />????????? ??????????? ????? ????? ????????? ??????? ??????????? ? ????????? ??????? ???? ??? ? ????? ?????? ?
        <p>????? ?????? ??? ??? ??????? ?? ???? ?????? ? ? ?????? ??? ???????? ???????? ? ????????? ???????? ??? ????; ???? ??? ???????? ?????? ????? ?????? ? ???? ???? ?
        <p>??? ???, ?????????? ????? ?????????? ??????? ?? ????? ??? ? ????????? ?????????? ????? ?????? ???? ???? ???? ????? ???????? ????? ?? ????? ? ???????????????? ?
        <p>?? ????????? ????? ?????? ?????? ????????? ??????????? ????? ????? ? ??????, ????? ???????? ???? ???? ????? ????? ?????? ??? ??? ?????? ????? ???? ? ???????  ?
        <p>???????? ???? ????? ? ????? ?????? ??&OElig;?????? ???? ?????? ? ?????, ??????????? ????? ??????? ???????? ?????? ???????? ????? ? ?????? ??? ?, ?????? ????? ?
        <p>????????? ????????? ???????? ?????? ???, ?????? ? ??????? ???? ?????? ???????? ??????????? ????????? ???? ????????? ???? ?????? ???? ? ??? ??????? ?????? &ls ?
        <p><strong>????????</strong> ??? ?????? ??????????? ??????????? ???? ???? ???? ? ??????????? ????? ?????? ???? ???????? ???? ??????? ??? ? ??????????? ????? ??? ?
        <p>??????? ?? ??????????? ????? ????? ????? ?????????? ????? ?????? ? ?????, ?????? ???????? &lsquo;???????????? ????? ???? ?????????&rsquo; ? ? ??????, ???? ?? ?
        <p><strong>????????? ???????????? ????? ??????</strong><br />??? ? ? ??? ?????? ????? ??????? ????? ? ? ??????? ???????? ??????? ? ???? ????????? ? ???? ???? ?? ?
        <p>??????? ????????????? ????????? ??????&ndash;? ?? ????? ? ?????? ?????? ??? ??? ???????? ???? ??? ???? ?????? ??????????? ???????? ????? ??? ???? ????????? ? ?
        <p>??????????? ???? ????? ??? ?????? ????? ?????? ????? ?????? ? ??????? ??????????? ???? ??? ???? ????????? ??? ??????????? ????? ???????? ?????????? ??&yen;?? ?
        <p><strong>???????? ???? &lsquo;??????&rsquo;</strong><br />?????? ????? ?????? ? ?????????? ?????? ?????????? ??? ???? ????? ??????? ? ???????? ???? ????? ???? ?
        <p>????? ????????? ??? ?? ????????? ????????? ??????? ??? ? ??? ??? ????????? ??????? ????? ????????? ??????? ???? ??????? ??, ??&divide;??? ????? ?????????? ?? ?
        <p>????? ??? ???????? ?????????? ???? ???????? ? ????????? ???????????? ??????? ???? ????? ?????? ????? ? ? ?????????? ??? ???????? ??????? ???? ?????? ??? ???  ?
        <p>???????? ?????? ????? ???? ????? ???????? ???? ??? ???? ????????? ???????? ???? ???? ?? ? ??, ????? ????? ????? ?? ?????? ???? ??????? ?????? ????? ???? ???? ?
        <p>???????; ??????? ????????? ??? ???????? ????, ????? ????? ????????? ??????? ???? ????? ? ?????&mdash; ??????&ndash;? ?? &lsquo;???? ????, ???? ????&rsquo; ?? ?
        <p>????????? ???????????? ?????? ???? ????? ????? ??????????? ???? ??? ????? ? ? ?????? ???? ????? ????? ?? ?? ? ?????? ?????? ???? ????????? ? ?????, ?????? ?? ?
        <p>???????????? ?????????? ???? ???????? ??????? ???? ??????? ?????? ??? ??? ???????, ??? ??? ??????? ? ??? ??? ????????????? ?????????? ?????? ??????? ????? ?? ?
        <p><strong>??????? ?????? ?????</strong><br />????? ??????, ?? &lsquo;???? ??????&rsquo; ?? ??????? &lsquo;??????&rsquo; ????? ???? ???????? ??????? ? ????, ??? ?
        <p>????? ???????? ??????????? ?????????? ?????? ????? ???? ????? ??????????? ? ?????????? ???? ? ???????? ???? ?????????? ??? ??????? ????????? ????? ?????? ??? ?
        <p>?????? ?????????????? ?????, ?????????????? ??????? ???? ? ? ?? ?????? ??&OElig;?????? ???? ?? ? ??????? ????????????? ??????? ????? ????? ????? ???????? ??  ?
        <p>??????? ????????????? ????? ???? ???? ?????? ? ????? ????? ??? ?????? ??? ?????? ??? ???? ??????? ????? ????? ? ? ????? ??????? ????? ???? ??????????? ?????  ?
        <p><em><strong>??????????????? ????????? ???????? ??????</strong></em></p> "; 
        $html = "<!DOCTYPE html>
        <html>
        <head>
          <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
          <meta charset='UTF-8'>
            <title>". $data->title ."</title>
            <style>
            @font-face {
              font-family: 'preeti';
              font-style: normal;
              font-weight: 400;
              src:url('".$public_path."/public/PREETI.TTF' format('woff');
            }
            </style>
        </head>
        <body style='font-family: preeti;'>
            ". $data->description ."
        </body>
        </html>";
        $html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
        
        
        $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
        $pdf::setFontSubSetting(true);
        $pdf::setFont('freeserif','',12);
        // $pdf->SetFont('dejavusans', '', 14, '', true);
        $pdf::AddPage();
        $pdf::writeHTML($html, true, 0, true, true);
        $output = $pdf::Output('001.pdf', 'I');

        return $output;    
      }

Should get output like this.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...