Plan-B
Plan-B

Reputation: 91

Dompdf-Laravel not working with Unicode character

I'm using Laravel 5.5 with DOMPDF its work fine for English but not working for Unicode it's always output ???? symbol to me

Controller

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PDF;

class PdfController extends Controller
{
    public function export(){
        $data['title']="Print Report";
        $pdf = PDF::loadView('pdf.invoice', $data);
        return $pdf->download('invoice.pdf');
    }
}

Blade

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <style>
        @font-face {
            font-family: khmer;
            font-style: normal;
            font-weight: 400;
            src: url({{asset('fonts/khmer.ttf')}}) format('true-type');
        }
    </style>

</head>
<body>
    កម្ពុជាក្រោម
</body>
</html>

Result ?????????

Upvotes: 9

Views: 13413

Answers (4)

Talha Ishtiaq
Talha Ishtiaq

Reputation: 79

you just need to add font-family: DejaVu Sans; in your body or also you can set the font family to any specific div in which you are going to display the Unicode Characters.

For body: body{font-family: DejaVu Sans;}

And for a specific div: <div style="font-family: DejaVu Sans;"> --- </div>

Hope this would help.

Upvotes: 6

Kiry Meas
Kiry Meas

Reputation: 1242

@Plan-B

i use this package it works the same way as this Dompdf-Laravel library (Dompdf-Laravel library works great for other languages but not Khmer font).

  • composer require niklasravnsborg/laravel-pdf
  • It has a good doc about font installation: https://mpdf.github.io/
  • after installing the package, go to its repo https://github.com/niklasravnsborg/laravel-pdf read the instruction.
  • run php artisan vendor:publish select the library from the command prompt. You'll get the pdf.php in config directory
  • Now you need to choose appropriate Khmer fonts from the doc https://mpdf.github.io/fonts-languages/available-fonts-v6.html
  • In the doc, it recommends from two Khmer font websites. You must download the fonts from the websites otherwise the fonts will not display correctly.
  • download the fonts and store somewhere in the project - by default, the config/pdf.php choose resources/fonts directory And here is my setup in the pdf.php
// some code here...
'font_path' => base_path('resources/fonts/'),
  'font_data' => [
    "khmerosmoul" => [/* Khmer */
      'R' => "KhmerOSmuol.ttf",
      'useOTL' => 0xFF,
    ],
    "khmerosmoullight" => [/* Khmer */
      'R' => "KhmerOSmuollight.ttf",
      'useOTL' => 0xFF,
    ],
    "khmerosbokor" => [/* Khmer */
      'R' => "KhmerOSBokor.ttf",
      'useOTL' => 0xFF,
    ],
    "khmerosmoulpali" => [/* Khmer */
      'R' => "KhmerOSmuolpali.ttf",
      'useOTL' => 0xFF,
      'useKashida' => 75
    ]
    // ...add as many as you want.
  ]

Note: you might got trouble with the pdf html structure. Copy its template example form the repo and redesign it as you like. https://github.com/niklasravnsborg/laravel-pdf/blob/master/tests/views/exposify-expose.html

Upvotes: 5

Raymond Cheng
Raymond Cheng

Reputation: 2505

It's because your server doesn't support your font, you need to Install a font for your server which supports your Unicode font.

To Generate A PDF, the package is using system font.

Upvotes: 0

Michal D
Michal D

Reputation: 26

I was trying to solve similar problem in my application what I've done is I've transformed view with mb_convert_encoding function.

In your case I would to sth like that:

    $data['title']="Print Report";
    $pdf = mb_convert_encoding(\View::make('pdf.invoice', $data), 'HTML-ENTITIES', 'UTF-8'));
    return PDF::loadHtml($pdf)->download('invoice.pdf');

Also be careful with font settings. Your needs to support UTF-8 charset.

Upvotes: 0

Related Questions