PDF.js的PHP标头可以逐步加载和显示

时间:2014-06-23 08:41:42

标签: php pdf.js

当PHP输出PDF文件时,是否有人知道如何使PDF.js加载并逐页显示页面?直接调用时,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

1 个答案:

答案 0 :(得分:3)

添加“Accept-Ranges”标题不会神奇地激活分块响应。您必须识别HTTP Range标头,在文件中搜索,然后将此数据与“206 Partial Content”状态行和“Content-Range”标头一起发送。

Here is sample code for streaming a MP4 file,它几​​乎可以直接复制粘贴为PDF。要获得最佳显示时间,请确保PDF已线性化(也称为“Web优化”)。这是PDF文档生成器的一项功能,它按顺序输出PDF流,以便在加载PDF文件的第一个块时可以呈现呈现第一页所需的所有数据。