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

时间:2022-07-01 01:51:11

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

我正在使用Yii Framework,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成功:function(data){window.open(data); }

Which i got from this site.

我是从这个网站得到的。

Can you please help

你能帮忙吗?

4 个解决方案

#1


3  

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.

这将导致浏览器下载该文件。

#2


1  

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文件。

#3


0  

Not quite, that will cause errors on some browsers, this is the correct way to set the window location.

不完全是,这会导致某些浏览器出错,这是设置窗口位置的正确方法。

window.location.assign( downloadUrlToPdf );

So

所以

  1. Send a request to make the pdf via Ajax to the server
  2. 发送请求以通过Ajax将pdf发送到服务器
  3. Process and generate the pdf on the server
  4. 在服务器上处理并生成pdf
  5. Return in the Ajax call the url to the file you just made
  6. 在Ajax中返回调用您刚刚创建的文件的url
  7. Use the above code fragment to open a download of said file
  8. 使用上面的代码片段打开所述文件的下载

#4


0  

in tcpdf , just pass this argument the Output method:

在tcpdf中,只需将此参数传递给Output方法:

$pdf->Output('yourfilename.pdf', 'D'); 

that's all

就这样

#1


3  

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.

这将导致浏览器下载该文件。

#2


1  

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文件。

#3


0  

Not quite, that will cause errors on some browsers, this is the correct way to set the window location.

不完全是,这会导致某些浏览器出错,这是设置窗口位置的正确方法。

window.location.assign( downloadUrlToPdf );

So

所以

  1. Send a request to make the pdf via Ajax to the server
  2. 发送请求以通过Ajax将pdf发送到服务器
  3. Process and generate the pdf on the server
  4. 在服务器上处理并生成pdf
  5. Return in the Ajax call the url to the file you just made
  6. 在Ajax中返回调用您刚刚创建的文件的url
  7. Use the above code fragment to open a download of said file
  8. 使用上面的代码片段打开所述文件的下载

#4


0  

in tcpdf , just pass this argument the Output method:

在tcpdf中,只需将此参数传递给Output方法:

$pdf->Output('yourfilename.pdf', 'D'); 

that's all

就这样