如何使用 AJAX (jQuery) 下载从 TCPDF (PHP) 生成的 PDF 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5500485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to download PDF File Generated from TCPDF (PHP) using AJAX (jQuery)?
提问by Wenceslaus D'silva
I am using Yii Framework, TCPDF and jQuery to generate a pdf.
我正在使用 Yii 框架、TCPDF 和 jQuery 生成 pdf。
The pdf is generated by inputing in a form and submitting it using ajax.
pdf 是通过在表单中输入并使用 ajax 提交来生成的。
The pdf is created but here is the problem when it returns to the client, it down not download.
pdf 已创建,但是当它返回到客户端时出现问题,它不会下载。
here is the php code
$pdf->Output('Folder Label.pdf','D');
这是php代码
$pdf->Output('Folder Label.pdf','D');
the jQuery on success function has
success: function(data) {
window.open(data);
}
成功函数上的 jQuery 有
success: function(data) {
window.open(data);
}
Which i got from this site.
我从这个网站上得到的。
Can you please help
你能帮忙吗
回答by Jon
If the problem is that you are not getting the browser's download dialog for the PDF, then the solution is to do it this way:
如果问题是您没有获得 PDF 的浏览器下载对话框,那么解决方案是这样做:
First, redirect the browser (using window.location
as the other answers say) to navigate to a special controller action in your application, e.g. with this url: http://your.application.com/download/pdf/filename.pdf
.
首先,重定向浏览器(使用window.location
其他答案)以导航到应用程序中的特殊控制器操作,例如使用此 url: http://your.application.com/download/pdf/filename.pdf
。
Implement the action referenced in the URL like this:
像这样实现 URL 中引用的操作:
public function actionPdf() {
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="filename.pdf";');
header('Content-Length: '.filesize('path/to/pdf'));
readfile('path/to/pdf');
Yii::app()->end();
}
This will cause the browser to download the file.
这将导致浏览器下载文件。
回答by Treffynnon
You need to save the PDF to somewhere on your server and then issue window.location = '/url/to/pdf-you-just-saved.pdf';
from your javascript. The users browser will then prompt them to download the PDF file.
您需要将 PDF 保存到服务器上的某个位置,然后window.location = '/url/to/pdf-you-just-saved.pdf';
从您的 javascript发出。用户浏览器将提示他们下载 PDF 文件。
回答by Joseph Lust
Not quite, that will cause errors on some browsers, thisis the correct way to set the window location.
不完全是,这会导致某些浏览器出错,这是设置窗口位置的正确方法。
window.location.assign( downloadUrlToPdf );
So
所以
- Send a request to make the pdf via Ajax to the server
- Process and generate the pdf on the server
- Return in the Ajax call the url to the file you just made
- Use the above code fragment to open a download of said file
- 通过 Ajax 向服务器发送制作 pdf 的请求
- 在服务器上处理并生成pdf
- 在 Ajax 中返回调用您刚刚创建的文件的 url
- 使用上面的代码片段打开所述文件的下载
回答by Aaska Patel
in tcpdf , just pass this argument the Output method:
在 tcpdf 中,只需将此参数传递给 Output 方法:
$pdf->Output('yourfilename.pdf', 'D');
that's all
就这样