FPDF中的致命错误

时间:2014-04-12 18:53:59

标签: php fpdf

我正在尝试使用fpdf17创建一个pdf表,并在此if语句的最后一行(而非}

收到错误
if($fill || $border==1)
{
    if($fill)
        $op = ($border==1) ? 'B' : 'f';
    else
        $op = 'S';
    $s = sprintf('%.2F %.2F %.2F %.2F re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
}

错误说:Fatal error: Unsupported operand types in C:\myfilepath\fpdf17\fpdf.php on line 630 我有两个课程:我的PDFTable课程:

<?php
require('../fpdf17/fpdf.php');

class PDFTable extends FPDF
{
// Load data
function LoadData($file)
{
    // Read file lines
    $lines = file($file);
    $data = array();
    foreach($lines as $line)
        $data[] = explode(';',trim($line));
    return $data;
}

// Simple table
function BasicTable($header, $data)
{
    // Header
    foreach($header as $col)
        $this->Cell(40,7,$col,1);
    $this->Ln();
    // Data
    foreach($data as $row)
    {
        foreach($row as $col)
            $this->Cell(40,6,$col,1);
        $this->Ln();
    }
}

// Better table
function ImprovedTable($header, $data, $widths)
{
    // Column widths
    $w = $widths;
    // Header
    for($i=0;$i<count($header);$i++)
        $this->Cell($w[$i],7,$header[$i],1,0,'C');
    $this->Ln();
    // Data
    foreach($data as $row)
    {
        $this->Cell($w[0],6,$row[0],'LR');
        $this->Cell($w[1],6,$row[1],'LR');
        $this->Cell($w[2],6,$row[1],'LR');
        $this->Cell($w[3],6,$row[1],'LR');
        $this->Cell($w[4],6,$row[1],'LR');
        $this->Ln();
    }
    // Closing line
    $this->Cell(array_sum($w),0,'','T');
}

// Colored table
function FancyTable($header, $data)
{
    // Colors, line width and bold font
    $this->SetFillColor(255,0,0);
    $this->SetTextColor(255);
    $this->SetDrawColor(128,0,0);
    $this->SetLineWidth(.3);
    $this->SetFont('','B');
    // Header
    $w = array(40, 35, 40, 45);
    for($i=0;$i<count($header);$i++)
        $this->Cell($w[$i],7,$header[$i],1,0,'C',true);
    $this->Ln();
    // Color and font restoration
    $this->SetFillColor(224,235,255);
    $this->SetTextColor(0);
    $this->SetFont('');
    // Data
    $fill = false;
    foreach($data as $row)
    {
        $this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
        $this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
        $this->Cell($w[2],6,number_format($row[2]),'LR',0,'R',$fill);
        $this->Cell($w[3],6,number_format($row[3]),'LR',0,'R',$fill);
        $this->Ln();
        $fill = !$fill;
    }
    // Closing line
    $this->Cell(array_sum($w),0,'','T');
}
}
?>

和我的Deposits课程甚至没有被调用,所以它并不重要。


这是我创造一切的地方:

<?php
//check if logged in
require_once('../config/db.php');
require_once('../classes/Login.php');
$login = new Login();
if ($login->isUserLoggedIn() == false) { $login->doLogout(); }

//Connect to your database
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

require_once('deposit_class.php');
require_once('table_class.php');
$deposit = new Deposit();
$pdf = new PDFTable();

$query = "SELECT r.departure AS depart, r.id AS reservation, u.unit_name AS unit, 
            CONCAT(g.fname, ' ', g.lname) AS guest, SUM(p.payment_amt) AS total
            FROM reservations r 
            JOIN units u ON r.unit = u.id 
            JOIN guests g ON r.guest = g.id 
            JOIN payments p ON r.id = p.reservation
            WHERE r.departure >= LAST_DAY(NOW() - INTERVAL 1 MONTH) + INTERVAL 1 DAY
            GROUP BY r.id
            ORDER BY r.departure ASC";
$result = mysqli_query($con, $query);
$file = 'deposits.txt';
while ($row = mysqli_fetch_assoc($result)) {
    $content = $row['depart'].';'.$row['reservation'].';'.$row['unit'].';'.$row['guest'].';'.$row['total'].'\n';
    file_put_contents($file, $content, FILE_APPEND | LOCK_EX);
}

$pdf->SetFont('Arial','',14);
$pdf->AddPage();

//send file to Table
$data = $pdf->LoadData($file);      
unlink('deposits.txt');         //unlink file

//create header
$header[] = array("Depart", "Reservation", "Unit", "Guest", "Total");

//create table
$widths[] = array(25,20,40,40,15);
$deposit_table = $pdf->ImprovedTable($header, $data, $widths);
$pdf->Output();
?>

知道我为什么会收到错误吗?这似乎发生在这里:$deposit_table = $pdf->ImprovedTable($header, $data, $widths);

我在其他地方使用它就好了。


编辑:我在PDFTable课程中发现它只输出here一次,然后输出错误:

// Header
    for($i=0;$i<count($header);$i++){
        echo "here";
        $this->Cell($w[$i],7,$header[$i],1,0,'C');
    }
    $this->Ln();

3 个答案:

答案 0 :(得分:0)

此表达式中有错误:

$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op)

为什么:它取决于操作数的值。例如,也许其中一个是数组。

所以你刚发出

var_dump($this->x, $k, $this->h, $this->y, $w, $h);

在麻烦的指令之前,看看价值观。或者,如果您有可用的调试器,请在该指令处放置一个断点,给出错误的断点,并手动检查变量。

我注意到您同时拥有$this->h$h。那是对的吗?如果是这样,您可能希望以不同的方式命名变量,以避免含糊不清。

答案 1 :(得分:0)

我发现你必须以某种方式将数组发送到ImprovedTable函数中。它必须定义如下:$array = array();而不是$array[] = array();

为什么呢?我不知道。

答案 2 :(得分:0)

这太老了,但我找到了自己代码的答案。如果我们犯了这样的错误:

$w = array(40, 35, 45, 40);

然后

foreach($header as $col)
  $this->Cell($w,7,$col,1);

FPDF返回错误(行上的fpdf.php中不支持的操作数类型...)。因为$ w是一个数组。您必须始终传递一个int值。

那样做

for( $i = 0; $i < count($header); $i++ )
  Cell($w[$i], 15, $header[$i], 1);

解决。