PDF Report Generation in PHP PHP, FPDF

👤 Sharing: AI
```php
<?php

require('fpdf/fpdf.php'); // Make sure to include the FPDF library

class PDF extends FPDF
{
    // Page header
    function Header()
    {
        // Logo (optional)
        // $this->Image('logo.png', 10, 10, 30);

        // Arial bold 15
        $this->SetFont('Arial','B',15);

        // Title
        $this->Cell(0,10,'My Sample PDF Report',0,0,'C');

        // Line break
        $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');
    }

    // Simple table
    function BasicTable($header, $data)
    {
        // Header
        foreach($header as $col)
            $this->Cell(40,7,$col,1);
        $this->Ln();

        // Data
        foreach($data as $row)
        {
            foreach($row as $col)
                $this->Cell(40,6,$col,1);
            $this->Ln();
        }
    }

    // Improved table
    function ImprovedTable($header, $data)
    {
        // Column widths
        $w = array(40, 35, 40, 45);

        // Header
        for($i=0;$i<count($header);$i++)
            $this->Cell($w[$i],7,$header[$i],1,0,'C');
        $this->Ln();

        // Data
        foreach($data as $row)
        {
            $this->Cell($w[0],6,$row[0],'LR');
            $this->Cell($w[1],6,$row[1],'LR');
            $this->Cell($w[2],6,$row[2],'LR',0,'R');
            $this->Cell($w[3],6,$row[3],'LR',0,'R');
            $this->Ln();
        }
        // Closing line
        $this->Cell(array_sum($w),0,'','T');
    }

    // Colored table
    function FancyTable($header, $data)
    {
        // Colors, line width and bold font
        $this->SetFillColor(224,235,255);
        $this->SetTextColor(0);
        $this->SetDrawColor(128,0,0);
        $this->SetLineWidth(.3);
        $this->SetFont('','B');

        // Header
        $w = array(40, 35, 40, 45);
        for($i=0;$i<count($header);$i++)
            $this->Cell($w[$i],7,$header[$i],1,0,'C',true);
        $this->Ln();

        // Color and font restoration
        $this->SetFillColor(224,235,255);
        $this->SetTextColor(0);
        $this->SetFont('');

        // Data
        $fill = false;
        foreach($data as $row)
        {
            $this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
            $this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
            $this->Cell($w[2],6,$row[2],'LR',0,'R',$fill);
            $this->Cell($w[3],6,$row[3],'LR',0,'R',$fill);
            $this->Ln();
            $fill = !$fill;
        }
        // Closing line
        $this->Cell(array_sum($w),0,'','T');
    }
}



// --- Example Usage ---
// Data (Replace with your actual data)
$header = array('Name', 'Country', 'Age', 'Value');
$data = array(
    array('John Doe', 'USA', 30, 100),
    array('Jane Smith', 'Canada', 25, 200),
    array('Peter Jones', 'UK', 40, 150),
    array('Alice Brown', 'Australia', 35, 250)
);

//Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial','',12);

// Basic Table Example
$pdf->Cell(0, 10, 'Basic Table Example:', 0, 1);
$pdf->BasicTable($header, $data);
$pdf->Ln(10); // Line Break

// Improved Table Example
$pdf->Cell(0, 10, 'Improved Table Example:', 0, 1);
$pdf->ImprovedTable($header, $data);
$pdf->Ln(10); // Line Break

// Fancy Table Example
$pdf->Cell(0, 10, 'Fancy Table Example:', 0, 1);
$pdf->FancyTable($header, $data);


// Simple Content
$pdf->Ln(10);
$pdf->Cell(0, 10, 'This is some sample text.', 0, 1);
$pdf->Cell(0, 10, 'You can add more text and customize the layout as needed.', 0, 1);


$pdf->Output('report.pdf', 'D'); // 'D' for download, 'I' for inline, 'F' to save as file
exit;

?>
```
👁️ Viewed: 9

Comments