show pdf content in user browser

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
maldar
Forum Commoner
Posts: 49
Joined: Mon Aug 18, 2003 4:39 pm

show pdf content in user browser

Post by maldar »

Hi all
I have a problem and am confused
I have many pdf files in my website and desire to show content of each pdf file when user click on its link (such as this http://www.mywebsite.com/show.php?file1.pdf

I use this script for opening pdf files and tell user browser to show it(open adobe reader in browser):


Code: Select all

<?php 
header( "Content-type: application/pdf" ); 
header("Content-disposition: inline;"); 

$filename = $_GET&#1111;'filename']; 
$fp = fopen ( $filename, "r" ); 
$output = fread( $fp, filesize( $filename ) ); 
fclose ( $fp ); 
echo $output; 
?>
but everywhen user click link provided insteed of opennig file in his browser ,browser suggests him to download file while adboe is installed

where is my wrong.
any tip may be usefull.please help
thanks in advnace
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

why even bother with this, the net effect is the same as just having their browser point directly to the pdf file, i dont see the logic in this, just wasting server processing power
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

dull1554 wrote:why even bother with this, the net effect is the same as just having their browser point directly to the pdf file, i dont see the logic in this, just wasting server processing power
Not very helpful... :roll:

If you have a look at the [php_man]pdf[/php_man] functions in the manual and scroll down to the user-notes, you'll discover this:

Code: Select all

$buf = PDF_get_buffer($p);
$len = strlen($buf);
header("Cache-Control: no-store");
header("Cache-Control: no-cache");
header("Cache-Control: must-revalidate");
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=file.pdf");
Which might be of help.
Last edited by patrikG on Mon Jun 07, 2004 9:12 am, edited 1 time in total.
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

pdf's will only open in a browser if it supports and is set up to use Adobe as a plugin for the browser, not all browsers support this especially older browsers and some non Microsoft ones.

What browser is the user using?
dave420
Forum Contributor
Posts: 106
Joined: Tue Feb 17, 2004 8:03 am

Post by dave420 »

patrikG - setting the filename through content-disposition can cause IE to spit out the "download" dialogue box. It's part of the security issues. As you're (possibly) hiding the extension of the file, it plays safe and asks you what to do.

Setting the content-type header is enough to let the browser know it's a PDF coming down the line. Any installed plugins will take that and render it for you.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

I assumed the guy who has problems with viewing the PDF is using IE and it sounds very much like a header() issue (alas, IE, alas, yet again :(). Can you reproduce the error on your machine?
dave420
Forum Contributor
Posts: 106
Joined: Tue Feb 17, 2004 8:03 am

Post by dave420 »

(why does everyone hate IE? :-P)
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

well i hyave totally rebelled away from Microsoft....the only microsoft product i use is a wireless optical mouse and i dont know that i even trust that.....
dave420
Forum Contributor
Posts: 106
Joined: Tue Feb 17, 2004 8:03 am

Post by dave420 »

too bad, dude :-P
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

not really i've started to enjoy linux and freeBSD, and i dont really mind Debian that much either
dave420
Forum Contributor
Posts: 106
Joined: Tue Feb 17, 2004 8:03 am

Post by dave420 »

Your loss! :-P
Post Reply