Solved

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

Posted on 2011-09-16
5
277 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 108

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 108

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In a previous article published here at Experts Exchange, Signature Image with Transparent Background (http://www.experts-exchange.com/Web_Development/Document_Imaging/A_12380-Signature-Image-with-Transparent-Background.html), I explained how to cre…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now