Google Chrome中的PHP内联PDF输出-PDF超过150k的问题


PHP inline PDF output in Google Chrome - issue with PDFs above 150k

我无法使用默认的PDF查看器插件和以下的PHP代码在Google Chrome中内联输出大于150k的PDF

$size = filesize($file_fullpath);
$begin = 0;
$end = $size;
header('HTTP/1.0 200 OK');
header("Content-Type: $mimeType");
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: no-cache');
header('Accept-Ranges: bytes');
header('Content-Length:' . ($end - $begin));
header("Content-Range: bytes $begin-$end/$size");
header("Content-Disposition: inline; filename='$filename'");
header("Content-Transfer-Encoding: binary'n");
header("Last-Modified: $time");
header('Connection: close');
$cur = $begin;
fseek($fm, $begin, 0);
while (!feof($fm) && $cur < $end && (connection_status() == 0)) {
   print fread($fm, min(1024 * 16, $end - $cur));
   $cur+= 1024 * 16;
}

所有功能都可以与FireFox或Internet Explorer配合使用。

好的,我解决了问题。它看起来像默认的谷歌Chrome PDF浏览器插件不喜欢它如何"服务"数据,它看起来像下面的简化代码工作良好

header('HTTP/1.0 200 OK');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: no-cache');
header("Content-Type: $mimeType");
header("Content-Disposition: inline; filename='$filename'");
header('Accept-Ranges: bytes');
header("Content-Transfer-Encoding: binary");
header('Content-Length:' . filesize($file_fullpath));
// output the file first clean mem
ob_clean();
flush();
while (!feof($fm)) {
   $mem_chunks = fread($fm, 1024);
   print $mem_chunks;
}
exit;

这是适用于Windows和Mac OS X、x86和x64版本的Google Chrome(49.xx版本)的一些早期版本的问题,但在最新版本中已修复。这是一个重新引入的错误。

因此,请检查您是否正在使用最新版本的谷歌浏览器。

更新(2016年4月8日):

  • 谷歌Chrome的v.42在地址栏中出现数据URI,数据大于128KB(base64编码数据),因此默默失败
  • v.49(如评论中所述)在datauri大于约150KB时失败