PHPExcel create pdf from a template - codeigniter

I would like to generate a pdf file using php excel from a template using the following code :
//load our new PHPExcel library
$this->load->library('excel');
echo 'Library load succesfull';
//name the worksheet
$objPHPexcel = PHPExcel_IOFactory::load('/Applications/MAMP/htdocs/timesheet/files/Timesheet_template.xlsx');
echo 'Loading template successfull...';
$objWorksheet = $objPHPexcel->getActiveSheet();
$objWorksheet->getCell('C17')->setValue($td_3);
$objWorksheet->getCell('D17')->setValue($td_4);
$objWorksheet->getCell('E17')->setValue($td_5);
$objWorksheet->getCell('F17')->setValue($td_6);
$month = date('F');
$year = date('Y');
$data['user_data'] = $this->session->userdata('user');
$user_id = $data['user_data']["employee_id"];
$f_name = $data['user_data']["f_name"];
$s_name = $data['user_data']["s_name"];
$username = $data['user_data']["user_name"];
$title = $data['user_data']["title"];
$is_active = $data['user_data']["is_active"];
$employee_type = $data['user_data']["employee_type"];
// $filename = 'just_some_random_name.xlsx'; //save our workbook as this file name
//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
//if you want to save it as .XLSX Excel 2007 format
echo 'ALMOST CREATING THE PDF FILE';
$objWriter = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
$rendererLibrary = 'domPDF0.6.0beta3';
$rendererLibraryPath = '/Applications/MAMP/htdocs/timesheet/system/helpers/dompdf';
$objWriter = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
$rendererLibrary = 'domPDF0.6.0beta3';
$rendererLibraryPath = '/Applications/MAMP/htdocs/timesheet/system/helpers/dompdf';
if (!PHPExcel_Settings::setPdfRenderer(
$objWriter, $rendererLibraryPath
)) {
die(
'Please set the $rendererName and $rendererLibraryPath values' .
PHP_EOL .
' as appropriate for your directory structure'
);
}
echo 'pdf creation almost starting';
// $objWriter = PHPExcel_IOFactory::createWriter($objPHPexcel,'pdf');
$objWriter = new PHPExcel_Writer_PDF($objPHPexcel);
echo 'SUCCESS FULLY CREATED THE PDF FILE...';
$under_score = "_";
$year = preg_replace('/\s+/', '', $year);
$inputFileType = $username . $under_score . $month . $under_score . $year . ".pdf";
$objWriter->save("files/un_approved/" . $username . $under_score . $month . $under_score . $year . ".pdf");
I am using dompdf library to convert the final template to pdf but the file that's generated comes as corrupted and cannot be opened by Adobe Reder. Please advise what is the best way I should approach it?

Related

upload file in public laravel

Hello guys when I use file upload and save link in database it save this "/tmp/phpkPhygr|public/allFiles/files/26657d5ff9020d2abefe558796b99584/69adc1e107f7f7d035d7baf04342e1ca.gif" so why "tmp/php..." show in this link or I want to remove this to work this url.
$files = array();
if($files = $request->file('files')){
foreach($files as $file){
$fileName = md5(rand(100,1000)) . "." . strtolower($file->getClientOriginalExtension());
$folder='public/allFiles/files/' . md5(rand(100,10));
$file_url = $folder . "/" . $fileName;
$file->move($folder, $fileName);
$files[] = $file_url;
}
}
$filesReq = $files;
if($filesReq == ' '){
$filesReq = 'no files';
}else{
$filesReq = implode('|', $files);
}

Upload image and extract filename instead of file path

I am using following code to upload thumbnail in "storage/app/public/websites"
$path = $request->file('thumbnail')->store('public/websites');
It is working fine and upload images to "websites" directory but problem is it returns the actual path e.g. websites/r63mAKN1kil3BIwvwwRevOv93MgWQFme39BwH8ZV.jpeg
i only want to save image name e.g. r63mAKN1kil3BIwvwwRevOv93MgWQFme39BwH8ZV.jpeg in database table.
By default Laravel generates Unique ID for image name. Is there way to return only filename instead of path ?
This is the hash name, so, first I think you need to separate your steps.
$file = $request->file('thumbnail');
$path = $file->store('public/websites');
when you need to add file name you can use $file->hashName();
You can use the basename function
http://php.net/manual/en/function.basename.php
$filename = basename($path);
This is what you need
$extension = $request->image->getClientOriginalExtension();
$image_name = str_replace(' ', '', trim($request->model) . time() . "." . $extension);
and to move the image to your desired folder use:
$image_path = $request->image->move(public_path('images'), $image_name);
$info = pathinfo( $url );
$contents = ( new \GuzzleHttp\Client() )->get( $url, [ 'verify' => true ] )->getBody()->getContents();
$file = str_finish(sys_get_temp_dir(), '/') . $info[ 'basename' ];
\File::put( $file, $contents );
$ext = ( new UploadedFile( $file, $info[ 'basename' ] ) )->guessExtension();

