Now it's time to combine the previous two topic(topic1, topic2) together and write a PHP code that gets a file's total page number.
First we must find the file's mime type. And as i explained in this topic, we use fileinfo or file command from Linux console to get the mime type of the file like;
if(function_exists('finfo_file')){
$finfo = finfo_open(FILEINFO_MIME);
$file_type = finfo_file($finfo, $file);
finfo_close($finfo);
}else{
echo "Pecl Fileinfo package not found!!! Trying to get file mime type from Linux Console\r\n";
$file_type = trim(exec("file -bi " . escapeshellarg($file)));
}
if(stripos($file_type, "\\012- ") !== false)
$file_type = substr($file_type, stripos($file_type, "\\012- ")+6, strlen($file_type));
Then we coupled the file's mime type from our predefined mime types;
//Our predefined mime types
var $image_formats = array("image/jpeg", "image/gif", "image/bmp");
var $file_formats = array("application/octet-stream", "application/vnd.ms-excel", "application/msword", "application/vnd.ms-powerpoint", "text/plain", "application/rtf", "text/html");
//check to know which function we use to convert the file to pdf
if(in_array($file_type, $this->image_formats)){
$file = $this->imageProcessing($file);
}else if(in_array($file_type, $this->file_formats)){
$file = $this->fileProcessing($file);
}
In imageProcessing function we convert the image files like jpegs, gifs, bmps to pdfs as i explained in this topic like;
private function imageProcessing($file){
$file_allinfo = pathinfo($file);
$file_pdf = $file_allinfo['dirname'] . "/" . $file_allinfo['filename'] . "_converted.pdf";
exec('convert ' . $file . " " . $file_pdf);
return $file_pdf;
}
and in fileProcessing function we convert the Microsoft Office files or html files etc to pdfs as i explained in this topic like;
private function fileProcessing($file){
$file_allinfo = pathinfo($file);
$file_pdf = $file_allinfo['dirname'] . "/" . $file_allinfo['filename'] . "_converted.pdf";
exec('python ./DocumentConverter.py ' . $file . " " . $file_pdf);
return $file_pdf;
}
But in order to run the function above, we mustn't forget to run OpenOffice at port 80 as i explained before;
openoffice "-accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager" -norestore -nofirststartwizard -nologo -headless &
And the last part that gets the total page number info and deletes the pdf file;
//this $file variable contains the path of pdf file that i return from one of my function
$file_allinfo = pathinfo($file);
$file_path_and_name = $file_allinfo['dirname'] . "/" . $file_allinfo['filename'];
exec("pdfinfo ". $file . " | grep Pages: | awk '{print $2}' | tail -n1", $output ,$retval);
$total_page_count = str_replace(" ", "", $output[0]);
unlink($file) or die("Unable To Delete Created File!\r\n");
I write this example as a PHP class, if you want to use it just create an object and pass it's function the file's path like;
require ('PageCount.php');
$c_pagecount = new page_count_class();
$v_totalpage = $c_pagecount->pageCounting("./sample.doc");
var_dump($v_totalpage);
Here you can get some sample files, with sample test code and my class file.
Subscribe to:
Post Comments (Atom)
.bmp)
No comments:
Post a Comment