3

I am trying to create a report using FPDF. bellow code generate a report very well but now I need to repeat the table header section on every page. Also how can I add margin on header and footer. currently only the logo section repeating. How to repeat the full section with company name logo and table header.

Here is my table header section:

    $pdf->SetFillColor(170, 170, 170); //gray
    $pdf->setFont("Arial","B","9");
    $pdf->setXY(10, 40); 
    $pdf->Cell(25, 10, "Payorder Date", 1, 0, "L", 1);  
    $pdf->Cell(35, 10, "Payorder Number", 1, 0, "L", 1);
    $pdf->Cell(50, 10, "Beneficiary Name", 1, 0, "L", 1);
    $pdf->Cell(30, 10, "Contra Date", 1, 0, "L", 1); 
    $pdf->Cell(30, 10, "Amount", 1, 0, "L", 1); 

Here is my full code:

    <?php
$localhost = 'localhost'; //name of server. Usually localhost
$database = 'payorder'; //database name.
$username = 'root'; //database username.
$password = ''; //database password.

// connect to db  
$conn = mysql_connect($localhost, $username, $password) or die('Error connecting to mysql');   
$db = mysql_select_db($database,$conn) or die('Unable to select database!'); 

        require('lib/include/fpdf/fpdf.php');
            class PDF extends FPDF {
                //Page header
                function Header(){
                    //Logo
                    $this->Image('images/logo.jpg',15,8,20);

                }
                //Page footer
                function Footer(){
                    //Position at 1.5 cm from bottom
                    $this->SetY(-15);
                    //$this->SetX(-35);
                    //Arial italic 8
                    $this->SetFont('Arial','I',8);

                    //Page number
                    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
                }
            }

            $pdf = new PDF();
            //$pdf->open();
            $pdf->AddPage("P","A4"); // P =portrait L = Landscape
            $pdf->AliasNbPages();   // necessary for x of y page numbers to appear in document
            $pdf->SetAutoPageBreak(true, 20);

            // document properties
            $pdf->SetAuthor('Sonali Bank Limited');
            $pdf->SetTitle('Daily Transuction Report');

            $pdf->SetFont('Arial','B',14);
            $pdf->Cell(30,10,' Company name Here');

            // Add date report ran
            $pdf->SetFont('Arial','I',10);
            $date =  date("d/m/Y");
            $pdf->Cell(20,20,"Company Subtitle here Dated: ".$date);
            $pdf->SetDrawColor(0, 0, 0); //black

            //table header
            $pdf->SetFillColor(170, 170, 170); //gray
            $pdf->setFont("Arial","B","9");
            $pdf->setXY(10, 40); 
            $pdf->Cell(25, 10, "Payorder Date", 1, 0, "L", 1);  
            $pdf->Cell(35, 10, "Payorder Number", 1, 0, "L", 1);
            $pdf->Cell(50, 10, "Beneficiary Name", 1, 0, "L", 1);
            $pdf->Cell(30, 10, "Contra Date", 1, 0, "L", 1); 
            $pdf->Cell(30, 10, "Amount", 1, 0, "L", 1); 

            $y = 50;
            $x = 10;  
            $pdf->setXY($x, $y);
            $pdf->setFont("Arial","","9");

            //payorder data query
            $sql = "SELECT * FROM payorder_data WHERE po_date ='$report_day'"; 
            $res = mysql_query($sql) or die(mysql_error());
            $row = mysql_num_rows($res);
            while($row = mysql_fetch_array($res)){
                    $pdf->Cell(25, 8, $row['po_date'], 1);  
                    $pdf->Cell(35, 8, $row['po_number'], 1);
                    $pdf->Cell(50, 8, $row['po_ben_name'], 1);
                    $pdf->Cell(30, 8, $row['contra_date'], 1);
                    $pdf->Cell(30, 8, $row['po_amount'], 1);
                    $y += 8;

                    if ($y > 260)    // When you need a page break
                    {
                        $pdf->AddPage();
                        $y = 40;
                    }
                    $pdf->setXY($x, $y);
                }
            $pdf->Output();
            }
Firefog
  • 3,094
  • 7
  • 48
  • 86

1 Answers1

2

you should modify your code on this section

if ($y > 260)    // When you need a page break
   {
     $pdf->AddPage();
     $y = 40;
   }

make something like this

if ($y > 260)    // When you need a page break
   {
      $pdf->AddPage();
      $pdf->SetFillColor(170, 170, 170); //gray
      $pdf->setFont("Arial","B","9");
      $pdf->setXY(10, 40); 
      $pdf->Cell(25, 10, "Payorder Date", 1, 0, "L", 1);  
      $pdf->Cell(35, 10, "Payorder Number", 1, 0, "L", 1);
      $pdf->Cell(50, 10, "Beneficiary Name", 1, 0, "L", 1);
      $pdf->Cell(30, 10, "Contra Date", 1, 0, "L", 1); 
      $pdf->Cell(30, 10, "Amount", 1, 0, "L", 1); 

      $y = 50;
      $x = 10;  
      $pdf->setXY($x, $y);
      $pdf->setFont("Arial","","9");
   }

EDITED :

For setting your margin, you can take a look the documentation at http://www.fpdf.org/en/doc/setmargins.htm

On this documentation, it's not include bottom margin, so for the bottom margin, please visit this post : How to set a bottom margin in FPDF

You also can setting margin like this

$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
Community
  • 1
  • 1
Hilarius L. Doren
  • 757
  • 1
  • 12
  • 29