【问题标题】:Downloading PDF to Browser将 PDF 下载到浏览器
【发布时间】:2018-12-11 23:21:18
【问题描述】:

我有一个 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 文件下载到用户浏览器。如果有人能解释如何做到这一点,我将不胜感激。

【问题讨论】:

  • 要么将其保存在 Web 服务器可访问的目录中,要么将其流式传输并提供适当的 http 标头(恕我直言,此处无法回答)。
  • 不管怎样,看看here,可能会有帮助。

标签: php


【解决方案1】:

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

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); 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-03
    • 2013-03-22
    • 1970-01-01
    • 2016-03-24
    • 2018-05-15
    • 2020-11-20
    • 2012-12-21
    相关资源
    最近更新 更多