【问题标题】:PHP forces PDF file download even though I'm using Content-Disposition: inline即使我使用的是 Content-Disposition: inline,PHP 也会强制下载 PDF 文件
【发布时间】:2013-10-15 20:28:31
【问题描述】:

如果可能,我会尝试在浏览器中显示 PDF——我知道我可以在 Chrome 中执行此操作,这就是我正在测试的内容。问题是,每次我尝试时,它都会提示下载而是。

我正在使用 PHP 会话,所以我知道发送了一些无关的标头,因此我致电 header_remove() 重置所有内容。

我调用这个函数来显示 PDF:

<?php
// For demonstrative purposes
session_start();

if (!isset($_SESSION['auth'])) {
    header('Location: login.php');
    die;
}

/*
 * void viewPDF (Report $report)
 * Outputs the PDF of the report
 */
function viewPDF ($report) {
        // Tell the browser we are going to serve a PDF file.
    $file = dirname(__FILE__).'/../reports/'.$report->id.'.pdf';
        // The location of the PDF
    if (!file_exists($file)) {
        die ('The PDF does not exist.');
            // Somehow the file does not exist.
    }

    header_remove();
        // I'm using PHP sessions, so remove the headers
        // automatically set that might break something.
    header('Content-Disposition: inline;filename='.$report->id.'.pdf');
    header('Content-Transfer-Encoding: binary');
    header('Content-Type: application/pdf');
    header('Content-Length: '.filesize($file));
    readfile($file);
        // Serve the report PDF file from the reports
        // repository.
    die;
        // Any whitespace could corrupt the PDF, so be extra
        // sure nothing else gets printed.
}

// For demonstrative purposes:
$report = new StdClass;
$report->id = 1;
viewPDF($report);
?>

这些是正在发送的标头:

Date: Tue, 08 Oct 2013 18:41:32 GMT
Server: Apache/2.2.22 (Win32) PHP/5.4.15
Content-Type: application/pdf
Content-Transfer-Encoding: binary
Content-Disposition: inline;filename=1.pdf
Connection: Keep-Alive
Keep-Alive: timeout=5, max=100
Content-Length: 73464

它仍然提示下载。下载后,我可以在 Adob​​e Reader 中打开它。

我错过了什么吗?

谢谢。

【问题讨论】:

  • 不是每个人都喜欢在浏览器中查看 PDF(我就是其中之一)。我宁愿单独下载和查看/打开它。如果浏览器被配置为不这样做,则不能强制浏览器查看内联文件。
  • 尝试在readfile之前添加这个:header('Accept-Ranges: bytes');
  • @MarcB:用户有两种选择,但客户希望它可以从浏览器中查看。我可以直接在我的 Chrome 浏览器中访问 PDF。一定有一个标题问题,因为如果我可以使用 Chrome 查看任何 PDF,那么我必须能够模拟查看任何其他 PDF……我是否需要将 REQUEST_URI 更改为 PDF 文件扩展名,也许?那会很痛苦....
  • @MehdiKaramosly:我确实尝试过,但没有任何区别。
  • stackoverflow.com/questions/4679756/… 不知道什么可以解决它,Marc B 可能是对的。

标签: php pdf http-headers


【解决方案1】:

这段代码对我有用:

    header('Content-Description: File Transfer');
    header('Content-Type: application/pdf');
    header('Content-Disposition: inline; filename="' . basename($file).'"');
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);

【讨论】:

  • 嗯,还是不行。我什至尝试过删除会话,清除标题,并完全按原样使用您的代码....
  • 您使用的是什么浏览器?我试过 Chrome 和 IE,它就像一个魅力 :)
  • 我真的不知道这是怎么发生的,但我的 Chrome PDF 查看器插件和 Adob​​e Reader 插件都在我的 Chrome 上被禁用。一切正常!非常感谢
猜你喜欢
  • 1970-01-01
  • 2010-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-13
  • 2013-05-12
  • 1970-01-01
相关资源
最近更新 更多