首页 > 解决方案 > Firefox 仅通过 php 读取 pdf 一次

问题描述

通过单击按钮,PDF 文档将以另一种固定形式显示。php 文件如下所示:

// filename is read_pdf.php

    $path = '../../pdfs';
    $code = filter_input(INPUT_GET, 'ac', FILTER_SANITIZE_NUMBER_INT);

    if (isset($antcode) ) {

        $a = ... get the pdf-name from a mysql db as a function auf $code;

        $file = $path . '/' . $a;
    // Header content type 
        header('Content-type: application/pdf');
        header('Content-Disposition: inline; filename="' . $a . '"');
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: ' . filesize($file));
        header('Cache-Control: private, max-age=0, must-revalidate');
        header('Pragma: public');
        header('Accept-Ranges: bytes');

        @readfile($file);
    }

在 jQuery 中,我开始文件读取过程:

var url ="php/read_pdf.php?ac=" + r.randid;
window.open(url,'_blank');  

Chrome 一切顺利。使用 Firefox,它可以与“localhost”一起正常工作。但在服务器上,Firefox 设法读取 pdf 一次。使用下一个 pdf 不会加载更多内容。如果我删除缓存,则可以再次加载一个 pdf。我玩过“缓存控制”,但没有任何帮助。

可能是什么问题?

标签: phppdffirefoxreadfilewindow.open

解决方案


问题是只有 Firefox 需要 ob_clean 和 flush 命令。将代码更改为:

 header('Content-type: application/pdf');    
 header('Content-Length: ' . filesize($file));
 ob_clean();
 flush();
 readfile($file);

现在它可以工作了。


推荐阅读