<?php
class PDF extends FPDF
{
    public $reportTitle = 'Detailed Sales Report (Invoice & Products)';
    public $periodText  = '';
    public $categoryText = 'All';

    function Header()
    {
        $this->SetFont('Arial', 'B', 13);
        $this->Cell(0, 7, $this->reportTitle, 0, 1, 'C');

        $this->SetFont('Arial', '', 9);
        $this->Cell(0, 5, 'Period: ' . $this->periodText . '    Category: ' . $this->categoryText, 0, 1, 'C');
        $this->Ln(2);

        $this->PrintTableHeader();
    }

    function Footer()
    {
        $this->SetY(-12);
        $this->SetFont('Arial', 'I', 8);
        $this->Cell(0, 5, 'Page ' . $this->PageNo(), 0, 0, 'C');
    }

    function PrintTableHeader()
    {
        $this->SetFont('Arial', 'B', 7);
        $this->SetFillColor(230, 230, 230);

        $this->Cell(8,  8, 'S#',      1, 0, 'C', true);
        $this->Cell(30, 8, 'Invoice / Date', 1, 0, 'C', true);
        $this->Cell(42, 8, 'Customer / Product', 1, 0, 'C', true);
        $this->Cell(18, 8, 'Weight',  1, 0, 'C', true);
        $this->Cell(16, 8, 'Carton',  1, 0, 'C', true);
        $this->Cell(16, 8, 'Qty',     1, 0, 'C', true);
        $this->Cell(20, 8, 'Wt(KG)',  1, 0, 'C', true);
        $this->Cell(22, 8, 'Gross',   1, 0, 'C', true);
        $this->Cell(20, 8, 'Disc.',   1, 0, 'C', true);
        $this->Cell(22, 8, 'Excl Tax',1, 0, 'C', true);
        $this->Cell(18, 8, 'Sales Tax',1, 0, 'C', true);
        $this->Cell(18, 8, 'Adv Tax', 1, 0, 'C', true);
        $this->Cell(22, 8, 'Incl Total',1, 1, 'C', true);
    }

    function CheckPageBreakCustom($height)
    {
        if ($this->GetY() + $height > 185) {
            $this->AddPage('L', 'A4');
        }
    }

    function RowCells($data, $fills = false, $fontStyle = '', $fontSize = 7)
    {
        $this->SetFont('Arial', $fontStyle, $fontSize);

        $this->Cell(8,  6, $data[0],  1, 0, 'C', $fills);
        $this->Cell(30, 6, $data[1],  1, 0, 'L', $fills);
        $this->Cell(42, 6, $data[2],  1, 0, 'L', $fills);
        $this->Cell(18, 6, $data[3],  1, 0, 'C', $fills);
        $this->Cell(16, 6, $data[4],  1, 0, 'R', $fills);
        $this->Cell(16, 6, $data[5],  1, 0, 'R', $fills);
        $this->Cell(20, 6, $data[6],  1, 0, 'R', $fills);
        $this->Cell(22, 6, $data[7],  1, 0, 'R', $fills);
        $this->Cell(20, 6, $data[8],  1, 0, 'R', $fills);
        $this->Cell(22, 6, $data[9],  1, 0, 'R', $fills);
        $this->Cell(18, 6, $data[10], 1, 0, 'R', $fills);
        $this->Cell(18, 6, $data[11], 1, 0, 'R', $fills);
        $this->Cell(22, 6, $data[12], 1, 1, 'R', $fills);
    }

    function TruncateText($text, $maxWidth)
    {
        $text = trim($text);

        if ($this->GetStringWidth($text) <= $maxWidth) {
            return $text;
        }

        $suffix = '...';
        $suffixWidth = $this->GetStringWidth($suffix);

        while ($text != '' && $this->GetStringWidth($text) + $suffixWidth > $maxWidth) {
            $text = substr($text, 0, -1);
        }

        return rtrim($text) . $suffix;
    }
}

?>