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

本文是小编为大家收集整理的关于FPDF。使用AJAX输出PDF的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个PHP文件,在该文件中我使用FPDF生成PDF文件.如果我执行该文件,它将生成并在浏览器上加载PDF文件.但是,当我尝试使用按钮生成PDF文件时,并且使用AJAX不起作用.

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

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

我想将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文件:

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文件中,我决定将发送的变量存储在确保用户使用if条件单击按钮之后发送的变量.然后,第二次调用PHP文件,因为用户没有单击我检查的按钮,请在ODER中使用else这次使用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
}

其他推荐答案

您可以将浏览器(成功)重定向到生成的PDF文件.可以通过AJAX响应获取URL.

示例:

$.ajax({  
    type: 'POST', 
    url: 'PDF.php',  
    data: { IDDocumento:IDDocumento, 
            TipoDocumento:TipoDocumento,  
            CedulaCliente:CedulaCliente, 
            btnPDF:'btnPDF'
    },
    success: function(data) {
        // redirect to the generated pdf file
        window.location = data.url;
    }
});

必须在服务器端(以PHP为单位)生成PDF文件的URL.

<?php
// pdf.php

// Generate PDF here
// ...

// Generate url
// Use an UUID to make sure that nobody can guess the url
$url = 'filename-with-uuid.pdf';

// Send json response
header('Content-Type: application/json');
echo json_encode(['url' => $url]);

其他推荐答案

这是我最终做的:

$.post(ajaxurl,data,function(html, response){
      $(responsediv).html(html)
}

然后在PHP侧我只是回声,然后用URL iframe到PDF文件集

$_SESSION['data']=$_POST['data']
echo <iframe src=pdf.php style="width:100%;height:100vh"

,最后在pdf.php

我有PDF生成代码

$pdf->Output('name.pdf',I);

本文地址:https://www.itbaoku.cn/post/2090921.html

问题描述

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;

推荐答案

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
}

其他推荐答案

You may redirect the browser (on success) to the generated pdf file. The url could be fetch via the ajax response.

Example:

$.ajax({  
    type: 'POST', 
    url: 'PDF.php',  
    data: { IDDocumento:IDDocumento, 
            TipoDocumento:TipoDocumento,  
            CedulaCliente:CedulaCliente, 
            btnPDF:'btnPDF'
    },
    success: function(data) {
        // redirect to the generated pdf file
        window.location = data.url;
    }
});

The URL for the PDF file must be generated on the server side (in PHP).

<?php
// pdf.php

// Generate PDF here
// ...

// Generate url
// Use an UUID to make sure that nobody can guess the url
$url = 'filename-with-uuid.pdf';

// Send json response
header('Content-Type: application/json');
echo json_encode(['url' => $url]);

其他推荐答案

here's what i ended up doing:

$.post(ajaxurl,data,function(html, response){
      $(responsediv).html(html)
}

then on php side i simply echo'd back and iframe with the url to the pdf file set

$_SESSION['data']=$_POST['data']
echo <iframe src=pdf.php style="width:100%;height:100vh"

and then finally in the pdf.php

i have the pdf generation code

$pdf->Output('name.pdf',I);