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() { require_once('tcpdf/config/lang/eng.php'); require_once('tcpdf/tcpdf.php'); }
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() { $this->load->helper('pdf_helper'); /* ---- ---- ---- ---- 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 tcpdf(); $obj_pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $obj_pdf->SetCreator(PDF_CREATOR); $title = "PDF Report"; $obj_pdf->SetTitle($title); $obj_pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, $title, PDF_HEADER_STRING); $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->SetDefaultMonospacedFont('helvetica'); $obj_pdf->SetHeaderMargin(PDF_MARGIN_HEADER); $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER); $obj_pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); $obj_pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); $obj_pdf->SetFont('helvetica', '', 9); $obj_pdf->setFontSubsetting(false); $obj_pdf->AddPage(); ob_start(); // we can have any view part here like HTML, PHP etc $content = ob_get_contents(); ob_end_clean(); $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.