Auto Download PDF

Discussion in 'PHP' started by jestep, Jul 20, 2006.

  1. #1
    I have a PDF file that I am trying to link to and I need to have visitors automatically download it instead of it opening inside the browser. Is there a way to do this without zipping it.

    I was thinking that there may be some php headers or something that will do it. I can pre-make it into a pdf, or I can generate one in php on the fly, if it matters.

    Any suggestions would be appreciated.
     
    jestep, Jul 20, 2006 IP
  2. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here's a copy and paste of a section from http://www.faqts.com/knowledge_base/view.phtml/aid/4906 :

    Using the Content-Disposition header correctly should force IE and
    other
    browsers to treat the file as an attachment/download. I do it all the
    time. Make sure you specify attachment as the type. ex:

    Content-Disposition: attachment; filename="file.pdf"

    Obviously this is easiest if you are dynamically outputing the contents
    of the PDF from a server script script (like Perl, PHP, ASP, etc).


    So yeah, have a sniff around the content disposition header...
     
    TwistMyArm, Jul 20, 2006 IP
    jestep likes this.
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    That's exactly what I'm looking for. Thanks a bunch.
     
    jestep, Jul 21, 2006 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    Ok, I found a way to do it within php. Works well in Mozilla and IE.

    
       header("HTTP/1.1 200 OK");
       header("Content-Length: $file-size");
       header("Content-Type: application/force-download");
       header("Content-Disposition: attachment; filename=$file-name");
       header("Content-Transfer-Encoding: binary");
    
    PHP:
     
    jestep, Jul 21, 2006 IP