第一页后的(FPDF,PHP,SQL)表值混乱

时间:2019-04-17 11:10:03

标签: php mysql pdf fpdf

我正在使用PHP和fPDF创建PDF“发票”,并且在第一页上一切正常,但是当表必须转到第二页时,它仅从sql查询返回第一个值,并且所有内容都转到了其余的进入第三页,依此类推。

pdf image

这是循环表行的代码

$Version = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\'
"Version $($Version.ReleaseId) (OS Build $($Version.CurrentBuildNumber).$($Version.UBR))"

这是完整的PHP代码:

allowedLateness

1 个答案:

答案 0 :(得分:-1)

在定义PDF和页面后立即进行这些更改。

$pdf = new PDF('P','mm','A4');
$pdf -> AliasNbPages();
$pdf->AddPage();
$pdf->SetAutoPageBreak(false);  // add this line and the next
$howhigh = $pdf->GetPageHeight();  // stash the height of the page for later

下一个更改是在else中,其中将计算较大单元格的大小。确实没有理由逐个检查要添加的数据。您可以将该代码块替换为:

//verificar se o texto passa a cell
if ($pdf->GetStringWidth($r['produto']) < $cellWidth) {
    //se não, não fazer nada
    $line=1;
} else {
        $line = ceil($pdf->GetStringWidth($item[2]));
        $line = round($line / $cellWidth,0) + 1;
}

最后,数据的实际输出需要进行一些更改以说明我们是否需要启动新页面。如您所见,将使用上面完成的计算。

//usar MultiCell em vez de Cell
//mas primeiro, como a MultiCell é sempre tratada como fim de linha, precisamos de 
//definir manualmente a posição xy para a próxima cell ficar ao lado.
//guardar a posição x e y antes de escrever a multicell
$xPos=$pdf->GetX();
$yPos=$pdf->GetY();
$total = $yPos + (($line * $cellHeight));
if ($total > $howhigh) {  // we will spill to a new page with this cell
    $pdf->AddPage();      // so start a new page before we add the cell
    $xPos=$pdf->GetX();
    $yPos=$pdf->GetY();
}
$pdf->MultiCell($cellWidth,$cellHeight,$r['produto'],1,'L');
//receber a posição para a próxima cell ao lado da multicell
//e equilibrar o x com o tamanho da multicell
$pdf->SetXY($xPos + $cellWidth , $yPos);