A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearHow to make PDF file downloadable in HTML link? - page 1
User InfoPosts
How to make PDF file downloadable in HTML link?#1
I am giving link of a pdf file on my web page for download, like below

<a href="myfile.pdf">Download Brochure</a>


The problem is when user clicks on this link then


If the user have installed Adobe Acrobat, then it opens the file in the same browser window in Adobe Reader.
If the Adobe Acrobat is not installed then it pop-up to the user for Downloading the file.


But I want it always pop-up to the user for download, irrespective of "Adobe acrobat" is installed or not.

Please tell me how i can do this?

posted date: 2008-12-12 23:09:00


Re: How to make PDF file downloadable in HTML link?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-12 23:09:01


Re: How to make PDF file downloadable in HTML link?#3
Instead of linking to the .PDF file, instead do something like <a href="pdf_server.php?file=pdffilename">Download my eBook</a>which outputs a custom header, opens the PDF (binary safe) and prints the data to the user's browser, then they can choose to save the PDF despite their browser settings. The pdf_server.php should look like this:header("Content-Type: application/octet-stream");$file = $_GET["file"] .".pdf";header("Content-Disposition: attachment; filename=" . urlencode($file)); header("Content-Type: application/octet-stream");header("Content-Type: application/download");header("Content-Description: File Transfer"); header("Content-Length: " . filesize($file));flush(); // this doesn't really matter.$fp = fopen($file, "r");while (!feof($fp)){ echo fread($fp, 65536); flush(); // this is essential for large downloads} fclose($fp); PS: and obviously run some sanity checks on the "file" variable to prevent people from stealing your files such as don't accept file extensions, add .pdf to the value

posted date: 2008-12-12 23:18:00


Re: How to make PDF file downloadable in HTML link?#4
Can you please provide me the code for that, how to do so in PHP.

posted date: 2008-12-12 23:19:00


Re: How to make PDF file downloadable in HTML link?#5
This is the key:header("Content-Type: application/octet-stream");Content-type application/x-pdf-document or application/pdf is sent while sending PDF file. Adobe Reader usually sets the handler for this MIME type so browser will pass the document to Adobe Reader when any of PDF MIME types is received.

posted date: 2008-12-12 23:27:00


Re: How to make PDF file downloadable in HTML link?#6
In a Ruby on Rails application (especially with something like the Prawn gem and the Prawnto Rails plugin), you can accomplish this a little more simply than a full on script (like the previous PHP example).In your controller:def index respond_to do |format| format.html # Your HTML view format.pdf { render :layout => false } endendThe render :layout => false part tells the browser to open up the "Would you like to download this file?" prompt instead of attempting to render the PDF. Then you would be able to link to the file normally: http://mysite.com/myawesomepdf.pdf

posted date: 2008-12-12 23:49:00


Re: How to make PDF file downloadable in HTML link?#7
Where to wrote this code? which controller, i am new to PHP please explain.

posted date: 2008-12-13 00:01:00


Re: How to make PDF file downloadable in HTML link?#8
I am facing another problem with this, that my file is located at /products/brochure/myfile.pdf I am giving $file variable as $file_path = $_SERVER['DOCUMENT_ROOT'].'/products/brochure/' . $file; but its downloading the file as "%2Fvar%2Fwww%2Fweb15%2Fweb%2Fproducts%2Fbrochure%2myfile.pdf"

posted date: 2008-12-13 02:11:00


Re: How to make PDF file downloadable in HTML link?#9
Don't loop through every file line.Use readfile instead, its faster. This is off the php site:http://php.net/manual/en/function.readfile.php$file = $_GET["file"];if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header("Content-Type: application/force-download"); header('Content-Disposition: attachment; filename=' . urlencode(basename($file))); // header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit;}Make sure to sanitize your get variable as someone could download some php files...

posted date: 2011-11-14 05:35:00


Re: How to make PDF file downloadable in HTML link?#10
Instead of using a PHP script, to read and flush the file, it's more neat to rewrite the header using .htaccess. This will keep a "nice" URL (myfile.pdf instead of download.php?myfile).<FilesMatch "\.pdf$">ForceType applicaton/octet-streamHeader set Content-Disposition attachment</FilesMatch>

posted date: 2012-02-09 02:24:00


Re: How to make PDF file downloadable in HTML link?#11
Try this:<a href="pdf_server_with_path.php?file=pdffilename&path=http://myurl.com/mypath/">Download my eBook</a>The code inside pdf_server_with_path.php is:header("Content-Type: application/octet-stream");$file = $_GET["file"] .".pdf";$path = $_GET["path"];$fullfile = $path.$file;header("Content-Disposition: attachment; filename=" . Urlencode($file)); header("Content-Type: application/force-download");header("Content-Type: application/octet-stream");header("Content-Type: application/download");header("Content-Description: File Transfer"); header("Content-Length: " . Filesize($fullfile));flush(); // this doesn't really matter.$fp = fopen($fullfile, "r");while (!feof($fp)){ echo fread($fp, 65536); flush(); // this is essential for large downloads} fclose($fp);

posted date: 2012-06-20 18:15:00


Re: How to make PDF file downloadable in HTML link?#12
The readfile function is indeed faster. I personally recommend using this answer instead of the accepted one

posted date: 2013-02-04 13:08:00


select page: « 1 2...»
Copyright ©2008-2017 www.momige.com, all rights reserved.