Solved

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

Posted on 2011-09-16
5
304 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 110

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 110

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
migrating to phpbb forum from vBulletin 4.2 3 93
How to use 2 ON statements in inner join 3 43
Do not understand error message 3 45
php time 12 23
This article focuses on how to remove password security from multiple PDF files by Adobe Acrobat program. Sometimes it is essential to access the stored data items and to print, edit as well as copy content from Portable Document Format files in abs…
The Adobe PDF proprietary file format is recognized as secure and formulated. But these PDF files are also prone to corruption and any external threat like virus attacks, improper storage can hit PDF file integrity.This type of damages can make cruc…
In this second video of the Xpdf series, we discuss and demonstrate the PDFimages utility, which, in a single command, is able to extract all the images from a PDF file and save each one in a separate image file (PBM, PPM, or JPG). Download and inst…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

737 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