CakePdf won't generate pdf file inside a folder - cakephp

Im using cakepdf for generating pdf files using wkhtmltopdf and it works, the issue is when i wanted to create pdf files inside a folder it shows me this exception :
System error <pre>Exit with code 1 due to network error: ContentNotFoundError </pre>
Here's the code Im using for generating the pdf file :
$CakePdf = new CakePdf();
$CakePdf->template('mynote','default');
//get the pdf string returned
$pdf = $CakePdf->output();
//or write it to file directly
$pdf = $CakePdf->write(APP . 'webroot'. DS .'files' . DS . 'mynote.pdf');
and for the pdf template i used mynote.ctp that only contains this code inside pdf folder
<div>Hello</div>
After some hustle i disbaled this error by commenting the code inside output functions but still i can't get the style and the images,like i use to.
Thanks in advance.

Related

Block direct downloads using Symfony

I want block the direct access to some of public files from my directory. For example, I have the file image.png and I have to fill a captcha to download that file, if I fail the captcha I don't want that the user could access the file. Is there any way to do that in Symfony?
First of all, create an .htaccess file inside the image directory to prevent the direct access of files inside the directory.
Deny from All
Now, give customised path, through controller to download the file, where you can easily integrate a captcha by using some bundle like Gregwar's CaptchaBundle.
On successful captcha validation, download the file through Response.
$filename = __DIR__ . "../path_to_file/image.png";
// Generate response
$response = new Response();
// Set headers
$response->headers->set('Cache-Control', 'private');
$response->headers->set('Content-type', mime_content_type($filename));
$response->headers->set('Content-Disposition', 'attachment; filename="' . basename($filename) . '";');
$response->headers->set('Content-length', filesize($filename));
// Send headers before outputting anything
$response->sendHeaders();
$response->setContent(file_get_contents($filename));
Note : Code not tested!
Hope this helps!

Tinymce image upload not working in cakephp 3.( by JustBoil plugin)

I am trying to add image upload option in tinymce editor using this plugin.
I have downloaded tinymce and keep it in webroot/js/tinymce folder. Then I downloaded justboil plugin, and keep it in tinymce/plugin folder. Then I have tried this code in my add.ctp file
<?php echo $this->Form->input('details',['class'=>'tinymce']); ?>
I have used this js code below
<script>
tinymce.init({selector:'.tinymce',
plugins: "jbimages,code"
});
</script>
Not it's looking like below image and working fine and also saving code data in database table.
After trying a image upload it's giving a code like
In tinymce\plugins\jbimages\config.php file , I have given bellow setting
$config['img_path'] = '/tinymce/images';
$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . $config['img_path'];
// same code working fine in php, but not in cakephp. May anyone help me please ?
I have been solve this problem, but still not 100 % accuracy. I am shearing my ans and also need a help to define path.
After add relative_urls: false in tinymce.init
tinymce.init({selector:'.tinymce',
plugins: "jbimages,code",
relative_urls: false
});
and I have also changed upload directory in config.php
$config['img_path'] = '/photobag.in/photobag/img';
All is working fine. Here in config.php file how can I dynamically define base_url? For example here photobag.in is my project folder.It may I will change it.

CakePHP Upload Plugin: Programmatic File Retrieval without a Form

