谷歌浏览器中的PHP内嵌PDF输出 - 使用高于150k的PDF文件 [英] PHP inline PDF output in Google Chrome - issue with PDFs above 150k

查看:143
本文介绍了谷歌浏览器中的PHP内嵌PDF输出 - 使用高于150k的PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm not able to inline output PDFs bigger than 150k in Google Chrome using default PDF Viewer plugin and PHP code below

$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均可正常工作。

All work fine with FireFox or Internet Explorer.

推荐答案

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

OK, I got issue resolved. It looks like default Google Chrome PDF Viewer plugin doesn't like it how does the data are "served" and it looks like simplified code below works fine

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;

这篇关于谷歌浏览器中的PHP内嵌PDF输出 - 使用高于150k的PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