fpdf表行从第二页中断

时间:2016-05-03 05:39:33

标签: pdf fpdf

我正在尝试使用FPDF输出从数据库中提取的数据表。

我的问题是输出的第一页按照预期的那样出现,但是在表格在第一页结束并进入第二页之后,表格的行每页都会进入一行。

我尝试搜索整个互联网的东西,但我找不到合适的答案参考我的下面的代码。下面是我的fpdf文件代码。

<?php

require('fpdf.php');
include("pdoconnect.php");

class PDF extends FPDF
{
// Page header
function Header()
{
$this->Image('picture.png',10,6,30);
$this->SetFont('Arial','B',15);
// Move to the right
$this->Cell(80);
// Title
$this->SetTextColor( 255, 119, 0 );
$this->Cell(30,10,'Report',0,0,'C');
 // Line break
$this->Line(10, 22, 210-10, 22);
$this->Ln(20);
}

// Page footer
function Footer()
{   
$this->Line(10, 280, 210-10, 280);
$this->SetY(-15);
$this->SetFont('Arial','I',8);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
  }
}

$result='SELECT * FROM report WHERE time BETWEEN "'.$_POST["fromdate"].'" AND "'.$_POST["todate"].'"';
$link=$db->prepare($result);
$link->execute();
$resultset=$link->fetchAll();
$count=$link->rowCount();

$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->SetTitle("Report"); 
$pdf->AddPage();

$row_height = 10;
$y_axis=30;


$pdf->SetY($y_axis);
$pdf->SetX(25);
$pdf->Cell(30, 10,"", 0, 0, 1);
$y_axis = $y_axis + $row_height;



$pdf->SetDrawColor(128,0,0);
$pdf->SetTextColor(102, 68, 34 );
$pdf->SetFont('Arial', 'B', 10);
$pdf->SetY($y_axis);
$pdf->SetX(11);
$pdf->Cell(34,10,'Order ID',1,0,'C');
$pdf->Cell(35,10,'Name',1,0,'C');
$pdf->Cell(30,10,'TID',1,0,'C');
$pdf->Cell(20,10,'Quantity',1,0,'C');
$pdf->Cell(20,10,'Date',1,0,'C');
$pdf->Cell(20,10,'Time',1,0,'C');
$pdf->Cell(30,10,'Bill Amount',1,0,'C');
$y_axis = $y_axis + $row_height;
$total=0;
foreach($resultset as $row)
{
    $len=strlen($row['name']);
    if($len>21)
{
     $name=substr($row['name'],0,19)."..";
}
 else
{
    $name = $row['name'];
 }



$oid    = $row['order_id'];

$tid    = $row['t_id'];
$qty    = $row['quantity'];
$date   = $row['date'];
$time   = $row['time'];
$amt    = $row['bill_amount'];

$total=$total+$amt;

$pdf->SetDrawColor(128,0,0);
$pdf->SetTextColor(0);
$pdf->SetFont('Arial', '', 9);
$pdf->SetY($y_axis);
$pdf->SetX(11);
$pdf->Cell(34, 10, $oid, 1, 0, 'L');
$pdf->Cell(35, 10, $name, 1, 0, 'L');
$pdf->Cell(30, 10, $tid, 1, 0, 'C');
$pdf->Cell(20, 10, $qty, 1, 0, 'C');
$pdf->Cell(20, 10, $date, 1, 0, 'C');
$pdf->Cell(20, 10, $time, 1, 0, 'C');
$pdf->Cell(30, 10, $amt, 1, 0, 'R');

$y_axis = $y_axis + $row_height;
$pdf->SetY(10);
$pdf->SetX(170);




}

$totalre=$total-$r_amt;
$pdf->SetDrawColor(128,0,0);
$pdf->SetTextColor(0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->SetY($y_axis);
$pdf->SetX(137);
$pdf->Cell(42, 10,'Total', 0, 0, 'C');
$pdf->SetTextColor(255,0,0);
$pdf->Cell(25, 10, $totalre , 0, 0, 'C');
$y_axis = $y_axis + $row_height;
$pdf->SetAutoPageBreak(false,20);
$pdf->Output();
?>

我希望第二页显示为第一页而不拆分表行。请帮忙。

1 个答案:

答案 0 :(得分:1)

这是因为yAxis大于页面的高度,您需要手动添加页面并使用类似于以下内容的方式重置yAxis:

if ($y_axis + $row_height >= $pdf->GetPageHeight() - 20)
{
    $pdf->AddPage();
    $y_axis = 30;
}

增加yAxis后(-20是允许页脚空间)

代码应包括在内:

$y_axis = $y_axis + $row_height;

if ($y_axis + $row_height >= $pdf->GetPageHeight() - 20)
{
    $pdf->AddPage();
    $y_axis = 30;
}

$pdf->SetY(10);
$pdf->SetX(170);
在你的foreach循环中