使用cakephp在视图中渲染pdf

gut*_*uts 1 pdf cakephp render view

我的问题是我想在CakePhP视图中呈现pdf.

所以我有一些html/php的视图,在页面的末尾我想渲染一些pdf(我不想链接pdf,真的需要在同一页面上渲染它们).

如果您对如何做到这一点有任何想法会很棒:)

谢谢.

ndm*_*ndm 5

您不能混合HTML和PDF并期望浏览器正确显示它,这将无法正常工作.您必须将PDF转换为HTML,或在iframe中显示PDF.

对于后者工作,你通常需要发送一个Content-Disposition类型inline,当然它需要一个本机支持PDF或安装了适当的插件的浏览器.

这是一些基本的示例代码.在视图中:

<p>Some html</p>
<iframe width='123' height='456' src='/path/view_pdf'></iframe>
Run Code Online (Sandbox Code Playgroud)

然后在链接控制器中,动作响应内联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;
}
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请查看Cookbook.