Chrome在内联PDF上显示“无法加载PDF文档”错误消息

kx1ctssn  于 2023-08-01  发布在  Go
关注(0)|答案(8)|浏览(350)

我在Chrome中使用PHP阅读PDF文件时遇到问题。
下面的代码是我如何在PHP中

  1. $path = "actually file path";
  2. header("Pragma: public");
  3. header("Expires: 0");
  4. header("Content-type: $content_type");
  5. header('Cache-Control: private', FALSE);
  6. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  7. header("Content-Disposition: inline; filename=\"$filename\"");
  8. header('Content-Transfer-Encoding: binary');
  9. header('Content-Length' . filesize($path));
  10. ob_clean();
  11. flush();
  12. readfile($path);

字符串
在这里,我将Content-Disposition设置为inline。因为我想显示pdf文件,如果用户浏览器有内置的pdf查看器插件。你可能知道,Chrome有内置的PDF查看器。
问题是我在服务器上有一堆PDF文件。只有一部分可以在Chrome浏览器上看到。我不明白为什么其他人不能以同样的方式工作。我已经检查了每个文件的权限。这似乎不是许可问题。
有谁知道是什么问题吗?- 谢谢-谢谢

dfddblmv

dfddblmv1#

我也一直在纠结这个问题。这是我在不同浏览器中得到的最接近一致结果的结果。我认为你可能遇到问题的原因是如果一些PDF文件太大,readfile()无法正确处理。试试这个:

  1. $file = "path_to_file";
  2. $fp = fopen($file, "r") ;
  3. header("Cache-Control: maxage=1");
  4. header("Pragma: public");
  5. header("Content-type: application/pdf");
  6. header("Content-Disposition: inline; filename=".$myFileName."");
  7. header("Content-Description: PHP Generated Data");
  8. header("Content-Transfer-Encoding: binary");
  9. header('Content-Length:' . filesize($file));
  10. ob_clean();
  11. flush();
  12. while (!feof($fp)) {
  13. $buff = fread($fp, 1024);
  14. print $buff;
  15. }
  16. exit;

字符串

展开查看全部
au9on6nz

au9on6nz2#

对我来说,添加以下标题修复了这个恼人的Chrome错误(?):

  1. header('HTTP/1.1 200 OK');

字符串

p4rjhz4m

p4rjhz4m3#

我有类似的问题,但我注意到顺序问题。似乎; filename=必须有引号围绕它,Content-Disposition: attachment试试这个:

  1. $file = "/files/test.pdf";
  2. $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
  3. $mime = finfo_file($finfo, $file);
  4. header('Pragma: public');
  5. header('Expires: 0');
  6. header('Content-Type: $mime');
  7. header('Content-Description: File Transfer');
  8. header('Content-Disposition: attachment; filename="'.basename($file).'"'));
  9. header('Content-Transfer-Encoding: binary');
  10. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  11. header('Content-Length' . filesize($file));
  12. ob_clean();
  13. flush();
  14. readfile($file);

字符串

展开查看全部
gg58donl

gg58donl4#

我已经修好了

  1. $path = 'path to PDF file';
  2. header("Content-Length: " . filesize ( $path ) );
  3. header("Content-type: application/pdf");
  4. header("Content-disposition: inline; filename=".basename($path));
  5. header('Expires: 0');
  6. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  7. ob_clean();
  8. flush();
  9. readfile($path);

字符串

krcsximq

krcsximq5#

有同样的问题,chrome没有显示内联PDF,卡在加载。解决方案是添加header('Accept-Ranges: bytes')
我的完整代码:

  1. header('Content-Type: application/pdf');
  2. header('Content-Disposition: inline; filename="'.$title.'"');
  3. header('Content-Transfer-Encoding: binary');
  4. header('Content-Length: '.filesize($file));
  5. header('Accept-Ranges: bytes');
  6. header('Expires: 0');
  7. header('Cache-Control: public, must-revalidate, max-age=0');

