【问题标题】:FPDF: Output PDF using AJAXFPDF:使用 AJAX 输出 PDF
【发布时间】:2019-06-11 19:49:06
【问题描述】:

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

我正在使用 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;

【问题讨论】:

  • 浏览器是否自动下载 PDF 几乎在所有情况下都取决于用户。如果您使用 fPDF ($ourpdf->Output('S')) 的 string 输出选项,您可以设置标题并随您的 PDF 一起发送。
  • 它不起作用

标签: javascript php ajax fpdf


【解决方案1】:

这就是我最终做的:

我决定使用 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 文件中,我决定在使用if 条件确保用户单击按钮后,存储在$_SESSION 变量中发送的变量。然后,第二次调用 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
}

【讨论】:

    【解决方案2】:

    您可以将浏览器(成功时)重定向到生成的 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;
        }
    });
    

    PDF 文件的 URL 必须在服务器端生成(在 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]);
    

    【讨论】:

    • 但是如何获取创建的 PDF 的 URL?
    • URL 必须在服务器端生成(在 PHP 中)。
    【解决方案3】:

    这是我最终做的:

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

    然后在 php 方面,我只是回显并 iframe 将 url 指向 pdf 文件集

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

    然后最后在pdf.php中

    我有 pdf 生成代码

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-23
      • 2011-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-12
      • 2013-01-03
      • 2022-12-13
      相关资源
      最近更新 更多