Question

On my website, I allow an admin to read a user's resumes. These resumes are stored outside of the root folder. In order to read them, I submit a form with the user's ID, get the path to their resume from the database and display it to the admin in a new tab as a PDF. Below is the code for displaying the PDF.

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

In order to submit the form to get to this code, I do the following in javascript.

$("#resume-user-id").val(ui.target.attr("id"));
$("#resume-form").submit();

And here is the form code.

<form id="resume-form" method="post" action="/account/profile/preview_resume.php" target="_blank">
    <input id="resume-user-id" type="hidden" name="user_id"></input> 
</form>

Now, this all works just just fine and dandy and the PDF is displayed correctly because I have hard coded in the file name and path. However, as soon as I access a post variable at all, the PDF doesn't load. Even if I keep the file name hard coded and just check if the post variables are set by using the isset function, everything just breaks. The page looks like it is trying to load a PDF, but it just keeps loading, and loading, and loading...

Any ideas what might be causing this?

--UPDATE--

I removed the header('Accept-Ranges: bytes'); line, and now it works fine (mostly). What exactly does this header do, and why would it be causing issues in this situation? I say 'mostly' fine because all but one of my test PDF's work. One of them however, I just get a message that says "Cannot load PDF"... Not sure why this is, or how it is different from the others.

Was it helpful?

Solution

Ok, I finally figured it out. After the update to the original question, I was still getting some errors with PDF files larger than 1MB. Chrome would not display the files and IE(compatibility mode) would corrupt them. I did not test it in firefox before finding a solution. My final script looks like this:

header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $file . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($path));
header('Accept-Ranges: none');
ob_clean();
flush();
readfile($path);

Note that $file is the name of the file (i.e. filename.pdf) and $path is the path to the file (i.e. C:/path/to/file/filename.pdf). To the original question, I added ob_clean(); and flush(); before the readfile call, and I set the 'Accept-Ranges' header to 'none'.

PDF file is now viewable in chrome and IE (compatibility mode). In firefox, the file actually downloads, but I am not too worried about that.

Similar question and answer found here: PHP stream PDF with fread and readfile produce a damaged pdf

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top