How To Create Pdf Using MPDF Codeigniter 3?

In my previous post, We wrote a article on CodeIgniter 3 PDF Generate Tutorial With Example and in that article We covered mPDF library manual installation step by step, without any types of the run composer. So in this post, We would like to use CodeIgniter 3 as well as composer to install and to load mPDF library.

Here are the simple 5 steps to generate main PDF in the CodeIgniter 3 web application with mPDF: also resolve Error “temporary files directory is not writable mpdf codeigniter”.

Step 1:

first of all you can simple Download and install CodeIgniter.

Also Read: PHP Create PDF file using mpdf

Step 2:

and then Run below composer command to download mPDF library from your web project Directory. It will make a fresh Directory called “vendor” as well as it will download free “mpdf” library into it.

$ composer require mpdf/mpdf

there is my the directory main step 3 and step 4 after installing mpdf

Step 3:

Open application/config/config.php file as well as update you vendor directory path

 $config['composer_autoload'] = 'vendor/autoload.php';

Also Read: PHP How-to create Dynamic PDF Files using FPDF

Step 4:

Use mPDF library inside in your controller, here is the sample:

load->view('html_to_pdf',[],true);
		$mpdf->WriteHTML($html);
		$mpdf->Output(); // opens in browser
		//$mpdf->Output('pakainfo.pdf','D'); // it downloads the file into the user system, with give name
	}

}

Step 5:

simple last step to create a view called html_to_pdf.php file in server side in your view Directory with below html part.




	
	Welcome to pakainfo



Welcome to pakainfo!

If you have not found Tamilrockers website worth or have not liked by pakainfo.

TamilRockers New Link: Want to know the way to get Tamilrockers New link ? or Tamilrockers Latest URL?

application/views/welcome_message.php

tamilrockers new website

application/controllers/Welcome.php

TamilRockers New Website 2020.. how to download tamilrockers new link without using any VPN applications enter tamil rockers domain tamilrockers new link.

Also Read: Laravel 7 Generate/Create PDF File Tutorial From Scratch

Step 6:

That’s it. Very quick and simple. Now access your CodeIgniter application, you should get similar results:

Using mPDF with CodeIgniter 3

mPDF is a PHP library that allows you to generate PDF files from HTML content. In order to use mPDF with CodeIgniter 3, you can follow these steps:

Download the latest version of mPDF from the official website and extract it to your CodeIgniter project’s “third_party” folder. You can rename the folder to “mpdf” for easier access.

Create a new CodeIgniter library file in your “application/libraries” folder and name it “Pdf.php” (or any other name you prefer).

In the “Pdf.php” file, define a class named “Pdf” and include the mPDF library by requiring the “autoload.php” file:

mpdf = new Mpdf();
    }

    public function generate($html, $filename) {
        $this->mpdf->WriteHTML($html);
        $this->mpdf->Output($filename, 'D');
    }

}

In your CodeIgniter controller, load the Pdf library and use the “generate” method to generate the PDF file from HTML content:

load->library('pdf');

        $html = '

Hello, world!

'; $filename = 'example.pdf'; $this->pdf->generate($html, $filename); } }

In this example, the “index” method generates a PDF file with the filename “example.pdf” containing the HTML content “

Hello, world!

“. The “generate” method of the Pdf library takes two parameters: the HTML content and the filename to save the PDF as.

Note that you can also configure the mPDF library by passing options to the constructor of the Pdf class, for example:

$this->mpdf = new Mpdf([
    'orientation' => 'P',
    'format' => 'A4'
]);

This sets the orientation to portrait and the format to A4. You can find more information about the available options in the mPDF documentation.

Leave a Comment