I use CakePHP-Upload plugin, now need to use the upload without form, following this example: Programmatic File Retrieval without a Form
All my upload are stored in the associated model, called the Attachment.
So when I save the article, at the same time save the images in Attachmen model.
Plugin documentation suggests something like this:
<?php
$this->Article->set(array('file' => $image_path)); // ?????
$this->Article->save();
?>
I have a larger collection of images on the server, previously uploaded via the Joomla CMS, now I have a migration of data to a custom CMS built on CakePHP framework. I need to take all these pictures and upload again, through this plugin.
How to properly use this plugin for my needs, I try to put the path to a local picture, but uploading is not working ???
EDIT
$image_path = WWW_ROOT . 'img' . DS . 'attachment' . DS . '59'.DS.'hpim4799.jpg';
debug($image_path);
'C:\xampp\htdocs\portal\webroot\img\attachment\59\hpim4799.jpg'
Do not save the record and not create new images.
Note, uploading through HTML form works well, I use windows 7 and xampp server.
EDIT 2 / Solution
Image path must be valid url like 'http://localhost/portal/img/attachment/59/hpim4799.jpg'
$image_path = Router::url('/', true) . 'img/attachment/59/hpim4799.jpg';
Thank you.
Can you tell me what the content of the variable $image_url is? The doucmentation says you can add a file via the form, or you can do it programmatically. If you do that, use the $image_url as path to the image. If you got an associated model like Attachment you shoud use:
<?php
$this->Article->set(array('Attachment.file' => $image_path)); // path + file to file
$this->Article->save();
?>
After hours of searching for solutions and detailed inspection of the Behavior code I finally found it.
Because Behavior uses php FILTER_VALIDATE_URL Filter, path must be a valid URL. Here is an example:
<?php
$image_path = Router::url('/', true) . 'img/attachment/59/hpim4799.jpg';
// return http://localhost/portal/img/attachment/59/hpim4799.jpg
$this->Article->Attachment->set(array('file' => $image_path));
$this->Article->Attachment->save();
?>

how to download excel file from php page using phpexcel

In our cakephp application there have a excel file downloading..
I just prepared a view page for that .
but i need to download that..
How can i do this?
I want to use the PHPExcel plugin for that....
This is the code I'm used for that:
/** PHPExcel_IOFactory */
include '../Vendor/PHPExcel/Classes/PHPExcel/IOFactory.php';
$objReader = PHPExcel_IOFactory::createReader('HTML');
$objPHPExcel = $objReader->load('../View/Events/settlement_report_excel.html');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save("myExcelFile.xlsx");
But it displayed ../View/Events/settlement_report_excel.ctp is an Invalid HTML file.
How can i overcome this issues...
You can create desired csv, xls using simple PHP in CakePHP environment. On execution of following code, desired excel file will be downloaded.
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=names.xls");
header("Pragma: no-cache");
header("Expires: 0");
$schema_insert = $firstname."\t".$lastname."\t".$fullname;
echo ($schema_insert . "\r\n");

Correct way in CakePHP 1.3 to create zip file and download without leaving view

I just upgraded from cakephp 1.1 to 1.3. I have everything on the site updated and working great, except for creating and downloading zip files.
Here is the code in my accounts_controller.php:
function zip() {
$this->checkSession();
$this->checkUpgradedAccount();
$files = array();
$this->layout="zip";
/*
code where I locate the files to zip, combine them, etc
*/
$tmp_file = "/home/[userdirectory]/tmp/".md5(mktime()).".zip"; //directory name edited
$command = "/usr/bin/zip -j $tmp_file ".implode(" ",$zip_files);
exec($command);
foreach($zip_files as $zf) {
unlink($zf);
}
$file_path = $tmp_file;
$this->set("path",$file_path);
$this->render();
}
When I call this action, however, I get an error:
Error: The requested address '/accounts/zip' was not found on this
server.
It worked just like this in version 1.1. I'm assuming something has changed, but I'm not sure what, and was unable to find anything pertinent in the documentation.
The zip.ctp view file does exists, but it has nothing in it other than: <?php ?>
I suspect something is different with layouts. There is NO "zip.ctp" in the /layouts directory. However, I have changed that line to $this->layout('default'); and it renders a blank page with NO ERROR, but also with no download.
Please direct me on the proper way to download my zip file in cake 1.3. Thanks in advance.
You have two different problems here. That error you're getting is because you don't have a zip layout file. As for your problem with getting the zip file, you should be using the media view class - http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Views.html#media-views

Resources