tags:

views:

59

answers:

2

I am using the TCPDF app in CakePHP as a vendor. I have everything setup according to the instructions. I am able to produce a valid PDF and it works great. Now I want to take the results of an HTML rendered view that already exists and create a PDF from it.

I set the HTML variable and use that in the view as: $pdf->writeHTML($html, true, false, true, false, '');

Here is what I have tried and it renders a blank screen:

ob_start();
$this->render('results', 'pdf');
$data = ob_get_contents();
ob_end_clean();
$this->set('html', $data);

I have also tried:

$data = $this->results();
$this->set('html', $data);

But it only shows the PDF with the word OBJECT in it.

How can I render an existing view but have the output directed to the PDF?

A: 

Try using Controller's afterFilter callback and output attributes. Here is an example:

function afterFilter(){
    $pdf->writeHTML($this->output, true, false, true, false, '');
}
Jamal Aziz
This didn't work... but at the same time, this would run after EVERY method in the controller. That's not a side affect I want either. But thanks for taking a shot at trying to help.
cdburgess
+1  A: 

I would do

ob_start();
....
ob_end_clean();

in the layout. For me it's too early to collect the data in the controller :)

Basically, I am using wkhtmltopdf to generate PDF content. It's really standalone and produces the html as it look in a modern browsers.

how I using it: There is a pdf.ctp layout where I am using ob_start() ... ob_end_clean() in order to collect the content (incl the layout).

then I run wkhtmltopdf command with the output of the previously generated html, then read this file and print it in the browser.

You can see my /app/views/layouts/pdf.ctp here. There are comments so you should understand it.

With this approach I could run my entire app in PDF :)

I haven't use the TCPDF recently so I don't know what he is doing. It could be similar :)

I've build a site with this approach: http://html-2-pdf.com so you can see this in action.

HTH

Nik
It turns out this is similar to the approach I have to take. It's VERY sloppy the way I did it (I think), but it is working. I had to set a variable in the existing view. If the variable is set to 'pdf', then I run the ob_start() and other commands necessary to generate the PDF. But I feel it makes the view very messy. I would prefer to have it generated from another location. Here's to my OCD!
cdburgess