如何使用AJAX(jQuery的)下载从TCPDF(PHP)生成的PDF文件?如何使用、文件、AJAX、TCPDF

2023-09-11 00:40:45 作者:情绪迷乱 -Orch ヽ

我使用Yii框架,TCPDF和jQuery生成PDF。

I am using Yii Framework, TCPDF and jQuery to generate a pdf.

的PDF是由输入查询的形式,并用ajax提交它产生的。

The pdf is generated by inputing in a form and submitting it using ajax.

创建的PDF,但现在的问题是,当它返回给客户端,这倒不是下载。

The pdf is created but here is the problem when it returns to the client, it down not download.

下面是PHP code $ PDF->输出('夹Label.pdf','D');

here is the php code $pdf->Output('Folder Label.pdf','D');

jQuery的成功函数 成功:功能(数据){  的window.open(数据); }

the jQuery on success function has success: function(data) { window.open(data); }

这是我从这个网站得到的。

Which i got from this site.

你能帮帮

推荐答案

如果问题是,你没有得到浏览器的下载对话框的PDF,那么解决的办法就是做这种方式:

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:

首先,重定向浏览器(使用 window.location的作为其他的答案说)导航到一个特殊的控制器动作在你的应用程序,例如:这个网址: http://your.application.com/download/pdf/filename.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.

实施中的URL引用这样的操作:

Implement the action referenced in the URL like this:

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.