简体   繁体   中英

Render pdf in view with cakephp

My problem is that I want to render a pdf inside a CakePhP view.

So I have a view with some html/php and at the end of the page I want to render some pdfs (I don't want to link pdf, really need to render them on the same page).

If you have any idea on how to do it that would be great :)

Thank you.

You cannot mix HTML and PDF and expect the browser to display it correctly, that won't work. You'll either have to convert the PDF to HTML, or display the PDF in an iframe.

For the latter to work you usually need to send a Content-Disposition of type inline , and of course it requires a browser that either supports PDF natively, or has an appropriate plugin installed.

Here's some basic example code. In the view:

<p>Some html</p>
<iframe width='123' height='456' src='/path/view_pdf'></iframe>

And then in the linked controller action respond with inline PDF data, which is pretty easy in Cake 2.x:

public function view_pdf()
{
    $this->response->file('/path/to/the/file.pdf');
    $this->response->header('Content-Disposition', 'inline');
    return $this->response;
}

For more information check the Cookbook .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM