我有一个 PHP 文件,我在其中使用 FPDF 生成 PDF 文件。如果我执行该文件,它会在浏览器上生成并加载 pdf 文件。但是,当我尝试使用按钮并使用 AJAX 生成 PDF 文件时,它不起作用。
我正在使用 AJAX,因为我需要将一些变量发布到 PHP,以便在生成 PDF 文件之前在数据库中进行查询。
我在互联网上寻找解决方案,但我仍然不知道如何实现这一目标。
我想在浏览器上加载pdf,而不是下载它
PHP:
$pdf->Output('name.pdf','I');
AJAX:
var IDDocument = 15;
var Document = 'Invoice';
var ClientID = '205160615';
$.ajax({
type: 'POST',
url: 'PDF.php',
data: { IDDocument:IDDocument,
Document:Document,
ClientID:ClientID,
btnPDF:'btnPDF'},
success: function(data) {
//load PDF on browser.
}
});
return false;
这就是我最终所做的:
我决定使用 JQuery 对 PHP 文件进行 POST,如下所示:
function f()
{
//Variables I needed to POST to PHP
var IDDocument = 15;
var Document = 'Invoice';
var ClientID = '205160615';
//POST to PHP using JQUERY
$.post('PDF.php'{
IDDocument:IDDocument,
Document:Document,
ClientID:ClientID,
btnPDF:"btnPDF"//btnPDF is just to check if user clicked the button
},
function() //this function is to call the PHP File a second time
{
window.open('PDF.php');
});
}
然后,在 PHP 文件中我决定存储发送的变量$_SESSION
确保用户通过使用单击按钮后的变量if
健康)状况。然后,第二次调用 PHP 文件时,由于用户没有单击按钮,我检查了使用else
为了这次创建并加载 PDF 文件。因为我之前存储了变量$_SESSION
变量,我只是用它们来加载 PDF 文件,然后取消设置它们。
这是 PHP 文件中的代码:
if(isset($_POST['btnPDF'])) //Check if user clicked the button
{
//If the user clicked the button, store the variables in $_SESSION variables
$_SESSION["IDDocument"]=$_POST['IDDocument'];
$_SESSION["Document"]=$_POST['Document'];
$_SESSION["ClientID"]=$_POST['ClientID'];
}
else
{
//the second time the PHP file is called, the user didn't clicked the button.
//This second time I use the $_SESSION variables previously stored in the first
//call to the PHP file in order to create and load the PDF file
//asign $_SESSION variables to PHP variables if you want to
$IDDocument=$_SESSION["IDDocument"];
$Document=$_SESSION["Document"];
$ClientID=$_SESSION["ClientID"];
//unset the $_SESSION variables
unset($_SESSION["IDDocument"],$_SESSION["Document"],$_SESSION["ClientID"]);
//Create and load the PDF file
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)