Concatenate PDF in PHP

As requirement for one of my projects, I needed to concatenate multipe PDF files provided by the user into just one file.

To achieve this, we need the TCPDF and FPDI libraries.

Class to concatenate pdf, Pdf_concat.class.php:

require_once('./includes/tcpdf/config/lang/eng.php');
require_once('./includes/tcpdf/tcpdf.php');
require_once('./includes/fpdi/fpdi.php');

class Pdf_concat extends FPDI {
     var $files = array();
	 
     function setFiles($files) {
          $this->files = $files;
     }
	 
     function concat() {
          foreach($this->files AS $file) {
               $pagecount = $this->setSourceFile($file);
               for ($i = 1; $i <= $pagecount; $i++) {
                    $tplidx = $this->ImportPage($i);
                    $s = $this->getTemplatesize($tplidx);
                    $this->AddPage('P', array($s['w'], $s['h']));
                    $this->useTemplate($tplidx);
               }
          }
     }
}

Usage:

Continue reading “Concatenate PDF in PHP”

Php file_exists, does it really exists?

“Php functions are sometimes so confusing”, I don’t know how true that is, but I just found it to be true in this very case. Php’s file_exists, a function that seems like it is made to check if a file exists or not, is the one I encountered recently.

So I created this little test for this very purpose. I created an images folder with 2 files and 1 folder. Next I used file_exists function in 3 conditions with respective relative paths, to see which of those existed.

$file1="images/two.jpeg";
$file2="images/icon/one.jpg";
$file3="images/small/icon/";

if (file_exists($file1))
    echo "File one exists";

if (file_exists($file2))
    echo "File two exists";

if (file_exists($file3))
    echo "File three exists";

To my surprise all the conditions returned true.

File one exists
File two exists
File three exists

file_exists — Checks whether a file or directory exists.

According to php.net manual:

Irrespective of existence of file, file_exists function return true even if the path provided exists, which is in accordance with php manual’s definition, it’s just the name that is confusing. So, its better to use is_file() together with file_exists() if the objective is to check if the file really exists or is_dir()to check for directory.

Cheers!

Creating a zip file using php

Creating a zip file in php is as easy as creating them on your desktop using some zipping software. Php comes with inbuilt class that helps in making a zip file and provides all the functionality that you would ever need. Here is a simple function that I coded for my project. Hope it helps some peoples out there as well.

Php code:

// creates a compressed zip file
function create_zip($files = array(), $filename=array(), $destination = '',$overwrite = false) {
	//if the zip file already exists and overwrite is false, return false
	if(file_exists($destination) && !$overwrite) { return false; }

	//vars
	$valid_files = array();
	$valid_names = array();
	$i=0;

	//if files were passed in...
	if(is_array($files)) {
		//cycle through each file
		foreach($files as $file) {
			//make sure the file exists
			if(is_file($file)) {
				$valid_files[] = $file;
				if(isset($filename[$i]) && $filename[$i]!='') {
					$valid_names[]=$filename[$i];
				} else {
					$valid_names[]=$file;
				}
			}
			$i++;
		}
	}

	//if we have good files...
	if(count($valid_files)) {
		//create the archive
		$zip = new ZipArchive();
		if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
			return false;
		}

		$i=0;
		//add the files
		foreach($valid_files as $file) {
			$zip->addFile($file,$valid_names[$i]);
			$i++;
		}

		//debug
		//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;

		//close the zip -- done!
		$zip->close();

		//check to make sure the file exists
		return file_exists($destination);
	} else {
		return false;
	}
}

Usage demo:

Continue reading “Creating a zip file using php”

Validating an email via JavaScript

Nearly every programmer needs to validate email id somewhere sometime while development. Here I have got a simple regex that is very good at validating JavaScript.

Demo

Email

Continue reading “Validating an email via JavaScript”

Firebug or developers tool

A very few people know of what developer tools are (or firebug in case of firefox). Well, developer tools are some handy tools that are used in web development, as their name suggests.  These tools give a decent way to check out not only the HTML css but even the java script, cookies and lot many things of about web page.

How to get them?

If you are on firefox you can install the addon firebug and just press F12 and you can see them. Chrome users can just press F12 and see it. These are also available on safari, opera and internet explorer and you can search them on Google.

Continue reading “Firebug or developers tool”