如何使PDF文件在HTML链接下载?

我正在给我的网页上的PDF文件的链接下载,如下所示

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

问题是当用户点击这个链接时

  • 如果用户安装了Adobe Acrobat,则它会在Adobe Reader的同一浏览器窗口中打开该文件。
  • 如果未安装Adobe Acrobat,则会popup给用户以下载文件。

但是我希望它总是popup给用户下载,不pipe“Adobe acrobat”是否安装。

请告诉我如何做到这一点?

而不是链接到.PDF文件,而是做类似的事情

 <a href="pdf_server.php?file=pdffilename">Download my eBook</a> 

它输出一个自定义标题,打开PDF(二进制安全),并将数据打印到用户的浏览器,然后他们可以select保存PDF,尽pipe他们的浏览器设置。 pdf_server.php应该是这样的:

 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:显然,对“文件”variables进行一些理智检查,以防止人们窃取您的文件,如不接受文件扩展名,拒绝斜杠,将.pdf添加到值

这是一个常见的问题,但很less有人知道有一个简单的HTML 5解决scheme:

 <a href="./directory/yourfile.pdf" download="newfilename">Download the pdf</a> 

其中newfilename是用户保存文件的build议文件名。 或者,如果将其保留为空,则它将默认为服务器端的文件名,如下所示:

 <a href="./directory/yourfile.pdf" download>Download the pdf</a> 

兼容性:我在Firefox 21和Iron上testing过,两者都正常工作。 它可能不适用于HTML5不兼容或过时的浏览器。 我testing过的唯一没有强制下载的浏览器是IE …

检查兼容性: http : //caniuse.com/#feat=download

不要遍历每个文件行。 改用readfile ,其速度更快。 这是closures的PHP网站: 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; } 

确保清理你的variables,因为有人可以下载一些PHP文件…

读取和刷新文件不是使用PHP脚本,而是使用.htaccess重写头文件。 这将保持一个“好”的URL( myfile.pdf而不是download.php?myfile )。

 <FilesMatch "\.pdf$"> ForceType applicaton/octet-stream Header set Content-Disposition attachment </FilesMatch> 

我find了一种方法来处理普通的旧HTML和JavaScript / jQuery,它们会降级。 testingIE7-10,Safari,Chrome和FF:

HTML下载链接:

 <p>Thanks for downloading! If your download doesn't start shortly, <a id="downloadLink" href="...yourpdf.pdf" target="_blank" type="application/octet-stream" download="yourpdf.pdf">click here</a>.</p> 

jQuery(纯JavaScript代码将更加冗长),模拟一个小的延迟后点击链接:

 var delay = 3000; window.setTimeout(function(){$('#downloadLink')[0].click();},delay); 

为了使这个更健壮,你可以添加HTML5特征检测,如果不存在,那么使用window.open()来打开一个新的文件窗口。

这是关键:

 header("Content-Type: application/octet-stream"); 

在发送PDF文件的时候发送内容型应用程序/ x-pdf文件或者应用程序/ pdf。 Adobe Reader通常会为此MIMEtypes设置处理程序,以便在收到任何PDF MIMEtypes时,浏览器将文档传递到Adobe Reader。

在HTML5中有一个更简单的方法:添加Download属性。

它受到了大多数现代浏览器的支持。

 <a download href="file.pdf">Download PDF</a> 

我知道我很晚回答这个,但我发现一个黑客在JavaScript中做到这一点。

 function downloadFile(src){ var link=document.createElement('a'); document.body.appendChild(link); link.href= src; link.download = ''; link.click(); } 

这是一个常见的问题,但很less有人知道有一个简单的HTML 5解决scheme: <a href="./directory/yourfile.pdf" download="filename">Download the pdf</a> }

在Ruby on Rails应用程序中(尤其是像Prawn gem和Prawnto Rails插件),您可以比完整的脚本(如前面的PHP示例)简单一点。

在你的控制器中:

 def index respond_to do |format| format.html # Your HTML view format.pdf { render :layout => false } end end 

render:layout => false部分告诉浏览器打开“你想下载这个文件吗? 提示而不是试图呈现PDF。 然后,您将能够正常链接到该文件: http : //mysite.com/myawesomepdf.pdf

尝试这个:

<a href="pdf_server_with_path.php?file=pdffilename&path=http://myurl.com/mypath/">Download my eBook</a>

pdf_server_with_path.php中的代码是:

 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); 

这是一个不同的方法。 我宁愿不依靠浏览器支持,或者在应用层处理这个问题,以使用Web服务器逻辑。

如果您使用的是Apache,并且可以将.htaccess文件放在相关目录中,则可以使用下面的代码。 当然,你也可以把它放在httpd.conf中,如果你有权访问的话。

 <FilesMatch "\.(?i:pdf)$"> Header set Content-Disposition attachment </FilesMatch> 

FilesMatch指令只是一个正则expression式,所以可以根据需要细化设置,也可以添加其他扩展。

标题行与上面的PHP脚本中的第一行做相同的事情。 如果你还需要设置Content-Type行,你可以用同样的方式来完成,但是我没有find必要的。

我解决了我的使用PDF文件的整个url(而不是只是把文件名或位置的HREF):a href =“domain。com / pdf / filename.pdf”