Generate Bangla Text Content in PDF using Laravel

Ariful Islam
2 min readMar 20, 2023

--

We most of the time use several libraries for generating bangla pdf text using laravel application. But due to some issues such as fonts/directory/php version/package error it becomes nightmare to solve; that’s why developer’s headache grows up. Here I will try to solve this issue for your time saving;

Step 1: Install Required carlos-meneses/laravel-mpdf package inside your laravel application

composer require carlos-meneses/laravel-mpdf

Note: It should work on your laravel 8, 9, 10; You may get error like below.

Declaration of Mpdf\Mpdf::setLogger(Psr\Log\LoggerInterface $logger) must be compatible with Psr\Log\LoggerAwareInterface::setLogger(Psr\Log\LoggerInterface $logger): void

For solving above error you need to add lower psr/log dependency inside your composer.json file like below and run composer update

“psr/log”: “2.0”

Step 2: Now run below command to publish pdf.php file inside app/config directory

php artisan vendor:publish — tag=mpdf-config

Step 3:

Download Solaimanlipi fonts and keep inside resources/fonts folder

https://www.wfonts.com/download/data/2016/04/29/solaimanlipi/solaimanlipi.zip

Edit your config/pdf.php file like below

'watermark_image_size' => 'D',
'watermark_image_position' => 'P',
'custom_font_dir' => base_path('resources/fonts/'), // don't forget the trailing slash!
'custom_font_data' => [
'solaiman_lipi' => [ // must be lowercase and snake_case
'R' => 'SolaimanLipi.ttf', // regular font,
'B' => 'SolaimanLipi_Bold_10-03-12.ttf', // bold font,
'useOTL' => 0xFF,
'useKashida' => 75
]
],

Step 4: Now define a route(/pdf) inside web.php

Route::get('/pdf', [App\Http\Controllers\HomeController::class, 'pdfGenerate']);

Step 5: Go to your HomeController and define a method called pdfGenerate to accept http request.

<?php

namespace App\Http\Controllers;

use Mccarlosen\LaravelMpdf\Facades\LaravelMpdf as PDF;
use Mpdf\MpdfException;

class HomeController extends Controller
{
/**
* @throws MpdfException
*/
public function pdfGenerate()
{
$data = [
'foo' => 'bar'
];

$pdf = PDF::loadView('pdf.document', $data); //view file location
return $pdf->stream('pdf.pdf');
//return $pdf->download('pdf.pdf');
}
}

Step 6: Its time to run http://127.0.0.1:8000/pdf to view the output or to download pdf file.

Output of Bangla PDF using Laravel

Boom! It works…..

For demo you can visit this demo github project link

Hopefully it will help you. If you want to get updates regularly related to php, laravel then subscribe me here and also follow me in linkedin.

Do code, be crazy

--

--

Ariful Islam

Software Engineer. Working on PHP, Laravel, CI, CakePhp, API. Open Source contributor, Cloud Expert, Project Consultant; https://linkedin.com/in/arif98741