Browser Forced Download of Adobe Acrobat .PDF Files.

ReDuX 0 Tallied Votes 146 Views Share

Do you work with datasheets regularly ? Always getting .pdf files open in your browser and having to wait EONs for them to download/display ? Well here is a resolve for that most frustrating of problems with Adobe's "Handy file intercept" feature. Use this script to FORCE the browser to download the file rather than display it inline.

// html file with links to either display or download the .pdf file.
<a href="pdf.php?pdfile=File-name-here">Download File</a><br>
<a href="data/File-name-here.pdf">View File</a><br>

/* The following code assumes that the files are located in a folder
called "data" and all files are .pdf. I havent tested it with files that
have spaces in the name, so i dont know if it works. But it does work
fine with files using _ and - in the names. */

// Get the file size of the selected file (for convenience so you know how
// long you have to wait...
$len = filesize('data/'.$pdfile.'.pdf');

// Declare File Type...
header('Content-type: application/pdf');

// Name the file. Cound just call it download.pdf or other using this.
header('Content-Disposition: attachment; filename="'.$pdfile.'.pdf"');

// declare Files Size here (for the sake pf peoples sanity, please add this
header('Content-Length: '.$len);

// Path to original file to be read and sent to browser.
readfile('data/'.$pdfile.'.pdf');


/* You could of course modify this to work with other filetypes, or just remove the filetype totally, its up to you.
Please make sure you leave in the file size bit so people dont go insane
waiting for a file of undetermined time, tearing their hair out...
*/
Rohtie 0 Newbie Poster

Can i use this to force other stuff too?

Remulon 0 Newbie Poster

rather than download it, how do I display it in the browser.. which I know is what you are trying to avoid.. but it is what I am trying to achieve.. :P

w3m 0 Newbie Poster

for security reasons, don't forget to check if the filename is ok. I mean the $_GET in the pdf.php, I never use directly the variable, that user can possibly manipulate, hackers may try to get another important files that you might not want to deliver too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.