1

Does anyone know how to get PDF.js load and display pages progressively when PDF file is output by PHP ? When called directly, the PDF file loads fine and is displayed progressively but when PHP is taking care of the output of the same PDF file, PDF.js waits for the entire file to be loaded before displaying the first page. I tried different headers such as these without success :

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

Thanks in advance for any help.

EDIT after @Rob's comment : here are the logs from the web browser :

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 Answer 1

5

Just adding the "Accept-Ranges" headers will not magically activate chunked responses. You have to recognize the HTTP Range header, seek in the file, then send this data together with a "206 Partial Content" status line and "Content-Range" header.

Here is sample code for streaming a MP4 file, it can almost directly be copy-pasted for PDFs. To have optimal display times, make sure that your PDF is linearized (also known as "Web optimized"). This is a feature of PDF document generators, which outputs the PDF stream in order so that all data required to render the first page(s) is available when the first chunks of the PDF file have been loaded.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you very much for answering. I tried this before without any success, still I adapted the script you linked to try again but it doesn't work any better, pdfjs is downloading the whole file before rendering... maybe the filename is important for pdfjs and is missing in the script you linked. if so what Content-Disposition would best fit for my purpose ?
@Olivier Show the network log of your browser's developer tools. I think that you're not sending the right response headers. Is the PDF linearized, as I suggested?
Thank you Rob, I added the logs to the original post for better display
The PDF is loading and displayed fine by pdfjs when call directly for testing purposes. for production, the file will be outside the webroot and might not be linearized by uploaders.
@Olivier The script I linked isn't mine, it's just the first reasonable hit I found after googling for "PHP range request". Edit the script and use Accept-Range: bytes instead of Accept-Range: 0-$length.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.