简体   繁体   English

如果通过AJAX调用脚本,则TCPDF Output()函数方法不起作用,但是如果在浏览器中调用脚本,则TCPDF Output()函数方法有效

[英]TCPDF Output() function method not working if script is called through AJAX, but works if script is called in browser

I'm using TCPDF to dynamically generate PDF documents. 我正在使用TCPDF动态生成PDF文档。 I am using jQuery's $.ajax() method to call the PDF generation script ( pdf_output.php ) which saves the PDF to the server's file system using TCPDF's Output() method: 我正在使用jQuery的$.ajax()方法调用PDF生成脚本( pdf_output.php ),该脚本使用TCPDF的Output()方法将PDF保存到服务器的文件系统中:

//Close and output PDF document
$pdf->Output("Account Summary - ".$client_data['name'].".pdf", 'F');

This works great when calling the PHP script directly from the browser, but doesn't work when calling the script via AJAX. 当直接从浏览器直接调用PHP脚本时,此方法非常有用,但通过AJAX调用该脚本时则无效。 The script executes (I can print $_POST variables from the PHP script into the developer console via echo json_encode() and the script returns a value at the end), but the PDF never is saved to the server's filesystem. 该脚本执行(我可以通过echo json_encode()将PHP脚本中的$_POST变量打印到开发人员控制台中,并且该脚本最后返回一个值),但是PDF永远不会保存到服务器的文件系统中。

However, if I run the pdf_output.php script directly in the browser, it works! 但是,如果我直接在浏览器中运行pdf_output.php脚本,它将起作用!

Here's how I'm calling the output script: 这是我调用输出脚本的方式:

Javascript: 使用Javascript:

function trigger(client_number, month, year)
{
  $("#selected_client").val(client_number);
  $('#cash_recon').modal('show'); 

  var function_name = "_generate_pdf_closing";
  var data =
      { 
        function_name: function_name,
        client_id: client_number,
        month: month,
        year: year
      }
  console.dir(data);
   $.ajax({
      type: "POST",
      url: "ajax/inspect.php",
      data: data,
      success: function (data) 
      {
        console.log("Success callback executed!");
      },

});

Inspect.php Inspect.php

<?php
include_once('../includes/data_functions.php');
$function_name = $_REQUEST['function_name'];
switch ($function_name) 
{
    case '_generate_pdf_closing':
        print (generate_pdf_closing($_REQUEST['client_id'], $_REQUEST['month'], $_REQUEST['year']));
        break;
}
?>

generate_pdf_closing() from data_functions.php: 从data_functions.php generate_pdf_closing():

function generate_pdf_closing($client_id, $month, $year){

  include("../pdf/pdf_output.php");
  return $result;

}

To clarify: If I call the pdf_output.php file in the browser, it generates and saves the PDF to the server perfectly. 澄清pdf_output.php :如果我在浏览器中调用pdf_output.php文件,它将生成PDF并将其完美地保存到服务器。 However, if I call the pdf_output.php script via AJAX, it appears to execute but the PDF is never generated/saved to the server. 但是,如果我通过AJAX调用pdf_output.php脚本,则该脚本似乎可以执行,但PDF永远不会生成/保存到服务器。

Ideas? 想法? Any help is appreciated :) 任何帮助表示赞赏:)

This was resolved! 解决了! The issue was that we had to use an absolute path when defining the file location of the Output() method. 问题是,在定义Output()方法的文件位置时,我们必须使用绝对路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM