Using FPDF to add header and footer on every page in a document (php)

admin 0

create index.php and paste the following code:

add fpdf libraries

<?php

require(‘fpdf.php’);

ob_start();

session_start();

include (“../_init.php”);

$invoice_id = $request->get[‘invoice_id’];

include(‘../_inc/template/partials/pdf_view_new.php’);

//GENERAL

$customer_name=$data[‘customer_name’] ;

$date=$data[‘date’] ;

$bill_to=$data[‘bill_to’] ;

$customer_email=$data[‘customer_email’] ;

$invoice_note=$data[‘invoice_note’] ;

$items =$data[‘items’] ;

//TOTALS

$subtotal=$data[‘subtotal’] ;

$order_tax=$data[‘order_tax’] ;

$payable_amount=$data[‘payable_amount’] ;

//BARCODE

$barcode=$data[‘barcode’] ;

//print_r ($items);

//print json_encode($items);    

//INCLUDED ABOVEEEEEEEEEEEEEEEEEE

/*foreach($data[‘items’] as $result) {

    echo $result[‘p_image’], ‘<br>’;

}*/

              class PeoplePDF extends FPDF {

                             //Page header

                             function Header(){

                              global $invoice_id;

                              global $date;

                              global $bill_to;

                             global $customer_email;

                              global $customer_name;

                              global $invoice_note;   

                              global $items;  

                              global $barcode;           

                                           $logo=”logo.jpg”;

                                           $this->SetFont(‘times’, ”, 10);

                                           $this->Image($logo,80,15,60,20);

                                           //$this->Image($barcode,160,10,0,0);

                                           //$this->Text(161, 25, $hcode);

                                           $this->Text(15, 17, ‘One Two One Kenya Limited’);

                                           $this->Text(15, 22,’TOD K&I Place, Opp Total SS’);

                                           $this->Text(15, 27,’Along Langata Road, Karen’);

                                           $this->Text(15, 32,’P.O BOX 75267-00200′);

                                           $this->Text(15, 37,’Email:info@one2onekltd.com’);

                                           $this->Text(15, 42,’Nairobi, Kenya’);

                                           $this->SetFont(‘arial’, ‘B’, 22);

                                           $this->Text(170, 20,’INVOICE’ );

                                           $this->SetFont(‘times’, ”, 9);

                                           $this->Rect(150, 30, 50, 10);// the rectangle left,top,width, height

                                           $this->Line(150,35, 200, 35).//underline the student names

                                           $this->Text(152, 34,’Date’);

                                           $this->Text(152, 39,$date);

                                           $this->Text(180, 34,’Invoice#’);  //NO ENTRY POINT

                                           $this->Text(180, 39,$invoice_id);

                                           $this->Line(172, 30, 172, 40).//create next collumn

                                           $this->Rect(15, 45, 80, 40);// the rectangle left,top,width, height

                                           $this->Line(15,55, 95, 55).//underline the student names

                                           $this->Text(17, 50,’Bill to’);

                                           $this->Text(17, 63, $bill_to);

                                           $this->Text(17, 68, $customer_email);

                                           $this->Text(17, 73,’Kenya’);

                                           $this->Rect(110, 45, 90, 25);// the rectangle left,top,width, height

                                           $this->Line(110,55, 200, 55).//underline the student names

                                           $this->Text(112, 50,’Ship to’);

                                           $this->Text(112, 62,$customer_name);

                                           $this->Rect(110, 75, 90, 10);// the rectangle left,top,width, height

                                           //$this->Line(110,75, 200, 75).//underline the student names

                                           $this->Text(112, 81,’PIN: P051344613Q’);

                                           $this->SetXY(15,90);

                             $this->Cell(40, 5, “P.O. Number”, 1, 0, “L”, 0);

                             $this->Cell(30, 5, “Terms”, 1, 0, “C”, 0);

                             $this->Cell(30, 5, “Rep”, 1, 0, “C”, 0);

                             $this->Cell(40, 5, “Ship”, 1, 0, “C”, 0);

                             $this->Cell(20, 5, “Via”, 1, 0, “C”, 0);

                             $this->Cell(25, 5, “FOB”, 1, 0, “C”, 0);

                             $this->SetXY(15,95);

                             $this->Cell(40, 5, $invoice_note, 1, 0, “L”, 0);

                             $this->Cell(30, 5, “”, 1, 0, “C”, 0);

                             $this->Cell(30, 5, “”, 1, 0, “C”, 0);

                             $this->Cell(40, 5, $date, 1, 0, “C”, 0);

                             $this->Cell(20, 5, “”, 1, 0, “C”, 0);

                             $this->Cell(25, 5, “”, 1, 0, “C”, 0);

                                           $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’);

                             }

              }

              $pdf = new FPDF();

              $pdf = new PeoplePDF();

              $pdf->AliasNbPages();//for page numbers

              //$pdf->open();

              $pdf->addPage();

              $pdf->SetAutoPageBreak(false);

              $pdf->SetFillColor(224,224,224); //black

              $pdf->SetDrawColor(0, 0, 0); //black

              $pdf->SetFillColor(224,224,224); //gray

              $pdf->setFont(“times”,”B”,”9″);

              $pdf->setXY(15, 110);

              $pdf->Cell(20, 5, “Quantity”, 1, 0, “L”, 0);

              $pdf->Cell(50, 5, “Item Code”, 1, 0, “C”, 0);

              $pdf->Cell(50, 5, “Description”, 1, 0, “C”, 0);

                             $pdf->Cell(30, 5, “MPN”, 1, 0, “C”, 0);

              $pdf->Cell(20, 5, “Price Each”, 1, 0, “C”, 0);

              $pdf->Cell(15, 5, “Amount”, 1, 0, “C”, 0);

               $pdf->Ln();

               $y = $pdf->GetY();

              $x = 15;

              $pdf->setXY($x, $y);

              //DISPLAY HEREEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE LOOOPPPPED  DISPLAY THE TABLE HEREEEE

              $fill=0;

                             $pdf->setFont(“times”,””,”9″);

               $cellWidth=50;//wrapped cell width

              $cellHeight=7;//normal one-line cell height        

              $cellWidth2=50;//wrapped cell width

              $cellHeight2=7;//normal one-line cell height

