CakePhp 4 add“Content-Type:text/html”当我尝试显示PDF时

5w9g7ksd  于 2023-04-30  发布在  PHP
关注(0)|答案(1)|浏览(209)

我试图显示一个PDF内联,但我得到的是二进制代码。我下面展示的mock在webroot中的一个简单的php文件中工作正常,但显示的是来自实体模板的二进制代码。

<?php
header("Content-type: application/pdf"); 
header('Content-Disposition: inline; filename="file.pdf"');    
header("Cache-control: private");
@ readfile('/opt/demo/webroot/document.pdf');
?>

而且我的布局在Headers定义之前没有额外的字符:

<?=$this->fetch('content')?>

我从get_headers()中得到了下面的信息:
数组([0] =〉HTTP/1。1 302找到[1] =〉日期:Tue,25 Apr 2023 03:42:42 GMT [2] =〉服务器:Apache [3] =〉过期:1981年11月19日星期四08:52:00 GMT [4] =〉缓存控制:no-store,no-cache,must-revalidate [5] =〉Pragma:no-cache [6] =〉Set-Cookie:PHPSESSID=givi13cckql8v0oq0lnsvn2rvb;path=/;SameSite=Lax [7] =〉Set-Cookie:csrfToken=wbFIJHOSq0HE1K4d0eYljTI2YzUzOWQwMTEyMGEwYjNmMTVhMmM0NTdiYmIxNjU0YTI3MTY3MzE%3D;path=/; HttpOnly [8] =〉Location:/login?redirect=http%3A%2F%2F107.23.196.88%2Fdocuments%2Fshow%2Ff198a0d3-ffd3-44e3-8715-8f5ed8a05185 [9] =〉Content-Length:0 [10] =〉连接:close [11] =〉**Content-Type:text/html;**charset=UTF-8 [12] =〉HTTP/1.1 200 OK [13] =〉日期:Tue,25 Apr 2023 03:42:43 GMT [14] =〉服务器:Apache [15] =〉Expires:Thu,19 Nov 1981 08:52:00 GMT [16] =〉缓存控制:no-store,no-cache,must-revalidate [17] =〉Pragma:no-cache [18] =〉Set-Cookie:PHPSESSID=avupp24r2uqf66i6d95n6t5hpk;path=/;SameSite=Lax [19] =〉Set-Cookie:csrfToken= rfiGOmviqYjfyyyyZbtXVzQ4OTAxNzc5OTc4ZjRmNmI2YmI3ZGZ10WMwYWQ5YWVjMDayOTg2YmM%3D;path=/; HttpOnly [20] =〉变化:Accept-Encoding [21] =〉Connection:close [22] =〉Transfer-Encoding:chunked [23] =〉Content-Type:text/html; charset=UTF-8)?〉
是否有必要进行另一个更改以在模板中使用标题?
先谢谢你了

8oomwypt

8oomwypt1#

我可以用Cake\Http\Response::withFile()方法解决这个问题。

public function show ($id){
    $path = '/home/ubuntu/receipts/' . $id . '.pdf';
    $response = $this->response->withFile($file['path']);
    return $response;
}

感谢您的帮助@ndm

相关问题