Home » CodeIgniter » HTML to PDF Conversion in CodeIgniter

HTML to PDF Conversion in CodeIgniter

A number of PHP libraries to convert HTML page to PDF file. After a lot of research and googling find TCPDF PHP library to convert HTML page to PDF file for client requirement. TCPDf PHP library quite easy to integrate with CodeIgniter.

The implementation of the code.

Download the TCPDF library code

Download it from TCPDF website http://www.tcpdf.org/

Now create “tcpdf” folder in “application/helpers/” directory of your web application which is developed in CodeIgniter.

Copy all TCPDF library files and paste it in “application/helpers/tcpdf/” directory.

Update the configuration file “tcpdf_config.php” of TCPDF, which is located in “application/helpers/

Copy all TCPDF library files and paste it in “application/helpers/tcpdf/” directory.

Update the configuration file “tcpdf_config.php” of TCPDF, which is in application/helpers/tcpdf/config

Do modifications according to application requirements like the set logo, font, font size, width, height, header etc in the config file.

Give a read, write permissions to “cache” folder.

After defining directory structure, updating configuration file and assigning permissions.

Create one CodeIgniter PHP helper file in “application/helpers/” directory of CodeIgniter, and name it as “pdf_helper.php”, then copy below-given code and paste it in helper file

Helper: application/helpers/pdf_helper.php
function tcpdf()

Then in controller file call the above helper, suppose our controller file is “createpdf.php” and it has a method as pdf(), so the method pdf() will load the “pdf_helper” helper and will also have any other code.

Controller: application/controllers/createpdf.php
function pdf()
        ---- ---- ---- ----
        your code here
        ---- ---- ---- ----
    $this->load->view('pdfreport', $data);

Now create one view file, say “pdfreport.php” in “application/views/” directory, which is also loaded in pdf() method in the controller. So in view file, we can directly call the tcpdf() function which we have defined in “pdf_helper” helper, which will load all required TCPDF classes, functions, variable etc. Then we can directly use the TCPDF example codes as it is in our current controller or view. Now in our current view “pdfreport” copy below given code.

View: application/views/pdfreport.php
$obj_pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$title = "PDF Report";
$obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$obj_pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$obj_pdf->SetFont('helvetica', '', 9);
// we can have any view part here like HTML, PHP etc
$content = ob_get_contents();
$obj_pdf->writeHTML($content, true, false, true, false, '');
$obj_pdf->Output('output.pdf', 'I');

Thus our HTML page will be converted to PDF using TCPDF in CodeIgniter. We can also embed images, CSS, modifications in PDF file by using TCPDF library.

Leave a Reply


Check Also

Export Database using codeigniter

Database Utility Class : CodeIgniter PHP script to download mysql db-codeigniter,Backup MySQL database with CodeIgniter ...