Solved

Displaying PDF embedded in PHP page with "Header", IE issue

Posted on 2011-09-16
5
289 Views
Last Modified: 2012-05-12
I have a PHP page that dynamically displays a PDF page embedded within a PHP using Headers as follows:

<?php
include "database_functions.php";
$query="select * from pdf_files where pid = '".postUrlStringValue('pid',null)."'";
$res=mysql_query($query,getdbHandle());
$row = mysql_fetch_assoc($res);
extract($row);
$filename = str_replace(" ", "_", $name);
if (isset($pdfdata)) {
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-type: application/pdf");
header("Content-Disposition:inline; filename=\"$filename\"");
echo $pdfdata;
}
?>

The $pdfdata is actually coming from a database where the pdf is stored, atlhough I could put the pdf files on the server and reference them in the database.  I would prefer to keep them in a database though.

The pdf's display fine in Firefox and and Safari, but there is an error in IE where the new window will pup up for a second and then close itself, no data displayed.  Wondering if I need to change/adjust the headers, if it is a IE PDF Plug-in issue or what.  I did have a statement in there with the strlen($pdfdata) for the size of the file and that did not seem to make any difference.

Thanks.
0
Comment
Question by:sscotti
5 Comments
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 36549412
not sure about the IE thing, but for what it's worth, my understanding is that storing files in the database itself slows things down a lot, so maybe it's better to reference the pdf, which would inadvertently solve your problem anyway...

Cheers.
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 400 total points
ID: 36549680
Here is how I force a download of a file.  The issue you are having may be related to the special header issues with IE.  Check the comments in this code. ~Ray
<?php // RAY_force_download.php
error_reporting(E_ALL);



// DEMONSTRATE HOW TO CAUSE A FILE DOWNLOAD



// REQUIRED FOR USE WITH THE PHP date() FUNCTIONS
date_default_timezone_set('America/New_York');

// A FILE TO DOWNLOAD - THIS LINK COULD COME IN THE URL VIA $_GET, OR COULD BE GENERATED INSIDE THE SCRIPT
$url = "http://www.LAPRBass.com/piechart.png";

// USE CASE
force_download($url);




// FUNCTION TO FORCE A DOWNLOAD FROM A FILE
function force_download($filename)
{
    // GET THE CONTENTS OF THE FILE
    $filedata = file_get_contents($filename);

    if ($filedata)
    {
        // GET A NAME FOR THE FILE
        $basename = basename($filename);

        // THESE HEADERS ARE USED ON ALL BROWSERS
        header("Content-Type: application-x/force-download");
        header("Content-Disposition: attachment; filename=$basename");
        header("Content-length: ".(string)(strlen($filedata)));
        header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
        header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");

        // THIS HEADER MUST BE OMITTED FOR IE 6+
        if (FALSE === strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE '))
        {
            header("Cache-Control: no-cache, must-revalidate");
        }

        // THIS IS THE LAST HEADER
        header("Pragma: no-cache");

        // FLUSH THE HEADERS TO THE BROWSER
        flush();

        // CAPTURE THE FILE IN THE OUTPUT BUFFERS - WILL BE FLUSHED AT SCRIPT END
        ob_start();
        echo $filedata;
    }

    // ERROR
    else
    {
        die("ERROR: UNABLE TO OPEN $filename");
    }
}

Open in new window

0
 
LVL 5

Author Comment

by:sscotti
ID: 36549730
Ray,

In this instance I am more interested in actually displaying the pdf inline with a browser plug-in.  It works as is in FireFox and Safari on both the Mac and Windows (with plug-in for firefox on Windows).  The URL is sscotti.com and just select one of the drop down options to test.
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 total points
ID: 36549789
Why not just give the client a standard <a href="/path/to/file.pdf">link</a>?  I think the design would be simpler that way.

But that said, try detecting the user agent and omitting the cache-control header for MSIE.
0
 
LVL 4

Assisted Solution

by:Fugas
Fugas earned 100 total points
ID: 36555732
Hi,

I would use an iframe and Rays code to display the pdf. Ray, sometimes you wont to have direct link to the file, maybe when you allow to download it just loged in user.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PDF files have been in the limelight due to its unmatched features.  Personal documents, emails, business reports and eBooks are all converted into PDF files owing to peerless features provided by it. Adding watermark to a PDF file is a method to se…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Sometimes we receive PDF files that are in the wrong orientation. They may be sideways or even upside down. This most commonly happens with scanned or faxed documents. It is possible to rotate the view of these PDFs with the free Adobe Reader produc…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question