DEV Community

Arman Rahman
Arman Rahman

Posted on

mPDF Bangla font not working/Bangla font break in Laravel Fix

I am using laravel 9 Here. Hope this will work for other versions in the same way.

Since you're in the process of troubleshooting issues, I presume that you have already installed mPDF within your Laravel project.

If not CLICK ON THE LINK and follow the steps

Now you have to publish your config/pdf.php file of mPdf. Run this command on terminal from your project path.

php artisan vendor:publish --tag=mpdf-config
Enter fullscreen mode Exit fullscreen mode

Then, go to the file using your code editor. then add/replace this in the return array-

'custom_font_dir'          => base_path('resources/fonts/'),
'custom_font_data'         => [
        'kalpurush' => [
            'R'  => 'Kalpurush.ttf',
            'B'  => 'Kalpurush.ttf',
            'I'  => 'Kalpurush.ttf',
            'BI' => 'Kalpurush.ttf'
            ]
        ],
'auto_language_detection'  => true,
Enter fullscreen mode Exit fullscreen mode

Now download kalpurush.ttf file from internet source. Then place that file on this directory -

Note: You can use any other font you like. just make sure you are using the file name correct.

resources/fonts/kalpurush.ttf
Enter fullscreen mode Exit fullscreen mode

Now go to your pdf blade file and add this on style

body {
        font-family: sans-serif,'Kalpurush';
        font-size: 12px
      }
Enter fullscreen mode Exit fullscreen mode

I used this on controller

use PDF; // Used on top

    public function setupQuestionPost(QuestionGenerateHeadStoreRequest $request){
         ...
         ...

        $data = [
            'demo' => 'demo text',
        ];

        $pdf = PDF::loadView('backend.pdf.question_pdf', compact('data'));

        return $pdf->stream('document.pdf');
    }
Enter fullscreen mode Exit fullscreen mode

Thanks For reading. Hope this will Help you.

Top comments (0)