Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter 4 - Using mPDF
#1

(This post was last modified: 10-12-2022, 10:15 AM by Luís Andrade.)

Hello, Guys!
I've been migrating CI3 systems to CI4 for some time.
It's not too insane the task because CI4 makes the code cleaner and shorter and with a very short learning curve, for those who already know CI3.
Now we will quickly talk about how to use the mPDF library in CI4
Installing using composer is the best path to success.
Install composer on your computer and go to your system folder.

https://getcomposer.org/download/

See instructions:
https://mpdf.github.io/installation-setu...-v7-x.html

Run:
$ composer require mpdf/mpdf

It may ask if you want it to use the composer.json file from CI4. Answer Y
Done! You don't need anything else! Just code!

mPDF < v8.x asks for ext-gd extension. So... it's easy to enable it.
//******************
From https://stackoverflow.com/questions/2283...without-gd
answered Aug 22, 2021 at 21:53
Mohamed Hany
PHP7 Or PHP8 For Windows:

Check if in your php.ini file has the following line:

;extension=gd
if exists, change it to

extension=gd
if not found, Add this

extension=gd
and restart apache

(it works on WINDOWS)
//******************

In your code for PDF it will like this:
$this->mpdf is my choice, and you can use any name you want, p.ex., $this->mympdf, $this->mydog, $this->otherpdf... 

//Basic config
$this->mpdf = new \Mpdf\Mpdf([
            'mode' => 'c',
            'format' => 'A4-L',
            'default_font_size' => '12',
            'default_font' => 'Helvetica',
            'margin_left' => 20,
            'margin_top' => 20,
            'margin_right' => 20,
            'margin_bottom' => 20,
        ]);

//For header and footer you need adapt for your needs. This is my easy example.

//**** PDF Header
        $header = array(
            'L' => array(
                'content' => $myContentTitle,
                'font-size' => 9,
                'font-style' => 'B',
                'font-family' => 'sans-serif',
                'color' => '#000000'
            ),
            'C' => array(
                'content' => $myContentTitle,
                'font-size' => 8,
                'font-style' => 'B',
                'font-family' => 'sans-serif',
                'color' => '#000000'
            ),
            'line' => 1, //line 1 = ON
            'R' => array(//RIGHT
                'content' => utf8_encode($someText),
                'font-size' => 8,
                'font-style' => 'B',
                'font-family' => 'sans-serif',
                'color' => '#000000'
            ),
        );

        //**** PDF footer
        $footer = array(
            'L' => array(
                'content' => '{DATE j/m/Y}- ',
                'font-size' => 9,
                'font-style' => 'B',
                'font-family' => 'sans-serif',
                'color' => '#000000'
            ),
            'C' => array(
                'content' => '{PAGENO}/{nbpg}',
                'font-size' => 9,
                'font-style' => 'B',
                'font-family' => 'sans-serif',
                'color' => '#000000'
            ),
            'line' => 1, //line 1 = ON
            'R' => array(//RIGHT
                'content' => '    ',
                'font-size' => 9,
                'font-style' => 'B',
                'font-family' => 'sans-serif',
                'color' => '#000000'
            )
        );
        //***************

$data= "your data here";
$html = view('rep/rep_to_pdf', $data);
$this->mpdf->SetHeader($header, 'O');
$this->mpdf->SetFooter($footer, 'O');

//Here you can use Bootstrap CSS from
//https://stackoverflow.com/questions/49102418/how-to-use-bootstrap-in-mpdf
//https://github.com/kartik-v/yii2-mpdf/blob/master/src/assets/kv-mpdf-bootstrap.css

$this->mpdf->WriteHTML(path to bootstrap CSS, 1);
$this->mpdf->SetDisplayMode('fullpage');
$this->mpdf->WriteHTML($html, 2);      
$this->mpdf->Output($nome_arquivo, 'D'); //option "D" save file

Now you have your PDF Smile

Configuration of mPDF:
https://mpdf.github.io/

Do your best, always!
Reply


Messages In This Thread
CodeIgniter 4 - Using mPDF - by Luís Andrade - 10-12-2022, 09:20 AM
RE: CodeIgniter 4 - Using mPDF - by jasaekspedisi - 10-12-2022, 09:26 AM
RE: CodeIgniter 4 - Using mPDF - by abatrans - 10-12-2022, 01:19 PM
RE: CodeIgniter 4 - Using mPDF - by Luís Andrade - 10-16-2022, 07:01 AM



Theme © iAndrew 2016 - Forum software by © MyBB