?
Solved

How to put a PDF file in a web page for download

Posted on 2010-01-07
35
Medium Priority
?
537 Views
Last Modified: 2012-05-08
Hey Experts,

i have some PDF files and i need to put the links to a web page for download option. No one should able to see the path where in server the PDF files are exists. Once a visitor clicks a link to download a file it should give option to save the file in local system  not to pop up or open in browser.

any help would be appreciated.


thanks in advance.
SMIT
0
Comment
Question by:Odessa_Technologies
  • 15
  • 12
  • 3
  • +3
35 Comments
 
LVL 3

Accepted Solution

by:
seandolan earned 1500 total points
ID: 26199892
If you have the ability to use PHP here is some code that may help:
$filename = 'myfilename.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.");

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26199897
You need a server process that fetches the PDF from the server file system and sends it with header

Php version:

header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename");
echo(file_get_contents("path_to_pdf-files/$filename"));

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26199908
Missed by a few seconds there...
However mine is simpler.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:Kamaraj Subramanian
ID: 26199917
0
 
LVL 3

Expert Comment

by:seandolan
ID: 26199958
Mine allows for compliance with different file types, but for less code mplungjan's solutions is better. If you use mplungjan change it to this:

header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=$filename");
echo(file_get_contents("path_to_pdf-files/$filename"));

So that the file can still be handles correctly.
0
 
LVL 2

Author Comment

by:Odessa_Technologies
ID: 26200029
the website is not done in PHP. it contains simple HTML pages. So provide me some solutions that sync with HTML code or JavaScript.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26200035
No you do NOT want to change application/octet-stream to application/pdf if you want to force the download dialog to appear
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26200056
Not possible to hide the location of the pdf and only an easily circumventable piece of javascript to force download:

<a href="file.pdf" onClick="alert('Please use right-click and download'); return false">Download pdf</a>
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 26200103
The browser can only do what the server tells it.

Neither HTML nor JavaScript are server-side tools.

To get the browser to force a Save, you have to have the web server send specific headers.

These headers are as suggested by mplungjan. Do NOT use application/pdf as this tells the browser that the content is a PDF file and you will probably get the in page display of the PDF.

To get the headers sent, you need some sort of server side tool.

PHP is extremely common.

You do not have to have the rest of the site in PHP.

As a test, create a file called info.php, containing the following single line.

<?php phpinfo();

Place the file in the same folder as your other files and then load it from your browser ...

http://www.yoursite.com/info.php

Do you see the same line? Or do you see the PHP configuration page?
0
 
LVL 2

Author Comment

by:Odessa_Technologies
ID: 26200127
Sorry to say Chrome does not give the right click download option. So i can't use this. and pop-up alert message will not be acceptable. What i want one link. clicking on the link it should pop-up save window to save the file in local system. that's it.
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 26200150
You cannot force a client to download+save (rather than displaying) a file without cooperation from the server.

0
 
LVL 2

Author Comment

by:Odessa_Technologies
ID: 26200161
i understand. but i have certain limitation to work on multiple languages. So i have to user HTML or JavaScript so shot out this issue.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26200164
Given your restrictions: Answer = Not possible
0
 
LVL 2

Author Comment

by:Odessa_Technologies
ID: 26200221
your attitude is not appreciated. there must be some way out...
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26200261
What version of Chrome do you have?

Mine works fine with this

<a href="http://v3.espacenet.com:80/espacenetDocument.pdf?flavour=maximizedPlainPage&locale=en_V3&pageNumber=1&FT=D&date=20070614&CC=WO&NR=2007067899A2&KC=A2" onclick="alert('Please use right-click'); return false">PDF</a>
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26200317
What attitude?
I tell you the facts - "Don't blame the mirror" ;)

mirror.jpg
0
 
LVL 2

Author Comment

by:Odessa_Technologies
ID: 26200357
do not take it personalty. Here we are to cross the technical limitation and get the things done.

FYI - i'm using Chrome 3.0.195.38.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26200438
Me too. And the return false on the click and the save link as on left click works on XP at least
0
 
LVL 2

Author Comment

by:Odessa_Technologies
ID: 26200592
My OS is windows Server 2008.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26200655
But that is not something normal users will run on their desktop or laptop is it?

