使用php强制下载pdf [英] Using php to force download a pdf

查看:112
本文介绍了使用php强制下载pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个网站有一个强制下载pdf的按钮。



按钮的html:

 < a href = scripts / download.php> 
< input type =imagesrc =images / download.gifalt =提交按钮/>
< / a>

到目前为止,php脚本:

 <?php 
header('Content-Type:application / pdf');
header('Content-disposition:attachment; filename = documents / ECM_IT_ResumeDownload.pdf');
readfile('documents / ECM_IT_ResumeDownload.pdf');
?>

这似乎是下载文件,但是当我打开它,我得到这个错误: p>

Adobe Reader无法打开documents_ECM_IT_ResumeDownload.pdf,因为它不是受支持的文件类型或因为文件已被损坏(例如,它作为电子邮件发送附件并没有正确解码)



任何帮助将不胜感激。



编辑
在文本编辑器中打开pdf,并收到以下消息:





警告:readfile(documents / ECM_IT_ResumeDownload.pdf)[function.readfile]:无法打开流: html / scripts / download.php 中没有这样的文件或目录 4



该文件绝对是在那里。在html / documents / ECM_IT_ResumeDownload.pdf中

解决方案

你是否尝试摆脱关闭PHP标签(?>)结束?它将页面视为一个纯PHP页面,删除可能会意外附加到输出结尾的任何可能的新行。当我动态创建excel文件进​​行下载时,这有助于我,而且它们正在下载被破坏。查看此页面了解更多信息:



http://www.php.net/manual/en/language.basic-syntax.phptags.php



从编辑过的问题中,PHP似乎无法找到该文件。尝试使用文件的绝对路径:c:\blah\de\blah\bloo.pdf或c:/blah/de/blah/bloo.pdf。如果其中一个路径正常工作和下载,您的相对路径在某些方面是不正确的。


Im trying to get a website to have a button that forces a download of a pdf.

Heres the html of the button:

    <a href=scripts/download.php>
    <input type="image" src="images/download.gif" alt="Submit button"/>
    </a>

And the php script so far:

    <?php
    header('Content-Type: application/pdf');
    header('Content-disposition: attachment;filename=documents/ECM_IT_ResumeDownload.pdf');
    readfile('documents/ECM_IT_ResumeDownload.pdf');
    ?>

This seems to download the file fine but when I go to open it i get this error:

"Adobe Reader could not open 'documents_ECM_IT_ResumeDownload.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)."

Any help would be greatly appreciated.

EDIT Opened the pdf in a text editor and got this message:

"
Warning: readfile(documents/ECM_IT_ResumeDownload.pdf) [function.readfile]: failed to open stream: No such file or directory in html/scripts/download.php on line 4
"

The document is definitely there though. in html/documents/ECM_IT_ResumeDownload.pdf

解决方案

Have you tried getting rid of the closing PHP tag (the ?>) at the end? It will treat the page as a pure PHP page, removing any possible new lines that might accidentally get appended to the end of the output. This helped me when I was dynamically creating excel files for download, and they were downloading as corrupted. Check out this page for more information:

http://www.php.net/manual/en/language.basic-syntax.phptags.php

From your edited question, it seems like PHP is unable to find the file. Try using an absolute path to the file like so: "c:\blah\de\blah\bloo.pdf" or "c:/blah/de/blah/bloo.pdf". If one of those paths works and downloads correctly, your relative path is incorrect in some way.

这篇关于使用php强制下载pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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