dongxiaoke2018 2016-10-20 09:06
浏览 49
已采纳

创建类时FPDF错误

I've a problem with creation af the class needed for outputting JSON data to PDF. The PHP file is as following:

<?php    
header('Content-type: text/json');    
ob_start();    
require('fpdf.php');    

class PDF extends FPDF{    
  // Colored table    
  function FancyTable($PDFtabHead, $PDFtabData)    
  {    
    // stuff with table works if tested with another PHP page    
  }    
} // END -class    


$PDFtabHead = json_decode(($_POST['PDFtabHead']), true);    
$PDFtabData = json_decode(($_POST['PDFtabData']), true);    

$pdf = new PDF();    
$pdf->SetFont('Arial','',12);            
$pdf->AddPage();    
$pdf->FancyTable($PDFtabHead,$PDFtabData);    
$pdf->Output();    
echo json_encode(array("res"=>$PDFtabHead)); // Only to chek if outputs something    
ob_end_flush();    
?>    

The caller function from my JS file is:

$.ajax({
    type: "POST",
    async: false,
    url: './FPDF/PDF.php',
    dataType: 'json',
    data: {PDFtabHead: tabHead, PDFtabData: tabData},
    success: function (response) {

      var res = response.res;
     console.log("ok return: "+res);
    } // success
    ,
    error: function() {
      console.log("ERROR CONNECTING TO ./FPDF/PDF.php");
    }// error

  }); // ajax

tabHead and tabData are correct, this is the output of tabData :

["05:22","0043","22:31","200201","05:22","0044","22:31", ......]

The call to PDF.php always ends with error, outputting to console : ERROR CONNECTING TO ./FPDF/PDF.php

If I test it from another test page that doesn't send data in JSON format, but a normal PHP Array it works. Obviously I have to change to

$PDFtabHead = ($_POST['PDFtabHead']);
$PDFtabData = ($_POST['PDFtabData']);

in this case PDF page is rendered correctly.

Similarly if I delete the class declaration, and simply return back to my JS page the JSON array, it works; doesn't print PDF it's clear but the Array is returned as expected

  • 写回答

1条回答 默认 最新

  • duanbamo0127 2016-10-21 05:49
    关注

    OK I've got the solution at the FPDF forum http://www.fpdf.org/?go=forum&i=56364&t=56363, Oliver's answer is

    Don't return a PDF from an AJAX call.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块
  • ¥15 nhanes加权logistic回归,svyglm函数