无法将 MySQL 数据输出到 FPDF 表


Unable to output MySQL data to FPDF table

我已经尝试了fpdf上的教程,并搜索了有用的代码。我找到了我认为可以在SO上解决问题的方法。也就是说,遵循此处的示例并没有产生好的结果。我绝对需要这方面的帮助。

这是我的代码:

<?php
require_once '../root_login.php';
require('fpdf.php');
class PDF extends FPDF
{
// Page header
function Header()
{
// Logo
//$this->Image('images/bg_all.jpg',10,6,30);
// Arial bold 15
$this->SetFont('Times','B',20);
// Move to the right
$this->Cell(80);
// Title
$this->Cell(40,10,'B A S E B A L L',0,0,'C');
// Line break
$this->Ln(6);
// Arial bold 15
$this->SetFont('Arial','B',9);
$this->Cell(200,10,'LITTLE LEAGUE ROSTERS',0,0,'C');

$this->Ln(20);
}
// Page footer
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}
// Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',8);
$stmt = $db->prepare('SELECT fname, lname 
                    FROM rosters 
                    ORDER BY lname');
$stmt->execute();
$result = $stmt->fetchALL(PDO::FETCH_ASSOC);
foreach($result as $row) {
$pdf->Cell(0,10,'F NAME:', $row['fname']); 
$pdf->Ln();
$pdf->Cell(0,5,'L NAME:', $row['lname']);
$pdf->Ln();
}
$pdf->Output();
?>

您可以在此处查看输出

有人看到我做错了什么吗?为什么要画这些线?谢谢。

在 FPDF 中,您应该完全定义所有单元格选项,并且单元格的文本应位于第三个位置:

$pdf->Cell(0,5,'L NAME:'. $row['lname'], 0, 0, 'L'); 
------------------------^

请注意串联。然后跟进 border(0 表示无边框,应该是默认值),下一行定义(放置 1 相当于放置 0 并在后面调用 Ln()。默认值:0.) 和单元格中文本的对齐方式。

连接字符串并使用较少的参数执行此操作?

  foreach($result as $row) {
    $pdf->Cell(0,10,'F NAME: '.$row['fname']); 
    $pdf->Ln();
    $pdf->Cell(0,5,'L NAME: '.$row['lname']);
    $pdf->Ln();
    }