Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Force a pdf file to download instead of opening.

Posted on 2004-07-31
11
Medium Priority
?
9,536 Views
Last Modified: 2013-11-19
Have a special case instance where I would like a visitor to be able to download a pdf form without the pdf file first opening in a new window.

What I have working:
straight html page (frames).
a link to name.pdf.
Using MS Frontpage

My only options for the pdf hyperlink were to have it open in the same page, specific frame, or new window.
And what seems to be a real puzzle is that I have security set on the pdf document to allow the filling in of form data, printing and saving, but nothing else.
If the user opens up the pdf in a new window and fills in any information on the form, it is saved to the pdf regardless of being on the web server???  the next user can open up the pdf and any information previously filled in, is right there still visible.

So, for my purposes with this pdf form, I need to force download instead of opening as a web page.

TIA,

kkohl
0
Comment
Question by:kkohl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 11684943
It's in how you send the headers. Check this page http://us4.php.net/manual/en/function.header.php and search for Content-Disposition for a PHP example.

The only other way to do this without a server-side scripting language is to zip up the pdf file.

Regards,
${Zyloch}
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 11685316
Download a file instead of opening it. IE only and assuming you have ASP capabilities:
<%@Language="JScript"%>
<%
var myFSO = new ActiveXObject("Scripting.FileSystemObject")
var objFile = myFSO.openTextFile(Server.MapPath("/path/somefile.pdf"));
var str = new String(objFile.readAll());
Response.ContentType = 'application/unknown';
Response.addHeader('content-disposition', 'attachment;filename=somefile.pdf');
Response.Write(str);
%>

Cd&
0
 
LVL 14

Expert Comment

by:Esopo
ID: 11685822
I've come to the conclution that the best method is to just ask the users to right click the link and choose "save as". Of course that's not what you are looking for so I will suggest you use CobolDinosaur's method, or it's equivalent in any other server side scripting language like PHP, .NET, even Perl.

I don't think this can be done on the client side, check this link to see what I mean (and for code on how to do it in every server side language):
http://www.experts-exchange.com/Web/Web_Languages/HTML/Q_21073694.html

Best regards,

Esopo.
0
Understanding Web Applications

Without even knowing it, most of us are using web applications on a daily basis. Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We often confuse these web applications tools for websites.  So, what is the difference?

 
LVL 36

Accepted Solution

by:
Zyloch earned 270 total points
ID: 11686183
It's not possible to do it completely client-side since downloading the pdf or displaying it is specific to each browser. You can do it server side using Cd's example or sending different page headers.

Regards,
${Zyloch}
0
 
LVL 9

Expert Comment

by:stefanaichholzer
ID: 11686795
Why don't you just put the .pdf file into a .zip file?, .zip files can't be opened by the browser, so each time the user wants to download one he'll have to save it to the drive, no matter what...

Hope that works for you...

;)
0
 
LVL 8

Expert Comment

by:Ahmed Merghani
ID: 11692362
I've come to the conclution that the best method is to just ask the users to right click the link and choose "save as". Of course that's not what you are looking for so I will suggest you use CobolDinosaur's method, or it's equivalent in any other server side scripting language like PHP, .NET, even Perl.

I don't think this can be done on the client side, check this link to see what I mean (and for code on how to do it in every server side language):
0
 
LVL 3

Author Comment

by:kkohl
ID: 11695301
It's all in the headers, as Zyloch pointed out.  I did find a little clearer answer when digging a little deeper on the web.

I changed the file extension from .pdf to .fdf, and added the headers to account for the downloading of my file.

<meta http-equiv="Content-Type" content="application/vnd.fdf; name="%s.FDF"">
<meta http-equiv="Content-Disposition" content="attachment; filename="%s.FDF"">

The user can still choose to open the file from the current location but it is not automatic and the default button that is focused is the save button.  Also, it was verified that if the user opens the file from the site location, any form changes are not saved to that web location regardless if the user tries to or not.

This method answered all requirements.
0
 
LVL 3

Author Comment

by:kkohl
ID: 11695375
(yes, this method was the fix for IE, there are other headers for the different browsers)
Very clunky way if you ask me, but it was done in straight HTML, no asp or xml, no java, no perl...  and the bottom line is, it works.

Thanks all!

0
 
LVL 1

Expert Comment

by:milkmon123
ID: 11942201
Hi There,

Here is what I would use...

<?php

header('Content-Type: application/save');
header('Content-Disposition: attachment; filename=<<<FILE NAME>>');
readfile(<<< ABSOLUTE PATH TO YOUR FILE >>>);

?>

Hope it helps

Matt Wilkes
0
 

Expert Comment

by:debjitk
ID: 11946381
Matt Wilkes is right you can force to download a file without displaying but adding specific headers.

Debjit Kar
0

Featured Post

Understanding Web Applications

Without even knowing it, most of us are using web applications on a daily basis. Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We often confuse these web applications tools for websites.  So, what is the difference?

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

609 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