Opening a PDF on a website
Moderator: General Moderators
Opening a PDF on a website
I am making a web site that will give the user the option to open one of several pdf files and print them. How do I make a link open a pdf file with adobe reader instead of trying to load it in the browser? Right now all it does is lead to a page that says http://www.mydomain.com/thepdf.pdf that is blank.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Code: Select all
header("Content-disposition: attachment; filename='foo.pdf'");Code: Select all
<?php
header("Content-disposition: attachment; filename='cqapp.pdf'");
?>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
you need to set other headers such as content-length, content-type, encoding... and actually send the file 
readfile()
Look in header() documentation and here about their help on downloads via php.
readfile()
Look in header() documentation and here about their help on downloads via php.
OK... nevermind, it only works on my local server. Here is the new php file. What am I doing wrong now??
Code: Select all
<?php
$filename = $_GET['file'];
$filename = realpath($filename);
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: attachment; filename=\"$filename\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".@filesize($filename));
set_time_limit(0);
@readfile("$filename") or die("File not found.");
?>So I removed this line:
still doesn't work...
Code: Select all
$filename = realpath($filename);Copied this DIRECTLY from php.net and it doesn't work... the pdf file is getting corrupted somehow. I don't understand!
Code: Select all
$filename = 'cqapp.pdf';
$filename = realpath($filename);
$file_extension = strtolower(substr(strrchr($filename,"."),1));
switch ($file_extension) {
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpe": case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
if (!file_exists($filename)) {
die("NO FILE HERE");
}
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: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".@filesize($filename));
set_time_limit(0);
@readfile("$filename") or die("File not found.");