duan7007 2019-06-11 19:49
浏览 114
已采纳

FPDF:使用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.

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.

I want to load the pdf on browser, not download it

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;
  • 写回答

2条回答 默认 最新

  • dongpa3109 2019-06-12 04:24
    关注

    This is what I ended up doing:

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

    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.

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?