简体   繁体   中英

fpdf add title and table header on each page

<?Php
require('fpdf/fpdf.php');
$host = "localhost"; 
$username = "root"; 
$password = "";  
$db_name = "Students"; 

$con = new mysqli($host, $username, $password, $db_name);

$count = "SELECT * FROM class";

$pdf = new FPDF(); 
$pdf->AddPage();

$width_cell=array(70,40,30,30,20);
$pdf->SetFont('Arial','B',12);

$pdf->SetFillColor(193,229,252);

$pdf->Cell($width_cell[0],10,'ID No',1,0,'C',true);
$pdf->Cell($width_cell[1],10,'Name',1,0,'C',true);
$pdf->Cell($width_cell[2],10,'Surname',1,0,'C',true); 
$pdf->Cell($width_cell[3],10,'Cell No',1,0,'C',true);
$pdf->Cell($width_cell[4],10,'Teacher',1,1,'C',true); 

$pdf->SetFont('Arial','',11);
$pdf->SetFillColor(235,236,236); 
$fill=false;


foreach ($con->query($count) as $row) {
$pdf->Cell($width_cell[0],10,$row['Id_no'],1,0,'C',$fill);
$pdf->Cell($width_cell[1],10,$row['Name'],1,0,'C',$fill);
$pdf->Cell($width_cell[2],10,$row['Surname'],1,0,'C',$fill);
$pdf->Cell($width_cell[3],10,$row['CellNO'],1,0,'C',$fill);
$pdf->Cell($width_cell[4],10,$row['Teacher'],1,1,'C',$fill);
$fill = !$fill;
}

$pdf->Output();
?>

Question :How do i put in a title at the top of my page for each page. and the table header on each page. Been struggling and i tried the fpdf tutorial page on the website. But i still can't get it to work

The way that you are expected to do this, at least from the comments in FPDF, is to inherit from the base FPDF class and implement the Header method:

use Fpdf\Fpdf;

class MyPdf extends Fpdf {
    function Header() {
        $this->Cell(100, 100, 'TITLE');
    }
}

$pdf = new MyPdf();

Additionally, there is a Footer method, which can be overridden in the same fashion if you need to have a footer on each page.

I believe that you should extend the Fpdf class and work out a complete header section and footer section which will overwrite the default Header and Footer. See the following example.

class PDF extends FPDF
{
// Page header
function Header()
{

    $this->SetFillColor(54,80,104);
    $this->SetDrawColor(150,150,150);
    $this->Rect(0,0,220,50,'F');

    $this->SetFont('helvetica','B',18);
    $this->SetTextColor(255,255,255);
    $this->SetXY(10,5);
    $this->Cell(100,15,"My Title",0,0,'R');


}

// Page footer
function Footer()
{
    $this->SetY(-10);
    $this->SetFont('helvetica','I',7);

    $this->SetXY(100,-22);
    $this->Cell(100,15,'My Footer',0,0,'L');



}
}

Then instead of using a

$pdf = new FPDF(); 

You should use

$pdf = new PDF(); 

This way the same header and footer will be applied to all of your pages

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM