/* This file is
This is an archive
Erythropoietin tre
Q: Getting this e
The role of the en
Q: Does .NET 4.0
Dedicated to the
In-depth: 'Rio', s
Introduction =====
# -*- coding: utf-


Golf at Augusta Na
In vitro and in vi
OAKLAND, Calif. –
Sidney Mier Sidne
This application r
Fetal echocardiogr
// // YJDemo1View
Sri Lanka: Massacr
#ifndef ROUTER_H #
Q: CakePHP: The view content is not a plain string I'm using CakePHP 1.3.10 and I am trying to send a pdf file to my user. I have made a view in the /app/View/Users/pdf.ctp in which there is a form that allows you to upload files:
Form->create('User', array('type' => 'file')); ?> Form->file('pdffile'); ?>
the controller looks like this: public function pdf(){ if($this->request->is('post')){ $this->User->create(); if($this->User->save($this->request->data)){ if ($this->request->data['User']['pdffile']['name'] != '') { $this->User->pdffile = $this->request->data['User']['pdffile']['name']; if (file_exists($this->User->pdffile)) { $file_dir = 'pdfs'; $this->pdf->setPath(WWW_ROOT . $file_dir . DS . $this->request->data['User']['pdffile']); $this->pdf->render(); $this->response->file($this->pdf); } else { $this->Session->setFlash('This pdf does not exist.'); } } } } } Now, I can see the pdf file on my browser, but when I submit the form it displays an error: Error: The view content is not a plain string. I have also tried this: $file = array(); $file['User']['pdffile']['name'] = $this->request->data['User']['pdffile']['name']; $file['User']['pdffile']['file'] = $this->request->data['User']['pdffile']['name']; $file['User']['pdf'] = $this->request->data['User']['pdffile']; print_r($this->pdf); exit(); and the data was displayed on the page, but there was no pdf... Please help. A: When you pass a file to CakePHP's pdf() function, it assumes you have a directory named "pdfs" with the exact same path as your view (which you do with your code). You probably don't want to put the file in that directory but you will probably want to add a route to the routing config that you'll have to do in Config/routes.php that goes to users/pdf. Here's an example of how you would do that: $route['users/pdf'] = array( 'controller' => 'users', 'action' => 'pdf', ); Your function call would then be this: $this->pdf($this->request->data['User']['pdffile']['name'], 'users/pdf'); And now all of this can be condensed into: public function pdf(){ if($this->request->is('post')){ $this->User->create(); if($this->User->save($this->request->data)){ if ($this->request->data['User']['pdffile']['name'] != '') { $this->User->pdffile = $this->request->data['User']['pdffile']['name']; if (file_exists($this->User->pdffile)) { $this->pdf($this->request->data['User']['pdffile']['name'], 'users/pdf'); } else { $this->Session->setFlash('This pdf does not exist.'); } } } } } To make this even more compact: public function pdf(){ if($this->request->is('post')){ $this->User->create(); if($this->User->save($this->request->data)){ if ($this->request->data['User']['pdffile']['name'] != '') { $this->User->pdffile = $this->request->data['User']['pdffile']['name']; if (file_exists($this->User->pdffile)) { $this->pdf($this->request->data['User']['pdffile']['name'], 'users/pdf'); } else { $this->Session->setFlash('This pdf does not exist.'); } } } } } public function pdf($pdffile, $controller = null) { if(is_null($controller) === true){ $controller = 'users'; } $file = array(); $file['User']['pdffile']['name'] = $pdffile; $file['User']['pdffile']['file'] = $pdffile; $file['User']['pdffile']['controller'] = $controller; $this->pdf($file); } Hope that gets you on the right track A: This solution is working: If you have no controller and you want to process your pdf files in pdf() action then put something like this $this->pdf($pdffile, 'user/mypdf');. It will take your pdf in same controller as current but not in current action (view) . Here I am taking pdf file which user uploaded, this file should be stored in specific location. If you have specific controller(user/pdf) and it has pdf action then you can call this function by call($pdf_name) This function has $this->Form->create() function and not render(); Controller: public function pdf(){ if($this->request->is('post')){ $this->User->create(); if($this->User->save($this->request->data)){ if ($this->request->data['User']['pdffile']['name'] != '') { $this->User->pdffile = $this->request->data['User']['pdffile']['name']; if (file_exists($this->User->pdffile)) { call($this->User->pdffile, 'user/pdf'); } else { $this->Session->setFlash('This pdf does not exist.'); } } } } } PDF file: public function call($pdf_name, $controller = null) { $file = array(); $file['User']['pdffile']['name'] = $pdf_name; $file['User']['pdffile']['file'] = $pdf_name; $file['User']['pdffile']['controller'] = $controller; $this->pdf($file); }