开发者

How can I render existing view for output to PDF?

开发者 https://www.devze.com 2023-01-13 23:40 出处:网络
I am using the TCPDF app in CakePHP as a vendor. I have everything setup according to the instructions. I am able to pro开发者_开发百科duce a valid PDF and it works great. Now I want to take the resul

I am using the TCPDF app in CakePHP as a vendor. I have everything setup according to the instructions. I am able to pro开发者_开发百科duce 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?


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


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

function afterFilter(){
    $pdf->writeHTML($this->output, true, false, true, false, '');
}
0

精彩评论

暂无评论...
验证码 换一张
取 消