Are you saying that google chrome will not allow you to left-click and save-as on the code I gave you on your computer?
0
 
LVL 2

Author Comment

by:Odessa_Technologies
ID: 26200847
check out the image that i have taken from the browser screen shot
Chrome-right-click-options.jpg
0
 
LVL 2

Author Comment

by:Odessa_Technologies
ID: 26200868
do not consider the save link as option. after clicking that link you get a save as option i need that save as window in the first place on simple left click.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26200873
Yes - save link as... will save the pdf on your harddisk
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26200894
I understand what you want and what you need. It is simply not possible to save a pdf on a simple left click unless you make the server send the pdf as application/octet-stream OR remove all knowledge of PDF from your computer
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26200983
Ok. thinking out of the box:

1. a pdf can print itself, so perhaps it can save itself too. I can have a look in the pdf javascript reference - however it would need to be opened to execute
2. IE can call document.execCommand("saveAs"...) - I cannot see how it would save a PDF though
3. you can load a signed applet or activeX that will save - however it needs to be installed by the user
4. you can create an addon for FF, plugin for IE and whatever Chrome supports. Still has to be installed
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26201072
Hmm might be possible

http://www.adobe.com/devnet/acrobat/pdfs/AcroJS.pdf
Create and save a document to My Documents on a Windows platform.
var myDoc = app.newDoc();
var myPath = app.getPath("user", "documents") + "/myDoc.pdf"
myDoc.saveAs(myPath);
myDoc.closeDoc();

0
 
LVL 2

Author Comment

by:Odessa_Technologies
ID: 26201124
activeX won't be a goos option because it's IE specific.
 i would agree with you in point 1 and 2.
0
 
LVL 2

Author Comment

by:Odessa_Technologies
ID: 26201143
how about creating  a JavaScript object?? will it be possible??

but i doubt it will open a new window too.
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 26201365
The saving is only 1 part of the problem.

The other part was that you wanted to hide the location of the PDF.

That requires server side coding to process the request and provide the actual file.

How do you intend to code that?

Again, without some intervention on behalf of the server, you are NOT going to be able to deal with this.
0
 
LVL 3

Expert Comment

by:seandolan
ID: 26206385
The answer is no, and this question about hiding the url is scattered around the net. You can't unless you run some for of server side process. Even if you were to use some sort of obfuscation a user will eventually be able to get the file unless it's done server side. Please don't get annoyed, I know hearing that something isn't possible is sometimes frustrating to hear. Maybe you should try giving us some details about why you want to do it, then we can make some further recommendations for alternatives. Otherwise you will spend a lot of time chasing this and it will make you more annoyed about it in the end.
0
 
LVL 2

Author Comment

by:Odessa_Technologies
ID: 26208967
we have a press release and Articles page. I need to make all the press releases and articles available for PDF download. the press release and articles page have two list page with heading. Below every heading i need too option one is Read more another is Download PDF. now the problem is that entire website contains simple HTML pages. Now tell me what to do??

I understand i need a server side code to fire the download file form server.

please do check this link this may give you some idea what exactly i'm looking for

http://www.microsoft.com/everybodysbusiness/en/us/projects/consolidate-virtualize.aspx?banner=3&CR_CC=100193041&WT.mc_id=Media
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26209149
If you mean the link "Download whitepaper" you get about 3 redirects and end up with


HTTP/1.x 200 OK
Content-Type: application/octet-stream
Last-Modified: Thu, 27 Aug 2009 03:50:24 GMT
Accept-Ranges: bytes
Etag: "49c32682c926ca1:0"
Server: Microsoft-IIS/7.0
Content-Disposition: attachment
Date: Fri, 08 Jan 2010 12:54:42 GMT
Content-Length: 1494020


0
 
LVL 29

Expert Comment

by:rdivilbiss
ID: 26262562
mplungjan>Given your restrictions: Answer = Not possible

TRUE

Odessa_Technologies>your attitude is not appreciated. there must be some way out...

I'm surprised anyone is still helping you.  You need to stream the pdf using server side code as mentioned several times, and it isn't hard to do.
0
 
LVL 2

Author Closing Comment

by:Odessa_Technologies
ID: 31673998
good solution
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 27280997
So no split. Hmm. thanks
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question