GeeCON Prague 2014*
The moose likes Servlets and the fly likes how to create a link to download pdf file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "how to create a link to download pdf file?" Watch "how to create a link to download pdf file?" New topic
Author

how to create a link to download pdf file?

Jim Wang
Ranch Hand

Joined: Oct 04, 2001
Posts: 41
Hi,
I use servlet create some link on my page to let user download PDF document. I use "<A href=http://192.168.../../../myPDF.pdf>" create link for download. But the browser wouldn't open a download dialog window like ".zip", it will open PDF file directly in my browser. How can I create a link let user to download PDF file?
Thanks.
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
Just instruct the user of your page to RIGHT CLICK on the link and choose "Save Target As..." (IE).
In Netscape it's "Save Link As..."

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Jim Wang
Ranch Hand

Joined: Oct 04, 2001
Posts: 41
Thanks Tony. Is that possible to let user just click one link on my page to download PDF file to local machine?
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Jim,

The browser decides what to do with links. If the browser is configured to recognize *.pdf files, it will perform the default operation (in some browsers, this is user specified). In most cases that means "launch the program that uses this type of file".

You may want to investigate the use of funky javascript tricks.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Mike Curwen:
The browser decides what to do with links.
Well, yes, but that doesn't mean you have no say in it. The behaviour is governed by the Mime headers that the web server sends back with the file.
First,the Content-Type header. If the content type is one the browser doesn't have a handler for, it displays the "Save As" dialog. If desperate, you can always configure the web server, servlet or JSP to use some absolutely wild Mime type
Second, the Content-Disposition header. Unfortunately, this is where I'm getting out of my depth (alright, I'm just too lazy to start digging through books and specs). I think, but am not sure, that if you specify "attachment; filename=filename" then the browser will display the "Save As" dialog even if it has a handler or plug-in for the type. Take my words here with a grain of salt though.
- Peter
Jim Wang
Ranch Hand

Joined: Oct 04, 2001
Posts: 41
Thanks Mike and Peter. Right now I understand why my browser always open those PDF files. Then here comes another question: How to make browser can not reconize PDF file and create a save as log?
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I would go with Peter's suggestion. There is really no way for you to code an HTML page that makes a browser ignorant of it's own settings. Users would probably not like you very much if you even *could* do this sort of thing.

But when Peter posted his reply, I knew that was the right track, because I do recall there are websites where (without right-clicking), the two links are: "read the pdf" and "download the pdf".

So there must be a way, and Peter is starting you on the right direction I think.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16103
    
  21

Now THIS is interesting!
About the content-disposition header - depending on your browser brand and version (also, whether you have a buggy release of IE), the content-disposition can be used to indicate whether a file should be displayed, downloaded, or otherwise disposed of. So if you want a PDF to be saved instead of displayed, set the appropriate Content-Disposition and hope the user's browser wants to co-operate.
I should point out that an httpd download is NOT a named file! The way the user's browser determines what to do with content by default (no Content-Disposition) is in no way determined by the C.D. "file name", since that's just a hint to the browser for the name displayed in the "Save as" dialog and can be overridden - for example, they can change "10K.PDF" to "CRUD.TXT" and that's the name the document will store under on the user's machine (when Notepad doesn't display it properly, that's THEIR fault!).
In the case of an embedded link on an HTML page to a graphic - or a PDF, the web server looks at the filename extension on the link's URL to get its MIME type, for generated content (dynamic PDF), YOU set the MIME type. The MIME type is what counts, not the filename. Only AFTER the file resides on the client's machine and is being accessed by the client OS does the filename extension become important (most OS's have nowhere to store the file's original MIME type anyway!).
That probably wasn't as clear or as succint as I'd hoped - but maybe it will help...


Customer surveys are for companies who didn't pay proper attention to begin with.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16103
    
  21

Anyway, the above is all theory. For the original question, the answer would be to replace the HREF to the PDF with the URL of a servlet that copies the actual PDF out with its own special headers (Content-disposition and maybe a binary-download MIME).
Jim Wang
Ranch Hand

Joined: Oct 04, 2001
Posts: 41
Hi, Tim:
Thanks for your help.I just want to make it clear. Do you mean I need a special servlet for downloading PDF file, and this servlet will read PDF first then pass it with MIME header?
Manjunath Reddy
Ranch Hand

Joined: Jul 26, 2001
Posts: 60
This link to an article written sometime back forces the browser not to interpret the attachment but download it.
http://www.planet-source-code.com/xq/ASP/txtCodeId.2183/lngWId.2/qx/vb/scripts/ShowCode.htm
Hope this helps.
cheers!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to create a link to download pdf file?