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

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

我在Chrome中使用PHP阅读PDF文件时遇到问题。
下面的代码是我如何在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);

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

dfddblmv

dfddblmv1#

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

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

字符串

au9on6nz

au9on6nz2#

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

header('HTTP/1.1 200 OK');

字符串

p4rjhz4m

p4rjhz4m3#

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

$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);

字符串

gg58donl

gg58donl4#

我已经修好了

$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);

字符串

krcsximq

krcsximq5#

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

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');

字符串

idv4meu8

idv4meu86#

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

-        // Waste time with chrome:
-        header("Content-type:application/pdf");
-        header("Content-Disposition:attachment;filename=$file_basename");
-        readfile($file);
         exit();
---------------------------

+        // Deliver the 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);
         exit();

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

  • (再次感谢@Kal).*
6g8kf2rb

6g8kf2rb7#

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

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

字符串

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

  • 步骤3.* 添加下面的头和代码来读取文件,所以最终的代码会像这样。
set_time_limit(0); 
ini_set('memory_limit', '100M');

$file = "path/to/file.pdf";
header('Content-Type: application/pdf');
header('Content-Disposition: inline; 
filename="yourfilename.pdf"'); //not the path but just the name
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');
ob_clean();
flush();
readfile($file);
exit();


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

yftpprvb

yftpprvb8#

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

$fileSize = filesize($filePath);
header("Expires: 0");
header("Content-Type: application/pdf");
header("Content-Description: File Transfer");
header('Content-Disposition: inline; filename="' . $yourFileName.pdf . '"');
header("Cache-Control: private, max-age=0, must-revalidate");
header("Content-Length: $fileSize");
header("Accept-Ranges: bytes");
     
//Flush the output buffer immediately
//The following three lines is important. Without them, it will not load large files:
ob_end_flush();
ob_flush();
flush();

$handle = fopen($filePath, "r");
$chunkSize = 2024 * 2024; // 1MB (Adjust as needed)
while (!feof($handle)) {
    echo fread($handle, $chunkSize);
    ob_flush();
    flush();
}
fclose($handle);
exit();

字符串

相关问题