Search code examples
oracle-databasecodeigniterfpdf

how can I make pdf with codeigniter?


so it can look like this example

what tutorial should I follow?


Solution

    1. Download the TCPDF file from here
    2. Extract and place the folder as 'tcpdf' in the path “application/third_party”
    3. Create a file named 'pdf.php' in the folder “application/libraries” and put the below code

      <?php
      if (!defined('BASEPATH'))
      exit('No direct script access allowed');
      
      require_once APPPATH . "/third_party/tcpdf/tcpdf.php";
      class Pdf extends tcpdf {
          public function __construct() {
          parent::__construct();
          }
      }
      ?>
      
    4. Add the following function in your controller

          public function createPDF($fileName,$html) {
              ob_start(); 
              // Include the main TCPDF library (search for installation path).
              $this->load->library('Pdf');
              // create new PDF document
              $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
              // set document information
              $pdf->SetCreator(PDF_CREATOR);
              $pdf->SetAuthor('TcPdf');
              $pdf->SetTitle('TcPdf');
              $pdf->SetSubject('TcPdf');
              $pdf->SetKeywords('TcPdf');
      
              // set default header data
              $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
      
              // set header and footer fonts
              $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
              $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
      
              $pdf->SetPrintHeader(false);
              $pdf->SetPrintFooter(false);
      
              // set default monospaced font
              $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
      
              // set margins
              $pdf->SetMargins(PDF_MARGIN_LEFT, 0, PDF_MARGIN_RIGHT);
              $pdf->SetHeaderMargin(0);
              $pdf->SetFooterMargin(0);
      
              // set auto page breaks
              //$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
              $pdf->SetAutoPageBreak(TRUE, 0);
      
              // set image scale factor
              $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
      
              // set some language-dependent strings (optional)
              if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
                  require_once(dirname(__FILE__).'/lang/eng.php');
                  $pdf->setLanguageArray($l);
              }       
      
              // set font
              $pdf->SetFont('dejavusans', '', 10);
      
              // add a page
              $pdf->AddPage();
      
              // output the HTML content
              $pdf->writeHTML($html, true, false, true, false, '');
      
              // reset pointer to the last page
              $pdf->lastPage();       
              ob_end_clean();
              //Close and output PDF document
              $pdf->Output($fileName, 'F');        
          }
      
    5. Crete a view exactly how you want in the pdf and load it in the controller to make pdf

          $htmlContent = $this->load->view('views/your-view-file', $data, TRUE);       
          $createPDFFile = 'your-pdf-name'.'.pdf';
          $this->createPDF('location-for-pdf'.$createPDFFile, $htmlContent);
      

    This will create a pdf file exactly like the view you made, in the location you gave.