SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Aug 2005
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sending PDF strings to IE produce blank page

    I am sending PDF strings from a MySql database. It works in FireFox but not in IE. The code I am using follows.

    <?php
    // All rights reserved
    // Check for all php errors
    header("Pragma: public");
    header('Cache-control: private, must-revalidate');
    header("Content-type: application/pdf"); //Send first thing
    while (@ob_end_clean()); //This should keep the sesionpid from showing up on the url
    ob_start();
    session_start();
    session_regenerate_id();
    include("./includes/database.php");
    if (empty($_SESSION['authorized']) || (!($_SESSION['authorized']===TRUE))) {
    $redirect="index.php";
    session_write_close();
    return header("location:$redirect");
    exit;
    }
    $artnum= (empty($_GET["articlenum"]))?1:$_GET["articlenum"];
    //connect and pull rows
    $db = new database("mysql","...","...","...","...");
    $sql = "select * from ".$_GET["tablename"]." where id=$artnum";
    $db->query($sql);
    $my_row=$db->get_rows();
    $thiscontent=$my_row["file"];
    header("Content-Length: " . strlen($thiscontent));
    header("Content-Disposition: inline; filename=article.pdf");
    print $thiscontent;
    ?>


    I have tried moving headers around and in different positions. I also have added several headers from when I first started. It still works in FireFox but not in IE 7 and when I do a View Page I get the following in IE.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML><HEAD>
    <META http-equiv=Content-Type content="text/html; charset=windows-1252"></HEAD>
    <BODY></BODY></HTML>

    Any suggestions,
    Thanks,
    Michael Magee

  2. #2
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IE is notorious for this. The headers have to be just so. The docs on fpdf.org have some info on this.

    IE actually requests the file multiple times. the first time it stores it in a cache, and subsequent times are used to determine MIME type and other file attributes (screwball i know).

    Unfortunately, if the proper HTTP instructions for caching are not sent, the contents are not cached, and what gets displayed, is nothing.

    I have some functions that have proved to be effective for me in this. They are part of a utility class that is a grouping of general purpose proceedures. one of them makes a static call to the other, but as long as you understand the code you can modify it to your liking.

    PHP Code:
    class utility
    {
        function 
    getMimeType($file)
        {
            
    $nameArr explode('.',$file);
            
    $ext $nameArr[(count($nameArr) - 1)];
            switch(
    strtolower($ext))
            {
                case 
    'pdf':
                    
    $type 'application/pdf';
                    break;
                case 
    'jpg':
                    
    $type 'image/jpeg';
                    break;
                case 
    'jpeg':
                    
    $type 'image/jpeg';
                    break;
                case 
    'gif':
                    
    $type 'image/gif';
                    break;
                case 
    'mp4':
                    
    $type 'application/mpeg4-generic';
                    break;
                case 
    'mp3':
                    
    $type 'audio/mpeg';
                    break;
                case 
    'tiff':
                    
    $type 'image/tiff';
                    break;
                case 
    'png':
                    
    $type 'image/png';
                    break;
                case 
    'csv':
                    
    $type 'text/csv';
                    break;
                case 
    'txt':
                    
    $type 'text/plain';
                    break;
                case 
    'xml':
                    
    $type 'text/xml';
                    break;
                case 
    'html':
                    
    $type 'text/html';
                    break;
                case 
    'doc':
                    
    $type 'application/msword';
                    break;
                case 
    'zip':
                    
    $type 'application/zip';
                    break;
                case 
    'jpeg2000':
                    
    $type 'image/jp2';
                    break;
                case 
    'rtf':
                    
    $type 'text/rtf';
                    break;
                case 
    'mov':
                    
    $type 'video/quicktime';
                    break;
                case 
    'xls':
                    
    $type 'application/excel';
                    break;
                case 
    'ppt':
                    
    $type 'application/powerpoint';
                    break;
                case 
    'wav':
                    
    $type 'audio/x-wav';
                    break;
                case 
    'aiff':
                    
    $type 'audio/aiff';
                    break;
                case 
    'mid':
                    
    $type 'audio/x-midi';
                    break;
                case 
    'bmp':
                    
    $type 'image/bmp';
                    break;
                case 
    'mpeg':
                    
    $type 'video/mpeg';
                    break;
                case 
    'mpg':
                    
    $type 'video/mpeg';
                    break;
                case 
    'avi':
                    
    $type 'x-msvideo';
                    break;
                case 
    'pub':
                    
    $type 'application/x-mspublisher';
                    break;
                case 
    'sit':
                    
    $type 'application/x-stuffit';
                    break;
                case 
    'tar':
                    
    $type 'application/x-tar';
                    break;
                case 
    'ra':
                    
    $type 'audio/x-pn-realaudio';
                    break;
                case 
    'ram':
                    
    $type 'audio/x-pn-realaudio';
                    break;
                case 
    'tsv':
                    
    $type 'text/tab-separated-values';
                    break;
                case 
    'asf':
                    
    $type 'video/x-ms-asf';
                    break;
                case 
    'wma':
                    
    $type 'audio/x-ms-wma';
                    break;
                case 
    'wmv':
                    
    $type 'video/x-ms-wmv';
                    break;
                case 
    'flv':
                    
    $type 'video/x-flv';
                    break;
                case 
    'swf':
                    
    $type 'application/x-shockwave-flash';
                    break;
                case 
    'fla':
                    
    $type 'application/octet-stream';
                    break;
                default:
                    
    $type 'application/octet-stream';
                    break;
            }
            return 
    $type;
        }
        
       
    // arguments
       // filename, filesize, I for inline (plugin) D for force download
        
    function downloadHeaders($file,$filelen,$mode='I')
        {
            
    // $mode: D = force download, I = attempt inline
            
    $type utility::getMimeType($file);
            
    $mode strtoupper($mode);
            if(isset(
    $_SESSION))
            {
                
    session_cache_limiter('no-cache');
            }
            
    header('pragma: cache');
            
    header('pragma: public');
            
    header('Cache-Control: max-age=0');
            
    header("Cache-Control: private",false); // required for certain browsers 
            
    header('Content-Type: '.$type);
            
    header("Expires: 0");
            if(
    $mode == 'D')
            {
                
    header('Content-Disposition: attachment; filename='.$file);
            }
            else
            {
                
    header('Content-Disposition: inline; filename='.$file);
            }
            
    header("Content-Transfer-Encoding: binary");
            
    header('Content-Length: '.$filelen);
        }
    }

    $filename 'report.pdf';
    $filelen strlen($pdfcontentsfromdb);
    // lets download because IE pdf plugin sucks
    $mode 'D';
    utility::downloadHeaders($filename,$filelen,$mode);
    echo(
    $pdfcontentsfromdb);
    exit(); 

  3. #3
    SitePoint Member
    Join Date
    Aug 2005
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply. The solution works on my machine but not on others. So we have decided to use the text/html and use inline instead of attachment. That works on all machines. We have to go to the lowest common denominator for this application. Maybe Adobe will get their plug-in right some day.
    Thanks for the help.
    Michael Magee

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,832
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The problem isn't the Adobe plugin. The problem is that Internet Explorer doesn't work properly. This is just one of the thousands of things where IE doesn't work correctly.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's right. You can get IE to do this with other plugin related docs, that are generated programatically. IE 7 doesn't have quite the problem that 6 does in this regard however.

    i am surprised it didn't work better for you. The "D" force download option in the function is a good alternative if you can't get the inline view to work.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •