沃梦达 / 编程问答 / php问题 / 正文

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

How to download PDF File Generated from TCPDF (PHP) using AJAX (jQuery)?(如何使用 AJAX (jQuery) 下载从 TCPDF (PHP) 生成的 PDF 文件?)

本文介绍了如何使用 AJAX (jQuery) 下载从 TCPDF (PHP) 生成的 PDF 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Yii 框架、TCPDF 和 jQuery 生成 pdf.

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

通过在表单中​​输入并使用ajax提交生成pdf.

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代码<代码>$pdf->Output('文件夹标签.pdf','D');

成功函数的jQuery有<代码>成功:功能(数据){窗口.打开(数据);}

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

我从这个网站得到的.

你能帮忙吗

推荐答案

如果问题是你没有得到浏览器的 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.

这篇关于如何使用 AJAX (jQuery) 下载从 TCPDF (PHP) 生成的 PDF 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

基础教程推荐