PHP头为PDF.js加载和显示逐步


PHP headers for PDF.js to load and display progressively

有人知道如何获得PDF.js加载和显示页面时,PDF文件是由PHP输出?当直接调用时,PDF文件加载良好并逐步显示,但当PHP处理相同PDF文件的输出时,PDF.js等待整个文件加载后才显示第一页。我尝试了不同的标题,但没有成功:

$file = 'big.pdf';
$filename = 'fakefilename.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
@readfile($file);

提前感谢您的帮助。

在@Rob的评论后编辑:以下是来自web浏览器的日志:

Accept-Ranges   0-23822222
Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection  Keep-Alive
Content-Length  23822222
Content-Range   bytes 0-23822221/23822222
Content-Type    application/pdf
Date    Mon, 23 Jun 2014 13:06:33 GMT
Expires Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive  timeout=15, max=99
Pragma  no-cache
Server  Apache/2.2.16 (Debian)
X-Powered-By    PHP/5.3.3-7+squeeze19

仅仅添加"Accept-Ranges"头不会神奇地激活分块响应。您必须识别HTTP范围标头,在文件中查找,然后将此数据与"206部分内容"状态行和"内容范围"标头一起发送。

这是一个示例代码流的MP4文件,它几乎可以直接复制粘贴pdf。为了获得最佳的显示时间,请确保您的PDF是线性化的(也称为"Web优化")。这是PDF文档生成器的一个特性,它按顺序输出PDF流,以便在PDF文件的第一个块加载后,呈现第一页所需的所有数据都可用。