4

I'm trying to display a PDF in the browser if possible--and I know I can do this in Chrome, which is what I'm testing in. The trouble is, every time I try, it prompts a download instead.

I'm using PHP sessions, so I know there are some extraneous headers being sent, so I called header_remove() to reset everything.

I call this function to show the PDF:

<?php
// For demonstrative purposes
session_start();

if (!isset($_SESSION['auth'])) {
    header('Location: login.php');
    die;
}

/*
 * void viewPDF (Report $report)
 * Outputs the PDF of the report
 */
function viewPDF ($report) {
        // Tell the browser we are going to serve a PDF file.
    $file = dirname(__FILE__).'/../reports/'.$report->id.'.pdf';
        // The location of the PDF
    if (!file_exists($file)) {
        die ('The PDF does not exist.');
            // Somehow the file does not exist.
    }

    header_remove();
        // I'm using PHP sessions, so remove the headers
        // automatically set that might break something.
    header('Content-Disposition: inline;filename='.$report->id.'.pdf');
    header('Content-Transfer-Encoding: binary');
    header('Content-Type: application/pdf');
    header('Content-Length: '.filesize($file));
    readfile($file);
        // Serve the report PDF file from the reports
        // repository.
    die;
        // Any whitespace could corrupt the PDF, so be extra
        // sure nothing else gets printed.
}

// For demonstrative purposes:
$report = new StdClass;
$report->id = 1;
viewPDF($report);
?>

These are the headers being sent:

Date: Tue, 08 Oct 2013 18:41:32 GMT
Server: Apache/2.2.22 (Win32) PHP/5.4.15
Content-Type: application/pdf
Content-Transfer-Encoding: binary
Content-Disposition: inline;filename=1.pdf
Connection: Keep-Alive
Keep-Alive: timeout=5, max=100
Content-Length: 73464

It's still prompting a download though. Once it downloads, I can open it in Adobe Reader just fine.

Am I missing something?

Thanks.

M Miller
  • 5,364
  • 9
  • 43
  • 65
  • Not everyone likes having PDFs viewed in their browser (me, for one). I'd rather have it downloaded and viewed/opened separately. You cannot force a browser to view the file inline if it's been configured to not do that. – Marc B Oct 08 '13 at 18:48
  • try to add this before `readfile`: `header('Accept-Ranges: bytes');` – Mehdi Karamosly Oct 08 '13 at 18:49
  • @MarcB: The user has both options, but the client wants it to be viewable from the browser. I can access PDFs directly in my Chrome browser. There must be a header issue, because if I can view any PDF using Chrome, then I must be able to simulate viewing any other PDF... do I need to change the REQUEST_URI to the PDF file extension, maybe? That'd be a pain.... – M Miller Oct 08 '13 at 18:52
  • @MehdiKaramosly: I did try that, it didn't make any difference though. – M Miller Oct 08 '13 at 21:30
  • http://stackoverflow.com/questions/4679756/show-a-pdf-files-in-users-browser-via-php-perl No idea what could fix it then, Marc B is probably right. – Mehdi Karamosly Oct 08 '13 at 21:47

1 Answers1

6

This code worked for me :

    header('Content-Description: File Transfer');
    header('Content-Type: application/pdf');
    header('Content-Disposition: inline; filename="' . basename($file).'"');
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
Highly Irregular
  • 38,000
  • 12
  • 52
  • 70
Mehdi Karamosly
  • 5,388
  • 2
  • 32
  • 50
  • Hmm, still can't get it to work. I've even tried removing the session, clearing the headers, and using your code exactly as is.... – M Miller Oct 08 '13 at 23:14
  • What browser you are using ? I tried Chrome and IE and it works like a charm :) – Mehdi Karamosly Oct 08 '13 at 23:43
  • 1
    I honestly have no idea how this happened, but both my Chrome PDF viewer plugin and the Adobe Reader plugin were both disabled on my Chrome. Everything IS working! Thanks much – M Miller Oct 09 '13 at 00:01