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
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); require_once dirname(__FILE__) . '/fpdf/fpdf.php'; class Mypdf extends FPDF { protected $DPI; protected $MM_IN_INCH; protected $A4_HEIGHT; protected $A4_WIDTH; protected $maxwidth; protected $maxheight; function __construct() { $this->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
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Welcome extends CI_Controller { public function __construct() { parent::__construct(); $this->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
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
<section> <style> input[type="file"] { display: none !important; } .btn-success{ width:100%; font-size: 22px; font-weight: bold;} .navbar-brand img{width:80%;} .well label, .well label img{width:100%;cursor:pointer;} .well{ background: #f6f6f6; border-top: 5px solid #479b25;} .custom-text{font-size:16px;display:none;} @media only screen and (max-width:767px){ .header-text h1 { font-size: 20px; font-weight: bold; color: #03828a; } .well{padding:5px;width:100%;} #myform { margin-left: 0px; margin-right: 0px; } } </style> <div class="container"> <div class="row"> <div class="col-md-12"> <h2>Convert Images to PDF Document Online With Instructions</h2> <div class="col-md-6"> <div class="header-text single_like"> <p><b>Step-1: </b>Select .jpg or .jpeg or .png images from you device and click on "Convert to PDF" button. Wait for the convert PDF to finish. </p> <p><b>Step-2: </b>Automatically download PDF file.</p> </div> </div> <div class="col-md-6"> <fieldset class="well"> <form action="<?php echo base_url();?>/welcome/imagetopdf" name="myform" id="myform" method="post" enctype="multipart/form-data"> <center><p class="custom-text">You have selected <span class="count-file">0</span> files</p></center> <label for="file"><img src="<?php echo base_url();?>uploads/drag-drop-file-upload.jpg" alt="drag-drop-file-upload"></label> <input type="file" name="file" id="file" required=""> <input type="submit" name="submit" id="submit" value="Convert to PDF" class="btn btn-success check"> </form> </fieldset> </div> </div> </div> </div> </section> <script> $(document).ready(function(){ $("input#file").change(function(){ var files = $(this)[0].files; if(files.length > 0){ $(".count-file").text(files.length); $(".count-file").css("font-weight","bold"); $(".count-file").css("color","red"); $(".custom-text").css("display","block"); } }); }); </script> <script> $(document).ready(function(){ $("#submit").click(function(){ $(".custom-text").css("display","none"); }); }); </script> |
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
Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co