PHP Developers Network
http://forums.devnetwork.net/

splitting a pdf into images and saving files to amazon
http://forums.devnetwork.net/viewtopic.php?f=1&t=142599
Page 1 of 1

Author:  cjkeane [ Sun May 29, 2016 9:48 pm ]
Post subject:  splitting a pdf into images and saving files to amazon

Hi everyone
I need to split every page of a pdf into images. The max pages in a pdf will be 5 pages, so in the end, I need to have the original pdf + individual pages, one jpg per pdf page. I need the pdf and the images saved in an aws bucket folder. So far, the pdf is saved, but the pdf is not being extracted as images. Any assistance would be appreciate. This is my code so far.

Syntax: [ Download ] [ Hide ]
// save pdf
        if(isset($_POST['pdf_upload']))  {                                             
                $pdf_msg="";
                $bucket="pdf_bucket"; // enter here your created bucket name
                if (!class_exists('S3'))require_once('S3.php');
                if (!defined('awsAccessKey')) define('awsAccessKey', 'myaccesskey');
                if (!defined('awsSecretKey')) define('awsSecretKey', 'mysecretkey'); //add your original secret key
                $s3 = new S3(awsAccessKey, awsSecretKey);
                $pdf_name = $_FILES['presentation_file_name']['name'];
                $pdf_tmp = $_FILES['presentation_file_name']['tmp_name'];
                $pdf_actual_name = time() ."-". basename( $_FILES['presentation_file_name']['name']);
                $allowtypes2=array("pdf");
                $max_file_size2="20480";       
                $errors2 = array();
                if (empty($_POST['presentation_name']) ) {$errors2[]='A presentation name is required.';        }
                if (empty($_FILES['presentation_file_name']['name'])) {$errors2[]='A pdf file is required.';    }
                if((!empty($_FILES["presentation_file_name"])) && ($_FILES['presentation_file_name']['error'] == 0)) {
                        $presentation_file_name = basename($_FILES['presentation_file_name']['name']);
                        $presentation_name = $_POST['presentation_name'];
                        $ext2 = substr($presentation_file_name, strrpos($presentation_file_name, '.') + 1);
                        $filesize2=$_FILES['presentation_file_name']['size'];
                        $max_bytes2=$max_file_size2*1024;
                        if (!in_array($ext2, $allowtypes2)) {
                                $errors2[]="Invalid extension for your file: <strong>".$presentation_file_name."</strong> Only PDF files are permitted.";
                        } elseif($filesize2 > $max_bytes2) {
                                $errors2[]= "Your file: <strong>".$presentation_file_name."</strong> is to big. Max file size is ".$max_file_size2."kb.";
                        }
                }                              
                if (empty($errors2)) {
                        $pdf_path = "uploads/pdfs/" . $company_id . "/" . $pdf_id . '/' .   $pdf_actual_name;
                        $s3->putObjectFile($pdf_tmp, $bucket , $pdf_path, S3::ACL_PUBLIC_READ);
                        // split pdf into images for slideshow
                        $jpg_name = $pdf_actual_name;
                        $jpg_basename = substr($jpg_file_name,0,strpos($jpg_file_name,'.'));
                        $jpg_file_name = basename($jpg_file_name, '.pdf');
                        $jpg_img = new imagick('http://companyportfolios.s3.amazonaws.com/uploads/pdfs/'.$jpg_file_name.'.pdf');
                        $jpg_img->setImageBackgroundColor('white');
                        $jpg_img->setResolution(300,300);
                        $jpg_num_pages = $jpg_img->getNumberImages();
                        $jpg_img->setImageCompressionQuality(100);
                        $jpg_images = NULL;
                        for($jpg_i = 0;$jpg_i < $jpg_num_pages; $jpg_i++) {        
                                $jpg_images[]=$jpg_basename.'-'.$i.'.jpg';
                                $jpg_img->setIteratorIndex($i);
                                $jpg_img->setImageFormat('jpeg');
                                $jpg_tmp = $jpg_file_name.'-'.$i.'.jpg';
                                $jpg_folder_path = 'http://companyportfolios.s3.amazonaws.com/uploads/pdfs/' . $company_id . '/' . $pdf_id . "/" . $jpg_tmp;
                                $s3->putObjectFile($jpg_tmp, $bucket , $jpg_folder_path, S3::ACL_PUBLIC_READ);
                        }
                        $pdf_msg="<div style='color:red;'>PDF uploaded Successfully<div style='color:red;'>";
                        echo $pdf_msg;
                        $pdf_file_name = $pdf_actual_name;
                        $presentation_file_name=mysqli_real_escape_string($dbc,ltrim($pdf_file_name));
                        $presentation_name = mysqli_real_escape_string($dbc,$presentation_name);
                        $company_id = mysqli_real_escape_string($dbc,$id);
                        $folder_date = mysqli_real_escape_string($dbc,$folder_date);
                        $pdf_update_query ="INSERT INTO company_presentations (company_id,presentation_file_name,presentation_name) VALUES ($company_id',' $presentation_file_name','$presentation_name')";
                        mysqli_query($dbc,$pdf_update_query) ;
                        echo '<div style="color:red;">The pdf has been uploaded and saved as: ' . $presentation_file_name . '</h4></div><br />';
                        $presentation_file_name=$pdf_actual_name;                                                      
                } else {
                        $presentation_file_name = "";
                        //echo '<b>Validation Error(s):</b><br /><br />';
                        foreach ($errors2 as $msg2) {
                                echo '<div style="color:red;"> -' . $msg2 . '</div><br />';
                        }        
                }
        }

 

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/