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”

Google | I’m Feeling Lucky Easter Eggs

A step ahead of search Easter eggs Google also has many I’m Feeling Lucky tricks as well. Whenever you search for a keyword on Google, you get a I’m Feeling Lucky link, on clicking this link with specific keywords you can see those Easter eggs. Listed below are the few hoaxes which I have come across by now.

google is afraid to show its logo with rajinikanth
  • google gravity (you can still use that search box)
  • google sphere (you can search for images)
  • ru/mov0001.swf (this one is worth trying you own self, people with weak heart please do not see it)
  • epic google — don’t forget to checkout his little brother Winnie google at the bottom of the page
  • find chuck norris — You may be feeling lucky, but not lucky enough to locate Chuck Norris.
  • find rajnikanth (this one is a better version of find chuck norris)
  • let me google that for you (this one works as a normal google search, but the fun beings after you press preview)
  • Sexy Snape (meet the sexy snake)
  • google pacman (this one is actually a doodle and you can still play that game)
  • love music? Try google guitar.
  • google reverse or typing google in reverse i.e. elgoog
  • google pig latin
  • google pond
  • create your own google type logo with goglogo or funny google
  • google is god (this one is real fun)
  • psychic google (this one is a funny doodle by google released on april fool’s day)
  • google gulp (a search on this will get to google autodrink on line of smart drink to enhance surfing efficiency and which is one of the most famous google april fool hoax as well
  • Blackle (search will get a custom black background google search engine which aims to save power by displaying in black)

Some other Easter Eggs.