Php 将PDF下载到浏览器

Php 将PDF下载到浏览器,php,Php,我有一个PHP web应用程序。我构建了一个从API端点下载PDF的函数,如下所示: public function importPdf($id) { $resource = fopen('tmp/'.$id.'.pdf', 'w'); $this->client->request('GET', $this->url . '/api/users/' . $id . '/pdf', [ 'headers' => [

我有一个PHP web应用程序。我构建了一个从API端点下载PDF的函数,如下所示:

public function importPdf($id)
{
    $resource = fopen('tmp/'.$id.'.pdf', 'w');

    $this->client->request('GET', $this->url . '/api/users/' . $id . '/pdf', [
        'headers' => [
            'Authorization' => "Bearer {$this->accessToken()}",
        ],
        'sink' => $resource,
    ]);
}

这是调用一个Guzzle实例,该实例工作正常,正在将PDF文件保存到我的服务器。但我需要的是将PDF文件下载到用户浏览器中。如果有人能解释一下如何做到这一点,我将不胜感激。

这就是你想要的吗?我使用它允许用户从浏览器下载某些文件

public function functionNameHere($filename){
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    ob_clean();
    flush();
    readfile($filename); 
}

您可以将其保存在web服务器可以访问的目录中,或者通过适当的http头对其进行流式处理(这是,IMHO,这里有点太宽了,无法回答)。无论如何,请看一看,这可能会有所帮助。