Site icon Tutorials Website: Upgrade Your Web Development & Coding Skills

How to Convert an Image to PDF in CodeIgniter using FPDF Library

In this article, You will learn about how to create pdf file in Codeigniter from image using fpdf library . PDF is the most used format to generate PDF for image document in the web application. PDF file provides a easy and user-friendly way to upload the bunch of records in a pdf format file. If you are working on Codeigniter then you get problem to how to do it. But in this post i am going to share you very simple example to image to pdf convert using FPDF library.

FPDF is a PHP library that helps to generate PDF file from image file. It’s very easy to convert an image to PDF in codeignniter with fpdf.

Step 1: Download Fresh Codeigniter 3

In First step we need to download fresh version of Codeigniter 3, After Download successfully, extract clean new Codeigniter 3 application.

so if you haven’t download yet then download from here : Download Codeigniter 3.

Step 2: Download fpdf library

 let’s download fpdf library from here : Click Here to download fpdf. After download, extract it to your “application/libraries” folder and rename it to “fpdf”.

Step 3: Create mypdf.php file in “application/libraries” Folder

Create mypdf.php file in “application/libraries” folder and copy and paste the following code in this file

DPI = 96;
    $this->MM_IN_INCH = 25.4;
   $this->A4_HEIGHT = 297;
   $this->A4_WIDTH = 210;
$this->maxwidth = 1100;
    $this->maxheight = 700;
    
 parent::__construct();
 }
 
 
 
 
    function pixelsToMM($val) {
        return $val * $this->MM_IN_INCH / $this->DPI;
    }
    function resizeToFit($imgFilename) {
        list($width, $height) = getimagesize($imgFilename);
        $widthScale = $this->pixelsToMM($width);
        $heightScale = $this->pixelsToMM($height);
        if(($heightScale > $this->A4_HEIGHT) || $widthScale > $this->A4_WIDTH){
        
        $scale= round(($width/($this->A4_WIDTH-10)),2);
        $scalehight=round(($height/$scale),0);
        
        return array(
            ($this->A4_WIDTH-10),
            $scalehight
        );
        }else{
            return array(
            round($this->pixelsToMM($width)),
            round($this->pixelsToMM($height))
        );
        }
    }
    function centreImage($img) {
        list($xwidth, $xheight) = $this->resizeToFit($img);
        // you will probably want to swap the width/height
        // around depending on the page's orientation
       
        $this->Image(
            $img,5,5,
           $xwidth ,
            $xheight
        );
    }
 
 
}
  
/* End of file Pdf.php */

Step 4: Add Controller Method

In this step we require to add “imagetopdf” method on welcome controller, So let’s add with following code. you have to just copy of welcome.php controller file:

application/controllers/Welcome.php

load->library("Fpdf");
    }

  /**
    * Get All Data from this method.
    *
    * @return Response
   */
   public function index()
   {
	$this->load->view('welcome_message');
   }
 
 
   /**
    * Get Download PDF File
    *
    * @return Response
   */
  public function imagetopdf(){
         if (!empty($_POST)) {
             
             
             
                              if ($_FILES['file']['name'] != "") {

                        $path = FCPATH . "uploads/imagetopdf";

                        $config['upload_path'] = $path;
                        $config['allowed_types'] = 'gif|jpg|png|jpeg';
                        $config['max_size'] = 5024;
                        $this->load->library('upload', $config);

                        $this->upload->initialize($config);

                        if (!$this->upload->do_upload('file')) {
                            $error = $this->upload->display_errors();

                            $this->session->set_flashdata('error', $this->upload->display_errors());
                            redirect("welcome/imagetopdf");
                            
                        } else {

                            $data_upload_files = $this->upload->data();

                            $image = "uploads/imagetopdf/" . $data_upload_files['file_name'];
                            $exp=explode('.',$_FILES['file']['name']);
                            $downloadfile=$exp[0].".pdf";


$pdf = new Mypdf();
$pdf->AddPage();
$pdf->centreImage($image);
$pdf->Output('D', $downloadfile);
                                       
                    
                    }
                                  
                              }else{
                                   $this->session->set_flashdata('error', 'Error occured!Please try again');
                    redirect("welcome/imagetopdf");
                              }
             
         }else{
        
        $this->load->view('imagetopdf');
         }
        
    }    
}

Step 5: Add View File

Now at last step we require to create “imagetopdf.php” view file for generate pdf file.  So you have to copy below code and create on view folder:

application/views/imagetopdf.php

Convert Images to PDF Document Online With Instructions

Step-1: Select .jpg or .jpeg or .png images from you device and click on "Convert to PDF" button. Wait for the convert PDF to finish.

Step-2: Automatically download PDF file.

You have selected 0 files

Now you can open bellow URL on your browser:

http://localhost:8000/welcome/imagetopdf

If you are on live sever:

 http://domainname.com/welcome/imagetopdf

How to Convert HTML to PDF in CodeIgniter using Dompdf Library

How to Generate PDF from Mysql Database using PHP

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request

Exit mobile version