문제

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?

도움이 되었습니까?

해결책

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, '');
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top