In this article, You will learn about how to create a single pdf file in Codeigniter from a separate set of images or pdf files using ImageMagick . PDF is the most used format to combine multiple images document or pdf files to a single pdf file 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 combine multiple images or pdf to a single pdf file using ImageMagick.
Let’s see the step by step process to combine multiple images or pdf files into a single pdf file:
Step 1: Download Fresh Latest Version of Codeigniter
In First step we need to download fresh Latest version of Codeigniter , After Download successfully, extract clean new Codeigniter application.
so if you haven’t download yet then download from here : Download Codeigniter 3.
Step 2: Add Controller Method
In this step we require to add “combineImagePdf” 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 |
<?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 combineImagePdf(){ if (!empty($_POST)) { $filesCount = count($_FILES['files']['name']); if($filesCount>0){ $uploads = array(); $target_path =FCPATH . "uploads/imagetopdf/"; foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) { array_push($uploads,$_FILES['files']['tmp_name'][$key]);; } $filename="combine".rand().".pdf"; $pdf = new Imagick($uploads); $pdf->setImageFormat('pdf'); $pdf->writeImages($target_path.$filename, true); $buffer = file_get_contents($target_path.$filename); /* Force download dialog... */ header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); /* Don't allow caching... */ header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); /* Set data type, size and filename */ header("Content-Type: application/octet-stream"); header("Content-Transfer-Encoding: binary"); header("Content-Length: " . strlen($buffer)); header("Content-Disposition: attachment; filename=$filename"); /* Send our file... */ echo $buffer; }else{ $this->session->set_flashdata('error', 'Uploaded image should be jpg or png'); redirect("welcome/combineImagePdf"); } }else{ $this->load->view('combineImagePdf'); } } } |
Step-3: Create a folder “imagetopdf” under “uploads” folder in root directory
Step-4: Add View File
Now at last step we require to create “combineImagePdf.php” view file for generate pdf file. So you have to copy below code and create on view folder:
application/views/combineImagePdf.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 |
<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 proface-features"> <div class="row"> <div class="col-md-12"> <h2>Combine multiple PDF Files or Images into a single PDF document without having to install any software.</h2> <div class="col-md-6"> <div class="header-text single_like" > <p ><b>Step-1: </b>Select up to 20 PDF Files or images from your computer or drag them to the drop area. </p> <p ><b>Step-2: </b>Automatically download combine pdf file.</p> </div> </div> <div class="col-md-6"> <fieldset class="well"> <form action="<?php echo base_url();?>welcome/combineImagePdf" 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/tools/drag-drop-file-upload.jpg" alt="drag-drop-file-upload"></label> <input type="file" name="files[]" id="file" multiple accept="image/png, image/jpeg,image/jpg,application/pdf" required/> <input type="submit" name="submit" id="submit" value="SUBMIT" 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"); $(".successerror-message").css("display","none"); } }); $("#submit").click(function(){ $(".custom-text").text("Please Wait...."); $(".custom-text").css("color","red"); setTimeout(function () { $(".custom-text").text(""); }, 2000); }); }); </script> |
Now you can open bellow URL on your browser:
http://localhost:8000/welcome/combineImagePdf
If you are on live sever:
http://domainname.com/welcome/combineImagePdf
How to Convert an Image to PDF in CodeIgniter using FPDF Library
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