$newdata =  array (

      ‘item_quantity’ => ”,

      ‘item_name’ => ”,

      ‘description’ => ”,

      ‘item_price’ => ”,

      ‘p_code’ => ”,

      ‘item_total’ => ”

    );

              array_push($data[“items”],$newdata);

    foreach($data[‘items’] as $result) {

              //check whether the text is overflowing

              if($pdf->GetStringWidth($result[‘item_name’])+2 < $cellWidth){

                             //if not, then do nothing

                             $line=1;

              }else{

                             //if it is, then calculate the height needed for wrapped cell

                             //by splitting the text to fit the cell width

                             //then count how many lines are needed for the text to fit the cell

                             $textLength=strlen($result[‘item_name’]);          //total text length

                             $errMargin=$cellHeight;                            //cell width error margin, just in case

                             $startChar=0;                  //character start position for each line

                             $textArray=array();       

                             $maxChar=0;                                 //maximum character in a line, to be incremented later

                             //to hold the strings for each line

                             $tmpString=””;                //to hold the string for a line (temporary)

                             while($startChar < $textLength){ //loop until end of text

                                           //loop until maximum character reached

                                           while(

                                           $pdf->GetStringWidth( $tmpString ) < ($cellWidth-$errMargin) &&

                                           ($startChar+$maxChar) < $textLength ) {

                                                          $maxChar++;

                                                         $tmpString=substr($result[‘item_name’],$startChar,$maxChar);

                                           }

                                           //move startChar to next line

                                           $startChar=$startChar+$maxChar;

                                           //then add it into the array so we know how many line are needed

                                           array_push($textArray,$tmpString);

                                           //reset maxChar and tmpString

                                           $maxChar=0;

                                           $tmpString=”;

                             }

                             //get number of line

                             $line=count($textArray);

              }

                             //check whether the text is overflowing

              if($pdf->GetStringWidth($result[‘description’])+2 < $cellWidth2){

                             //if not, then do nothing

                             $line2=1;

              }else{

                             //if it is, then calculate the height needed for wrapped cell

                             //by splitting the text to fit the cell width

                             //then count how many lines are needed for the text to fit the cell

                             $textLength2=strlen($result[‘description’]);        //total text length

                             $errMargin2=$cellHeight2;                        //cell width error margin, just in case

                             $startChar2=0;                //character start position for each line

                             $maxChar2=0;                               //maximum character in a line, to be incremented later

                             $textArray2=array();      //to hold the strings for each line

                             $tmpString2=””;                            //to hold the string for a line (temporary)

                             while($startChar2 < $textLength2){ //loop until end of text

                                           //loop until maximum character reached

                                           while(

                                           $pdf->GetStringWidth( $tmpString2 ) < ($cellWidth2-$errMargin2) &&

                                           ($startChar2+$maxChar2) < $textLength2 ) {

                                                          $maxChar2++;

                                                         $tmpString2=substr($result[‘description’],$startChar2,$maxChar2);

                                           }

                                           //move startChar to next line

                                           $startChar2=$startChar2+$maxChar2;

                                           //then add it into the array so we know how many line are needed

                                           array_push($textArray2,$tmpString2);

                                           //reset maxChar and tmpString

                                           $maxChar2=0;

                                           $tmpString2=”;

                             }

                             //get number of line

                             $line2=count($textArray2);

              }

        //this is the data to be displayed

        $item_quantity=$result[‘item_quantity’];

        $item_name=$result[‘item_name’];

        $description=$result[‘description’];

        $item_price=$result[‘item_price’];

        $p_code=$result[‘p_code’];

        $item_total=$result[‘item_total’];

        //write the cells

              $pdf->Cell(20,($line * $cellHeight),$item_quantity,1,0,”L”, $fill); //adapt height to number of lines

              //use MultiCell instead of Cell

              //but first, because MultiCell is always treated as line ending, we need to

              //manually set the xy position for the next cell to be next to it.

              //remember the x and y position before writing the multicell

              $xPos=$pdf->GetX();

              $yPos=$pdf->GetY();

              $pdf->MultiCell($cellWidth,$cellHeight,$item_name,1,”L”, $fill);

              //return the position for next cell next to the multicell

              //and offset the x with multicell width

              $pdf->SetXY($xPos + $cellWidth , $yPos);

              $pdf->MultiCell($cellWidth, $cellHeight,$description,1,”L”, $fill);

              //$pdf->Cell(60,($line * $cellHeight),$description,1,0,”L”, $fill);; //adapt height to number of lines

              $pdf->SetXY($xPos + $cellWidth+50 , $yPos);

              $pdf->Cell(30,($line * $cellHeight),$p_code,1,1,”L”, $fill); //adapt height to number of lines

              $pdf->SetXY($xPos + $cellWidth+80 , $yPos);

              $pdf->Cell(20,($line * $cellHeight),$item_price,1,0,”R”, $fill); //adapt height to number of lines

              $pdf->Cell(15,($line * $cellHeight),$item_total,1,0,”R”, $fill); //adapt height to number of lines

              $pdf->Ln();

              $yPos += $line * $cellHeight;

              $fill=!$fill;

              if($pdf->GetY()>260){

              $pdf->AddPage();

              $pdf->SetFillColor(224,224,224); //gray

              $pdf->setFont(“times”,”B”,”9″);

              $pdf->setXY(15, 110);

              $pdf->Cell(20, 5, “Quantity”, 1, 0, “L”, 0);

              $pdf->Cell(50, 5, “Item Code”, 1, 0, “C”, 0);

              $pdf->Cell(50, 5, “Description”, 1, 0, “C”, 0);

              $pdf->Cell(30, 5, “MPN”, 1, 0, “C”, 0);

              $pdf->Cell(20, 5, “Price Each”, 1, 0, “C”, 0);

              $pdf->Cell(15, 5, “Amount”, 1, 0, “C”, 0);

              $pdf->Ln();

              $pdf->setFont(“times”,””,”9″);

                                           $y = 115;

                             }

                                           $pdf->setX(15);

    }//end of foreach

    while($pdf->GetY()<255){

        $pdf->setXY(15, $pdf->GetY());

              $pdf->Cell(20, 7, “”, 1, 0, “L”, $fill);

              $pdf->Cell(50, 7, “”, 1, 0, “C”, $fill);

              $pdf->Cell(50, 7, “”, 1, 0, “C”, $fill);

              $pdf->Cell(30, 7, “”, 1, 0, “C”, $fill);

              $pdf->Cell(20, 7, “”, 1, 0, “C”, $fill);

              $pdf->Cell(15, 7, “”, 1, 0, “C”, $fill);

                             $pdf->Ln();

                             $fill=!$fill;

    }

    //yTOTALS

              if($y>260){

              $pdf->AddPage();

              $pdf->SetFillColor(128,128,128); //gray

              $pdf->setFont(“times”,”B”,”9″);

              $pdf->setXY(15, 300-30);

              $pdf->Line($x,300-40, 200, 300-40).//underline the student names

              $pdf->setXY(20,300-35);

              $pdf->Cell(50, 10, “Sub total: KES $subtotal”, 1, 0, “C”, 0);

              $pdf->setX(75);

              $pdf->Cell(50, 10, “Sales Tax(16.0%): KES $order_tax”, 1, 0, “C”, 0);

              $pdf->setX(130);

              $pdf->Cell(50, 10, “Total: KES $payable_amount”, 1, 0, “C”, 0);

              $pdf->setY(300-20);

              $pdf->setFont(“times”,””,”9″);

              $pdf->Text(90, $pdf->GetY(),’Thank you for choosing us!’);

              }else{

              $pdf->setFont(“times”,”B”,”9″);

              $pdf->setXY(15, 300-30);

              $pdf->Line($x,300-40, 200, 300-40).//underline the student names

              $pdf->setXY(20,300-35);

              $pdf->Cell(50, 10, “Sub total: KES $subtotal”, 1, 0, “C”, 0);

              $pdf->setX(75);

              $pdf->Cell(50, 10, “Sales Tax(16.0%): KES $order_tax”, 1, 0, “C”, 0);

              $pdf->setX(130);

              $pdf->Cell(50, 10, “Total: KES $payable_amount”, 1, 0, “C”, 0);

              $pdf->setY(300-20);

              $pdf->setFont(“times”,””,”9″);

              $pdf->Text(90, $pdf->GetY(),’Thank you for choosing us!’);

              //$pdf->setY(300-17);

              //$logo=”logo.jpg”;

              //$pdf->Image($barcode,90);

              }

              $pdf->Output();

?>

for full source code email citycloudtech@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *