Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-09-16
5
Medium Priority
?
324 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 111

Accepted Solution

by:
Ray Paseur earned 1600 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1600 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 400 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

PaperPort is a popular document imaging/management product from Nuance Communications (http://www.nuance.com/). It is in widespread use by both individuals (http://www.nuance.com/for-individuals/by-product/paperport/index.htm) and businesses (http:/…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

618 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