Skip to content
Advertisement

Chrome Download Button downloading PHP file instead of PDF

There are lots of similar questions to this online, none of them seem to be the exact same scenario nor do they provide any insight into what’s going on or how to resolve it. I hope I can provide additional info with my testing and narrow down to what I believe is happening and causing this issue.

I’m echoing and previewing a PDF file created via SSRS on the screen and most of the time I’m seeing the correct headers that explicitly I’m setting, but I have a single report type that seems to change the Content-Control header. When this happens, the download button in Chrome will then prompt to download my controller.php file instead of the PDF.

I’m using PHP running on an IIS server. Here are the headers I’m explicitly setting in my code:

   header('content-type:application/pdf');
   header('Cache-Control: store; cache; no-revalidate');
   header('Content-Disposition: inline; filename=' . $this->sFileName . '.pdf');

For most of the reports I output, the headers in the match those values. Here is an example of a Quote Report that outputs it correctly:

Quote Report

Here is the result when clicking the download button in the Chrome PDF Previewer:

Quote Download

In my testing of multiple reports, there is one in particular, the Order Report, that seems to always change the Content-Disposition header:

Order Report

Here is the result when clicking the download button in the Chrome PDF Previewer:

Controller.php Download

I ran a test where I commented out the Cache-Control header line that I am setting above and re-ran the Quote Report. I got the same results for the Cache-Control header that I’m getting for the Order Report and got the same results where it downloads my controller.php file instead of the PDF. That is why I believe it has something to do with that header that is causing this. There no where else I’m explicitly setting that header in my code something must be changing it. I’m hoping someone knows something about that header specifically in PHP that could be causing the value to change.

Thanks in advance!

Advertisement

Answer

I was able to get this resolved in my scenario.

What I ended up realizing was that every time I was printing the Order report, my headers were not being sent. Checking the status of headers_sent() on the final lines was returning false.

Now, why it was not sending the headers only for this one report type, I have no idea… From meticulously stepping through the lines of code over and over, everything appeared to be identical from when I was printing the Quote report. Yet the Quote report consistently sent the headers as expected and passing the $file, and $line params to headers_sent() output the correct file and line # that I was echoing the PDF coming back from SSRS. I noticed this because the Order report was hitting my session_start() code, where it shouldn’t have been if headers are output. It’s very strange because the Order report was still being output to the screen so I really have no idea why it thinks the headers weren’t being sent.

What I ended up doing was just adding an ob_flush() right after echoing the report to ensure the content and headers get sent. This seems to be working consistently.

If anyone has any insight as to why the headers were not being sent. I would be interested to hear in the comments.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement