Chrome has “Failed to load PDF document” error message on inline PDFs

前端 未结 5 833
无人共我
无人共我 2020-12-07 02:15

I have a problem with reading pdf file in Chrome by using PHP.

The following code is how I do in PHP

$path = \"actually file path\";
header(\"Pragma:         


        
相关标签:
5条回答
  • 2020-12-07 02:24

    i've fixed this way

    $path = 'path to PDF file';
    header("Content-Length: " . filesize ( $path ) ); 
    header("Content-type: application/pdf"); 
    header("Content-disposition: inline; filename=".basename($path));
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    ob_clean();
    flush();
    readfile($path);
    
    0 讨论(0)
  • 2020-12-07 02:26

    I've been wrestling with this same issue. This is as close as I got to consistent results across browsers. I think that the reason you could be having problems is if some PDF's are too large for readfile() to handle correctly. Try this:

    $file = "path_to_file";
    $fp = fopen($file, "r") ;
    
    header("Cache-Control: maxage=1");
    header("Pragma: public");
    header("Content-type: application/pdf");
    header("Content-Disposition: inline; filename=".$myFileName."");
    header("Content-Description: PHP Generated Data");
    header("Content-Transfer-Encoding: binary");
    header('Content-Length:' . filesize($file));
    ob_clean();
    flush();
    while (!feof($fp)) {
       $buff = fread($fp, 1024);
       print $buff;
    }
    exit;
    
    0 讨论(0)
  • 2020-12-07 02:34

    I had similar issue but I noticed the order matters. Seems that ; filename= must have quotes around it, Content-Disposition: attachment Try this:

        $file = "/files/test.pdf";
        $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
        $mime = finfo_file($finfo, $file);
    
        header('Pragma: public');
        header('Expires: 0');
        header('Content-Type: $mime');
        header('Content-Description: File Transfer');
        header('Content-Disposition: attachment; filename="'.basename($file).'"'));
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Content-Length' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
    
    0 讨论(0)
  • 2020-12-07 02:37

    For me adding the following header fixed this annoying Chrome bug (?):

        header('HTTP/1.1 200 OK');
    
    0 讨论(0)
  • 2020-12-07 02:39

    Had the same problem, chrome didn't display the inline PDF, stuck at loading. The solution was to add header('Accept-Ranges: bytes').

    My complete code:

    header('Content-Type: application/pdf');
    header('Content-Disposition: inline; filename="'.$title.'"');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.filesize($file));
    header('Accept-Ranges: bytes');
    header('Expires: 0');
    header('Cache-Control: public, must-revalidate, max-age=0');
    
    0 讨论(0)
提交回复
热议问题