简体   繁体   English

用cakephp渲染pdf

[英]Render pdf in view with cakephp

My problem is that I want to render a pdf inside a CakePhP view. 我的问题是我想在CakePhP视图内渲染pdf。

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). 所以我有一个带有一些html / php的视图,并且在页面的末尾我想呈现一些pdf(我不想链接pdf,确实需要在同一页面上呈现它们)。

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. 您无法将HTML和PDF混合使用,并期望浏览器正确显示它,这将无法正常工作。 You'll either have to convert the PDF to HTML, or display the PDF in an iframe. 您要么必须将PDF转换为HTML,要么在iframe中显示PDF。

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. 为了使后者正常工作,您通常需要发送一个inline类型的Content-Disposition ,当然,它需要一个本机支持PDF或已安装适当插件的浏览器。

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: 然后在链接的控制器操作中,用内联PDF数据进行响应,这在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 . 有关更多信息,请参阅食谱

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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