如何使PDF文件在HTML链接中下载? [英] How to make PDF file downloadable in HTML link?

查看:837
本文介绍了如何使PDF文件在HTML链接中下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < a href =我正在给我的网页上的pdf文件链接下载, myfile.pdf>下载手册< / a> 

问题是当用户点击此链接时,




  • 如果用户安装了Adobe Acrobat,则在Adobe Reader中的同一浏览器窗口中打开该文件。

  • 如果Adobe Acrobat不是安装然后弹出用户下载文件。



但是我想让它总是弹出用户的下载不管Adobe Acrobat是否安装。



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

解决方案

而不是链接到.PDF文件,而是执行类似于

  a href =pdf_server.php?file = pdffilename>下载我的电子书< / a> 

输出自定义标题,打开PDF(二进制安全),并将数据打印到用户的浏览器,那么他们可以选择保存PDF,尽管他们的浏览器设置。 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(); //这并不重要。
$ fp = fopen($ file,r);
while(!feof($ fp))
{
echo fread($ fp,65536);
flush(); //这对于大量下载是至关重要的
}
fclose($ fp);

PS:显然在文件变量上运行一些健全性检查,以防止人们窃取您的文件例如不接受文件扩展名,拒绝斜杠,将.pdf添加到值


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?

解决方案

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, deny slashes, add .pdf to the value

这篇关于如何使PDF文件在HTML链接中下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