How to save a jpeg image from a base64 data string with move_uploaded_file or file_put_contents?

currently i developing a mobile app that able to snap photo and save in server. I able to pass the image base64 data string to my php using AJAX. I also can decode the base64 data string to image, but cannot save in my server. Every time i run my app, the result show that i already save my image into server but when i check in my server i didn't find it. I follow the example on https://xuri.me/2014/11/18/save-a-png-image-from-a-base64-data-string-with-php.html.
Below is my php code.
$img = $_POST['img'];
$img = str_replace('data:image/jpg;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$img_name = uniqid();
$file_name = $img_name . '.jpeg';
$file_path = realpath("../media/rewardreceipt/");
$file_path = $file_path . "/" . $file_name ;
if(file_put_contents($file_path , $data)) {
$arr = array('result' =>$file_name);
echo json_encode($arr);
} else{
$arr = array('result' =>'false');
echo json_encode($arr);
}

Laravel 4 upload 1 image and save as multiple (3)

I'm trying to make an image upload script with laravel 4. (using Resource Controller) and i'm using the package Intervention Image.
And what i want is: when uploading an image to save it as 3 different images (different sizes).
for example:
1-foo-original.jpg
1-foo-thumbnail.jpg
1-foo-resized.jpg
This is what i got so far.. it's not working or anything, but this was as far as i could get with it.
if(Input::hasFile('image')) {
$file = Input::file('image');
$fileName = $file->getClientOriginalName();
$fileExtension = $file->getClientOriginalExtension();
$type = ????;
$newFileName = '1' . '-' . $fileName . '-' . $type . $fileExtension;
$img = Image::make('public/assets/'.$newFileName)->resize(300, null, true);
$img->save();
}
Hopefully someone can help me out, thanks!
You may try this:
$types = array('-original.', '-thumbnail.', '-resized.');
// Width and height for thumb and resized
$sizes = array( array('60', '60'), array('200', '200') );
$targetPath = 'images/';
$file = Input::file('file')[0];
$fname = $file->getClientOriginalName();
$ext = $file->getClientOriginalExtension();
$nameWithOutExt = str_replace('.' . $ext, '', $fname);
$original = $nameWithOutExt . array_shift($types) . $ext;
$file->move($targetPath, $original); // Move the original one first
foreach ($types as $key => $type) {
// Copy and move (thumb, resized)
$newName = $nameWithOutExt . $type . $ext;
File::copy($targetPath . $original, $targetPath . $newName);
Image::make($targetPath . $newName)
->resize($sizes[$key][0], $sizes[$key][1])
->save($targetPath . $newName);
}
Try this
$file = Input::file('userfile');
$fileName = Str::random(4).'.'.$file->getClientOriginalExtension();
$destinationPath = 'your upload image folder';
// upload new image
Image::make($file->getRealPath())
// original
->save($destinationPath.'1-foo-original'.$fileName)
// thumbnail
->grab('100', '100')
->save($destinationPath.'1-foo-thumbnail'.$fileName)
// resize
->resize('280', '255', true) // set true if you want proportional image resize
->save($destinationPath.'1-foo-resize-'.$fileName)
->destroy();

Image is not saving into the defined folder

I am now troubling with this small error. I have a module which is using to upload images. Now this is my code for saving the file. It is defined in saveAction() method of controller.
<?php
/*rest part*/
$postData = $this->getRequest()->getPost();
if(isset($_FILES['banner_img_1']['name']) and (file_exists($_FILES['banner_img_1']['tmp_name'])))
{
try
{
$uploader = new Varien_File_Uploader('banner_img_1'); $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
$path = Mage::getBaseDir('media') . DS . 'banner' . DS ;
$uploader->save($path, $_FILES['banner_img_1']['name']);
$postData['banner_img_1'] = $_FILES['banner_img_1']['name'];
}
catch(Exception $e)
{
}
}
else
{
if(isset($postData['banner_img_1']['delete']) &&
$postData['banner_img_1']['delete'] == 1)
$postData['image_main'] = '';
else
unset($postData['banner_img_1']);
}
However this is not storing my image in media/banner folder. When I use path as
<?php
$path = Mage::getBaseDir('media') . DS ;
it saves image in media folder. I dont know the reason why it happens like this. when I print the $path variable, it currectly points towards media/banner folder. What is the error in my code? Please help me to solve this minor error. Thanks in advance..
Try this code in the try block {}
$path = Mage::getBaseDir('media') . DS . 'banner' . DS .'banner'.DS;
$uploader = new Varien_File_Uploader('banner_img_1');
$uploader->setAllowedExtensions(array('jpg','png','gif'));
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
$destFile = $path.$_FILES['banner_img_1']['name'];
$filename = $uploader->getNewFileName($destFile);
$uploader->save($path, $filename);
$post_data['banner_img_1']='banner/banner/'.$filename;

Resources