Search code examples
phpmysqlfpdf

Unable to output MySQL data to FPDF table


I've tried the tutorials on fpdf and searched all over for helpful code. And I found what I thought would do the trick here on SO. That said, following the example here didn't produce good results. I definitely need help with this.

Here's my code:

<?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();

?>

You can view the output here

Does anyone see what I'm doing wrong? And why are those lines being drawn? Thanks.


Solution

  • In FPDF you should fully define all cell options and your text for the cell should be in the third position:

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

    Note the concatenation. Then follow up with the border(0 for no border, which should be the default), the next line definition (Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.) and the alignment of the text in the cell.