【问题标题】:fpdf table rows breaks from second pagefpdf 表格行从第二页中断
【发布时间】:2016-05-03 05:39:32
【问题描述】:

我正在尝试使用 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();
?>

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

【问题讨论】:

    标签: pdf fpdf


    【解决方案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 循环中

    【讨论】:

    • 它显示Fatal error: Call to undefined method PDF::GetPageHeight()。我必须在我的代码上方包含您的代码吗?
    • 有趣 - 使用最新版本的 FPDF 对我来说效果很好 - 尝试将 $pdf->GetPageHeight() 更改为 $pdf->h
    • 将其更改为$pdf-&gt;h 后,错误不会出现,但我的问题没有解决。我的输出仍然没有变化。 :(
    • 我在 $pdf-&gt;Output(); 之前的代码末尾添加了您的代码。对吗?
    • 不 - 当您在循环中增加 yAxis 时需要包含它。
    猜你喜欢
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 2023-03-30
    • 2018-05-22
    相关资源
    最近更新 更多