首页 文章

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

提问于
浏览
110

我在我的网页上提供pdf文件的链接供下载,如下所示

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

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

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

  • 如果未安装Adobe Acrobat,则会弹出一个用于下载文件的用户 .

但我希望它总是弹出给用户下载,无论是否安装了“Adobe acrobat” .

请告诉我怎么做到这一点?

12 回答

  • 14

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

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

    它输出一个自定义 Headers ,打开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(); // 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:显然对“文件”变量进行了一些健全性检查,以防止人们窃取您的文件,例如不接受文件扩展名,拒绝斜杠,将.pdf添加到值

  • 3

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

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

    其中 newfilename 是用户保存文件的建议文件名 . 如果你把它留空,它将默认为服务器端的文件名,如下所示:

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

    兼容性:我在Firefox 21和Iron上进行了测试,两者都运行良好 . 它可能不适用于HTML5不兼容或过时的浏览器 . 我测试的唯一没有强制下载的浏览器是IE ...

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

  • -1

    Don't loop 通过每个文件行 . 请改用 readfile ,速度更快 . 这是关于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;
    }
    

    确保清理你的get变量,因为有人可以下载一些php文件......

  • 0

    而不是使用PHP脚本来读取和刷新文件,使用 .htaccess 重写头文件更为简洁 . 这将保留"nice" URL( myfile.pdf 而不是 download.php?myfile ) .

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

    我发现了一种方法,可以使用简单的旧HTML和JavaScript / jQuery来优雅地降级 . 在IE7-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() 打开包含该文件的新窗口 .

  • 46

    这是关键:

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

    发送PDF文件时发送内容类型application / x-pdf-document或application / pdf . Adobe Reader通常为此MIME类型设置处理程序,以便在收到任何PDF MIME类型时,浏览器会将文档传递给Adobe Reader .

  • 5

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

    大多数现代浏览器都支持它 .

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

    我知道我回答这个问题的时间已经很晚了,但我发现在javascript中有一个黑客 .

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

    试试这个:

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

    这是一种不同的方法 . 我更喜欢而不是依赖浏览器支持,或者在应用层解决这个问题,以使用Web服务器逻辑 .

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

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

    FilesMatch指令只是一个正则表达式,因此可以根据需要进行精细设置,也可以添加其他扩展名 .

    Header行与上面PHP脚本中的第一行完全相同 . 如果您还需要设置Content-Type行,您可以以相同的方式这样做,但我没有找到必要的 .

  • 0

    我使用PDF文件的整个URL解决了我的问题(而不是仅仅将文件名或位置放到href):a href =“domain.com / pdf / filename.pdf”

  • 23

    在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部分告诉浏览器打开"Would you like to download this file?"提示而不是尝试呈现PDF . 然后你就可以正常链接到该文件:http://mysite.com/myawesomepdf.pdf

相关问题