IE (HTTPS): generating pdf from php file doesn't work
Asked Answered
L

6

11

Here is my issue. I am trying to call a page: foo.php?docID=bar and return a PDF to the screen which is stored as a BLOB in the DB.

Here is the portion of my code which actually returns the PDF:

$docID = isset($_REQUEST['docID']) ? $_REQUEST['docID'] : null;

if ($docID == null){
    die("Document ID was not given.");
}

$results = getDocumentResults($docID);

if (verifyUser($user, $results['ProductId'])){
    header('Content-type: application/pdf');
    // this is the BLOB data from the results.
    print $results[1];
}
else{
    die('You are not allowed to view this document.');
}

This works perfectly fine in Firefox.

However, in IE, it doesn't show anything at all. If i'm on another page (i.e. google.com), and I type in the URL to go to this page, it will say it's done, but I will still have google.com on my screen.

I checked the headers for the responses from both firefox and IE. They are identical.

Does anyone have any suggestions? Need more information?

EDIT: If it helps at all, here's the response header and the first line of the content:

HTTP/1.1 200 OK
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 349930
Content-Type: application/pdf
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: PHP/5.1.2
Set-Cookie: PHPSESSID=cql3n3oc13crv3r46h2q04dvq4; path=/; domain=.example.com
Content-Disposition: inline; filename='downloadedFile.pdf'
X-Powered-By: ASP.NET
Date: Tue, 21 Apr 2009 16:35:59 GMT

%PDF-1.4

EDIT: Also, the page which pulls out the pdf file actually uses HTTPS instead of HTTP.

Thanks in advance,

~Zack

Lonergan answered 21/4, 2009 at 15:49 Comment(1)
I figured out the problem. See my explanation belowLonergan
L
20

I figured out what the issue was. It's an IE bug dealing with IE, HTTPS and addons. (See here)

It was a caching issue. When I set:

  header("Cache-Control:  max-age=1");
  header("Pragma: public");

(see here), the PDF was in cache long enough for the adobe reader add-on to grab it.

Lonergan answered 21/4, 2009 at 18:35 Comment(4)
For any .NET users who stumble upon this or a similar issue, see here: #1039207Ibnsaud
+1 - Had a similar problem with a csv file. This fixed it. Thanks!Lifegiving
+1000000 - Searched high and low and this is the only combo that solved it for me!!Lissa
And you need to disable the option "Do not save encrypted pages to disk"!Bierman
E
3

I had this issue too, i used the following which seems to work fine

header("Content-type: application/pdf");
header("Content-Length: $length");
header("Content-Disposition: inline; filename='$filename'");
Eisen answered 21/4, 2009 at 16:11 Comment(1)
This doesn't seem to work either. The content length excludes the header, right? Also, the $filename is just arbitrary, correct? I just called it downloadedFile.pdf.Lonergan
H
2

Try this:

 header("Content-Type: application/pdf");
 header("Content-Disposition: inline; filename=foo.pdf");
 header("Accept-Ranges: bytes");
 header("Content-Length: $len");
 header("Expires: 0");
 header("Cache-Control: private");

Also, if you are using sessions, you can try setting

session_cache_limiter("none");

or

session_cache_limiter("private");
Hypoblast answered 21/4, 2009 at 18:27 Comment(0)
R
2
if ( USR_BROWSER_AGENT == 'IE' ) {
    header( 'Content-Disposition: inline; filename="' . $name . '"');
    header( 'Expires: 0' );
    header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
    header( 'Pragma: public' );
} else {
    header( 'Content-Disposition: attachment; filename="' . $name . '"' );
    header( 'Expires: 0' );
    header( 'Pragma: no-cache' );
}
Rawdin answered 2/7, 2011 at 0:24 Comment(0)
J
0

This was the only header I needed to change:

header("Pragma: public");
Jade answered 22/3, 2011 at 19:26 Comment(0)
S
-2

I think you need to add more headers.

header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=THEFILENAME.pdf;");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . strlen($results[1]));
Sitwell answered 21/4, 2009 at 15:52 Comment(4)
I'm not trying to create it as a download (ie. have that save window pop up). I just want it to be viewable by adobe reader within the browser.Lonergan
Try taking out the force-download and download headers thenSitwell
Are you allowed to use more than one Content-type header?Upton
Also, "force-download" and "download" don't seem to be registered application media types: iana.org/assignments/media-types/applicationUpton

© 2022 - 2024 — McMap. All rights reserved.