Saturday, January 30, 2016

CakePHP 2.xx rendering view of original method after saving pdf through second method

I am using CakePHP 2.xx and the following component - https://github.com/segy/Mpdf to render pdf's.


I have 2 Controller methods, the first method request_pdf calls a second method send_pdf in the same Controller. The second method creates and saves a pdf to file, and will then email the pdf (but for simplicity I have omitted this part of the code).


Once the second method send_pdf finishes creating the pdf control should be passed back to the first method request_pdf where it should then render on screen the view file in view/Documents/request_pdf.


The pdf correctly saves to the TMP directory, BUT when request_pdf renders in the browser, it renders the pdf on screen. The saving of the pdf seems to interfere with the rendering of the view.


Here is my Controller code


App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');

class DocumentsController extends AppController {

   
public $components = array('Mpdf');

// --------------------------------------------------
// beforeFilter
// --------------------------------------------------

   
public function beforeFilter()
   
{
        parent
::beforeFilter();
   
}

   
function request_pdf()
   
{
        $this
->send_pdf();

       
// have also tried this as described by the author of Mpdf component
       
// $result = $this->requestAction(array('action' => 'send_pdf/'), array('return', 'bare' => false));
   
}

   
function send_pdf()
   
{
        $this
->layout = null;
        $this
->Mpdf->init();

        $response
= $this->render('send_pdf');

        $thebody
= $response->body();
        $this
->Mpdf->WriteHTML($thebody);
        $this
->Mpdf->Output(TMP.'requested_pdf.pdf', 'F');

       
// have also tried this
       
// $this->Mpdf->setFilename(TMP.'requested_pdf.pdf');
       
// $this->Mpdf->setOutput('F');
   
}
}


I'm sure that it is something simple that I'm over looking here regarding the rendering of the original view, but I can't work it out.


I have also tried calling different methods in both the first and second methods without success,


$this->layout  $this->view  $this->viewPath  $this->render  $This->autoRender


If I remove $this->layout = null from send_pdf then I get both the pdf AND view from request_pdfrendered on the screen overlaying each other.


All help appreciated.


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at https://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: