Chrome displays an error message "Failed to load PDF document" on embedded PDF files

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

The following code is how I do in PHP

$path = "actually file path"; header("Pragma: public"); header("Expires: 0"); header("Content-type: $content_type"); header('Cache-Control: private', FALSE); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header("Content-Disposition: inline; filename=\"$filename\""); header('Content-Transfer-Encoding: binary'); header('Content-Length' . filesize($path)); ob_clean(); flush(); readfile($path); 

Here I set the Content-Disposition to a string. Because I want to display a pdf file if the plugin for viewing PDF files is built in the user's browser. As you know, Chrome has a built-in pdf viewer.

The problem is that I have a bunch of pdf files on the server. Chrome can only view some of them. I cannot understand why others cannot work the same way. I checked the resolution of each file. This does not seem to be a resolution problem.

Does anyone know what the problem is? Thanks.

+6
source share
5 answers

I struggled with the same problem. It is so close that I get consistent results in browsers. I think the reason you might be having problems is because some PDFs are too large for readfile () to process correctly. Try the following:

 $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; 
+10
source

I 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); 
+1
source

If the same problem, chrome would not display the embedded PDF file stuck on loading. The solution was to add header('Accept-Ranges: bytes') .

My full code is:

 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'); 
+1
source

I had a similar problem, but I noticed that order matters. It seems that ; filename= ; filename= should have quotes around it, Content-Disposition: attachment Try the following:

  $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
source

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

  header('HTTP/1.1 200 OK'); 
0
source

Source: https://habr.com/ru/post/885946/


All Articles