SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Prevent PDFs from opening in browser window

    Is there code to prevent PDF's or any other file from opening in the browser window?

    How can I code it so it makes the user download it into their hard drive?

  2. #2
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Not with HTML, but you can do it by setting the HTTP header using a scripting language:

    Code php:
    $filename = "document.pdf";
    $path = "files/pdf/";
     
    if(file_exists($path.$filename)) {
     header("Content-Type: application/pdf");
     header("Content-Disposition: attachment; filename='".$filename."'");
     readfile($path.$filename);
    }
    else {
     printf("Could not read file %s located at %s", $filename, $path);
    }

    Be careful with which files you allow in this function, though, as this function will download any file without parsing it, including PHP files with your database password, etc.
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  3. #3
    SitePoint Wizard bronze trophy Black Max's Avatar
    Join Date
    Apr 2007
    Posts
    4,029
    Mentioned
    12 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MayanArch View Post
    Is there code to prevent PDF's or any other file from opening in the browser window? How can I code it so it makes the user download it into their hard drive?
    I wouldn't appreciate any site forcing me to do this.

  4. #4
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,072
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Black Max View Post
    I wouldn't appreciate any site forcing me to do this.
    Neither would I. I would expect to open the pdf in the browser then if whatever the pdf is about is of interest save it to a local drive from there. I would not want to have to go through two steps to open a pdf when i could view it in one step.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,868
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    It would only make sense to force the PDF for download when the link to the PDF is on a page where the exact same information is available on the web page and the only reason for the PDF is to allow the person to save a copy for later reference. Then forcing the download saves the step of having the PDF open in the browser before you hit the save button. You'd need to make it clear that the PDF contains the same info as the web page the person just read though so that there is no reason why they need to open the PDF now.
    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="^$">

  6. #6
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    I guess it's a matter of taste. I for one prefer to be prompted to download the document, as I may not need it immediately. Opening the document in the browser will slow down my machine, especially if I open a lot of documents at the same time. The download prompt will also give the option to open the document, if the user prefers this.
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!


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
  •