字符串

idv4meu8

idv4meu86#

在浪费了几个小时之后...我添加了评论,指出@Kal有唯一有效的解决方案。但不知何故,这还不够......当Chrome这样做时,这是一个不可能和令人沮丧的问题:
错误无法加载PDF文档。重新装填
x1c 0d1x的数据
这是结束折磨的差异。

  1. - // Waste time with chrome:
  2. - header("Content-type:application/pdf");
  3. - header("Content-Disposition:attachment;filename=$file_basename");
  4. - readfile($file);
  5. exit();
  6. ---------------------------
  7. + // Deliver the file:
  8. + header('Pragma: public');
  9. + header('Expires: 0');
  10. + header('Content-Type: $mime');
  11. + header('Content-Description: File Transfer');
  12. + header('Content-Disposition: attachment; filename="'.basename($file).'"');
  13. + header('Content-Transfer-Encoding: binary');
  14. + header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  15. + header('Content-Length'.filesize($file));
  16. + ob_clean();
  17. + flush();
  18. + readfile($file);
  19. exit();

字符串
大约30分钟,我愚弄了这个的各种变体......但我不能将其固定到“* 添加HTTP 200*”,不是“* 添加字节 ”,不是“ 引用您的文件名 ”,不是“ 分隔文件结尾 *”。这些都没用

  • (再次感谢@Kal).*
展开查看全部
6g8kf2rb

6g8kf2rb7#

我遇到了这个问题,挣扎了将近6个小时,终于让它工作了。我的解决方案与上述答案类似,但上述答案并不完整。有三个步骤来解决这个问题。

  • 第一步:* 在php.ini文件中添加这一行。
  1. output_buffering = False

字符串

  • 步骤2.* 如果您正在打开一个大的PDF文件,则会出现此错误。因此,为了解决这个问题,在添加标题之前,请确保您将这两行。
  1. set_time_limit(0);
  2. ini_set('memory_limit', '100M'); //the memory limit can be more or less depending on your file

  • 步骤3.* 添加下面的头和代码来读取文件,所以最终的代码会像这样。
  1. set_time_limit(0);
  2. ini_set('memory_limit', '100M');
  3. $file = "path/to/file.pdf";
  4. header('Content-Type: application/pdf');
  5. header('Content-Disposition: inline;
  6. filename="yourfilename.pdf"'); //not the path but just the name
  7. header('Content-Transfer-Encoding: binary');
  8. header('Content-Length: '.filesize($file));
  9. header('Accept-Ranges: bytes');
  10. header('Expires: 0');
  11. header('Cache-Control: public, must-revalidate, max-age=0');
  12. ob_clean();
  13. flush();
  14. readfile($file);
  15. exit();


100%工作溶液。如果你有任何问题,让我知道:)

展开查看全部
yftpprvb

yftpprvb8#

上面的解决方案对我来说都不起作用,但是下面的代码起作用了。你所需要做的就是按块发送文件。

  1. $fileSize = filesize($filePath);
  2. header("Expires: 0");
  3. header("Content-Type: application/pdf");
  4. header("Content-Description: File Transfer");
  5. header('Content-Disposition: inline; filename="' . $yourFileName.pdf . '"');
  6. header("Cache-Control: private, max-age=0, must-revalidate");
  7. header("Content-Length: $fileSize");
  8. header("Accept-Ranges: bytes");
  9. //Flush the output buffer immediately
  10. //The following three lines is important. Without them, it will not load large files:
  11. ob_end_flush();
  12. ob_flush();
  13. flush();
  14. $handle = fopen($filePath, "r");
  15. $chunkSize = 2024 * 2024; // 1MB (Adjust as needed)
  16. while (!feof($handle)) {
  17. echo fread($handle, $chunkSize);
  18. ob_flush();
  19. flush();
  20. }
  21. fclose($handle);
  22. exit();

字符串

展开查看全部

相关问题