FPDF:使用AJAX输出PDF [英] FPDF: Output PDF using AJAX

查看:75
本文介绍了FPDF:使用AJAX输出PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP文件,其中使用FPDF生成PDF文件.如果我执行该文件,它将生成pdf文件并将其加载到浏览器中.但是,当我尝试使用按钮并使用AJAX生成PDF文件时,此方法将无效.

I have a PHP file where I generate a PDF file using FPDF. If I execute that file, it generates and loads the pdf file on browser. However when I try to generate the PDF file using a button and by using AJAX it does not work.

我正在使用AJAX,因为我需要在生成PDF文件之前将一些变量发布到PHP以便在数据库中进行查询.

I am using AJAX since I need to post some variables to PHP to make a query in the database before generating the PDF file.

我在互联网上寻找解决方案,但我仍然不知道如何实现这一目标.

I looked for solutions on internet but I still do not know how to achieve this.

我想在浏览器中加载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;

推荐答案

这就是我最终要做的事情:

This is what I ended up doing:

我决定使用JQuery对这样的PHP文件进行POST:

I decided to use JQuery to make a POST to a PHP file like this:

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');
                         });

    }

然后,在确保用户使用if条件单击按钮之后,我决定在PHP文件中存储在$_SESSION变量中发送的变量.然后,第二次调用PHP文件,因为用户没有单击按钮,所以我检查了这次是否使用else来创建和加载PDF文件.由于我之前已将变量存储在$_SESSION变量中,因此我仅使用它们来加载PDF文件,然后取消设置它们.

Then, in the PHP File I decided to store the variables sent in $_SESSION variables after making sure the user clicked the button by using an if condition. And then, the second time the PHP file is called, as the user did not clicked the button I checked that using an else in oder to create and load the PDF file this time. Since I have the variables previously stored in $_SESSION variables, I just used them to load the PDF file and then I unset them.

这是PHP文件中的代码:

this is the code in the PHP file:

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
}

这篇关于FPDF:使用AJAX输出PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