2

I am working on a PHP based system in which i need to generate PDF files for reporting purpose. I am using fpdf library for this.

Now there can be Bangla characters in those PDF file. I tried so many ways and also tried Adding new fonts and encoding support fpdf but none is working for me.

Trying to solve this problem for last 1 day but didnt succeeded. If anybody have any solution or atleast know how to do this.

Edit:

I used Solaiman-Lipi font for this. I generated solaiman-lipi.php & solaiman-lipi.z file accordingly and then used below code to generate the PDF:

require('fpdf.php');

$pdf = new FPDF();
$pdf->AddFont('SolaimanLipi','','solaiman-lipi.php');
$pdf->AddPage();
$pdf->SetFont('SolaimanLipi','',35);
$str = iconv('UTF-8', 'windows-1252', '??? ??? ????');
$pdf->Write(10,$str);
$pdf->Output();

Edit:

Optionally i am attaching an image:

Unicode problem while generating PDF

Edit:

Added what the output should look like in PDF. This is Bangla Text.

enter image description here

3 Answers3

4

You could use mPDF instead of fpdf as library and use nikosh as bangla font. First, you have to copy nikosh.ttf font file on /ttfonts folder of mpdf library. Then, change config_fonts.php file. Add

"nikosh" => array(
    'R' => "Nikosh.ttf",
    'useOTL' => 0xFF,       
    ), 

inside the fontdata array. As Bangla has similarity with Indic language so I added Bangla font (Nikosh) information after Indic fonts.

For details you could see the following articles:

http://jobnstudy.blogspot.com/2016/10/how-to-write-bengali-bangla-unicode-pdf.html

  • This is why mPdf can only have proper indic support: https://mpdf.github.io/fonts-languages/opentype-layout-otl.html – Abbas Oct 08 '18 at 12:37
1

I think i found the solution of this problem. i've tried some library (dompdf, TCPDF,FPDF,mPDF) which support PHP but not all of them are work properly. I didn't find any unicode support with dompdf, tcpdf worked but the fonts are broken. so i tried mPDF and it gave me my expected output. i'm using this library to my laravel project. To using mPDF with laravel project you need to follow the documentation of laravel-mpdf. link: https://github.com/niklasravnsborg/laravel-pdf

after installed the library successfully what you have to do this following steps for other language or bangla support.

  1. first download the fonts for your supported language (i.e: for bangla i've downloaded solaimanLipi.ttf) and keep it to your fonts folder inside the resources folder. if there is no fonts folder just create it. 2.In config/pdf.php file add these codes inside of return array

    'font_path' => base_path('resources/fonts/'), 'font_data' => [ 'examplefont' => [ 'R' => 'ExampleFont-Regular.ttf', // regular font 'B' => 'ExampleFont-Bold.ttf', // optional: bold font 'I' => 'ExampleFont-Italic.ttf', // optional: italic font 'BI' => 'ExampleFont-Bold-Italic.ttf' // optional: bold-italic font 'useOTL' => 0xFF,
    'useKashida' => 75, ] // ...add as many as you want. ]

examplefont is your attribute name whatever you want to use. but inside this attribute paste your font name.

Example:

'bangla' => [
            'R'  => 'SolaimanLipi.ttf',    // regular font
            'B'  => 'SolaimanLipi.ttf',       // optional: bold font
            'I'  => 'SolaimanLipi.ttf',     // optional: italic font
            'BI' => 'SolaimanLipi.ttf' // optional: bold-italic font
            'useOTL' => 0xFF,   
            'useKashida' => 75, 
        ]

now in your view file use this CSS Scripts:

<style>
body {
    font-family: 'examplefont', sans-serif;
}
</style>

In Your Controller:

function generate_pdf() {
    $data = [
        'foo' => 'bar'
    ];
    $pdf = PDF::loadView('pdf.document', $data);
    return $pdf->stream('document.pdf');
}

Hints: pdf.document is your view file name. Use PDF at the top in your namespace

If you follow this step correctly ,it will work 100%. This is my output Output

N.B: This is my first post. if i did any mistakes to write review in correct format please consider it take it lightly. Thank You, Mahabubul Islam Mehedi

0

It works for Bangla. You can use any font by this process. I use this package for my laravel project barryvdh/laravel-dompdf By referencing from this package, here's the solution:

  1. by default it uses your storage>fonts folder to store fonts. For bangla I'll use SolaimanLipi.ttf font. Download the fonts from this URL solaimanlipi and save them into your storage>fonts folder.
  2. unzip the file and the fonts will have name like this "SolaimanLipi_29-05-06.ttf" ..etc. Rename it to "SolaimanLipi.ttf"
  3. Go to the file named storage/fonts/dompdf_font_family_cache.php If you don't have this file make sure you've ran this command php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"
  4. Now inside the file storage/fonts/dompdf_font_family_cache.php add the following lines:

Example:

'bangla' => array(
    'R' => $fontDir . '/SolaimanLipi',    // regular font
    'B' => $fontDir . '/SolaimanLipi',       // optional: bold font
    'I' => $fontDir . '/SolaimanLipi',     // optional: italic font
    'BI' => $fontDir . '/SolaimanLipi', // optional: bold-italic font
    'useOTL' => 0xFF,
    'useKashida' => 75,
)

You have to specify this font family into your HTML or body tag like this:

body {
    font-family: Arial, sans-serif, 'bangla';
}

You can also use 'kalpurush' and other bangla fonts this way. By specifying more families in the font-family group you can print multiple languages into your pdf.