Bangla font in pdf in Laravel using mpdf
Bangla Font in PDF Laravel(MPDF Package).In this tutorial we will learn step by step process how to download bangla font as pdf without having no font breakdown.For that we will use mPDF package.We can use many package for pdf like DomPDF but in DomPDF it has given font error For bangla font.For that we will use mPDF and it will give beautiful bangla font in pdf file.
Following are the steps
- Require this package in your composer.json
123"require": {"carlos-meneses/laravel-mpdf": "2.1.6"} - Run on terminal
1composer require carlos-meneses/laravel-mpdf - Add following in config/app.php
123456789101112131415'providers' => [// ...Meneses\LaravelMpdf\LaravelMpdfServiceProvider::class]'aliases' => [// ...'MPDF' => Meneses\LaravelMpdf\Facades\LaravelMpdf::class] - Run on terminal
1php artisan vendor:publish - Search ‘nikosh ttf’’ ,go to https://www.omicronlab.com/bangla-fonts.html , A file called Nikosh.ttf will be downloaded,Then go to project/vendor\mpdf\mpdf\ttfonts directory and paste this .ttf file
- Go to project/vendor/mpdf/mpdf/src/Config/FontVariables.php ,open FontVariables.php and search for ‘indics’ ,then after pothana2000 ,add follwoing code
1234"nikosh" => ['R' => "Nikosh.ttf",'useOTL' => 0xFF,],
or
12345678910111213/* Indic */"lohitkannada" => ['R' => "Lohit-Kannada.ttf",'useOTL' => 0xFF,],"pothana2000" => ['R' => "Pothana2000.ttf",'useOTL' => 0xFF,],"nikosh" => ['R' => "Nikosh.ttf",'useOTL' => 0xFF,],
- Go to project/config/ and create pdf.php
123456789101112131415161718192021222324252627282930<?phpreturn ['mode' => '','format' => 'A4','default_font_size' => '12','default_font' => 'nikosh',// 'margin_left' => 10,// 'margin_right' => 10,'margin_top' => 35px,// 'margin_bottom' => 10,// 'margin_header' => 0,// 'margin_footer' => 0,'orientation' => 'P','title' => 'Laravel mPDF','author' => '','watermark' => '','show_watermark' => false,'watermark_font' => 'sans-serif','display_mode' => 'fullpage','watermark_text_alpha' => 0.1,'custom_font_dir' => '','custom_font_data' => [],'auto_language_detection' => false,'temp_dir' => rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR),'pdfa' => false,'pdfaauto' => false,'use_active_forms' => false,]; - Laravel controller ,add
1use MPDF;
- Laravel controller function
123456789101112//data from query$empresult = DB::select(DB::raw("SELECT id FROM tablename1"));$dateformatMMYY = DB::select(DB::raw("SELECT id FROM tablename2"));$result = DB::select(DB::raw("SELECT id FROM tablename3"));//MPDF part below 2 lines$pdf=MPDF::loadView('Employee_History.empdata.pdfview_emp_his',compact('empresult','dateformatMMYY','result'), [], ['title' => 'Another Title'// 'margin_top' => 0]);return $pdf->download('document.pdf');
Here Employee_History.empdata.pdfview_emp_his =projectfolder/resources/view/Employee_History/empdata/pdfview_emp_his.blade.pdf or the view of pdf
’empresult’,’dateformatMMYY’,’result’ are data from query ,MPDF::loadView= it will load the view of pdf with data[compact]return $pdf->download(‘document.pdf’); here download will help to download the file and ‘document.pdf’ will be the filename of downloaded PDF
For package info https://packagist.org/packages/carlos-meneses/laravel-mpdf