Using $this when not in object context

Hi Experts,

I'm trying to extend the TCPDF class to create a custom header.  I've also added my own function draw_pdf_cell in order to have my own presets to send to TCPDF's Cell, a function that I need to use within my extended class, and outside it.  However, I keep getting:

Fatal error: Using $this when not in object context in C:\inetpub\wwwroot\CSS_Reservations\dispatch\dispatchPDF.php on line 161

Open in new window


I tried replacing $this-> with self:: , but I get this error elsewhere - even in TCPDF files, files that I have not touched.  What am I doing wrong?

My extended class code:
<?php
//This class creates custom Header for Dispatch

//Connect to TCPDF
    //Temporarily Change directory to TCPDF to load files...
    $currentDIR = getcwd();
    chdir("../tcpdf");
    require_once('examples/tcpdf_include.php');
    require_once('/tcpdf.php');

    //Change DIR back
    chdir($currentDIR);

class DispatchPDF extends TCPDF{
    
    private $date;
    
    public function setDispatchDate($formatted_date_string){
        $this->date = $formatted_date_string;
    }
    
    public function Header() {
        //General Settings & Widths
                
        $width_conf = 24;
        $width_type = 9;
        $width_group = 28;
        $width_room = 12;
        $width_tot = 11;
        $width_pax = 7;
        $width_deposit = 19;
        $width_owing = 16;
        $width_comment = 72;
        $width_attr = 15;
        
        $width_group_time = 17;
        $width_group_desc = 248;     
        
        $fill = FALSE;
        $default_font_family = 'Helvetica';
        $default_font_size = 8;
        
        if ($this->getPage() == 1){
            $this->SetFont($default_font_family, 'B', 16);
            $this->draw_pdf_cell($this, 265, $this->date, '0', FALSE, '0', 10);

            $this->Ln();
        }
        
        //Display column headers
            $this->SetFont($default_font_family, 'B', $default_font_size);

            //Line 1 of 2    
                            
                $this->draw_pdf_cell($width_conf, 'Conf. Number', 'C', TRUE);
                $this->draw_pdf_cell($width_type, 'Trip', 'C', TRUE);
                $this->draw_pdf_cell($width_group, 'Group Name', 'C', TRUE);
                $this->draw_pdf_cell($width_room, 'Room #', 'C', TRUE);
                $this->draw_pdf_cell($width_tot, '# tot', 'C', TRUE);
                $this->draw_pdf_cell($width_pax, '#', 'C', TRUE);
                $this->draw_pdf_cell($width_pax, '#', 'C', TRUE);
                $this->draw_pdf_cell($width_pax, '#', 'C', TRUE);
                $this->draw_pdf_cell($width_pax, '#', 'C', TRUE);
                $this->draw_pdf_cell($width_pax, '#', 'C', TRUE);
                $this->draw_pdf_cell($width_deposit, 'Dep/Pre', 'C', TRUE);
                $this->draw_pdf_cell($width_owing, '$ Owing', 'C', TRUE);
                $this->draw_pdf_cell($width_comment, 'Reservation Comment', 'C', TRUE);
                $this->draw_pdf_cell($width_attr, '# attr incl', 'C', TRUE);
                $this->draw_pdf_cell($width_attr, '# attr sold', 'C', TRUE);
                $this->draw_pdf_cell($width_attr, '# attr sold', 'C', TRUE);
                
                $this->Ln();
                $row_current++;
            
            //Line 2 of 2                    
                $this->draw_pdf_cell($width_conf, '', 'C', TRUE);
                $this->draw_pdf_cell($width_type, 'Type', 'C', TRUE);
                $this->draw_pdf_cell($width_group, '', 'C', TRUE);
                $this->draw_pdf_cell($width_room, '', 'C', TRUE);
                $this->draw_pdf_cell($width_tot, '', 'C', TRUE);
                $this->draw_pdf_cell($width_pax, 'ad.', 'C', TRUE);
                $this->draw_pdf_cell($width_pax, 'sn.', 'C', TRUE);
                $this->draw_pdf_cell($width_pax, 'st.', 'C', TRUE);
                $this->draw_pdf_cell($width_pax, 'ch.', 'C', TRUE);
                $this->draw_pdf_cell($width_pax, 'inf.', 'C', TRUE);
                $this->draw_pdf_cell($width_deposit, '', 'C', TRUE);
                $this->draw_pdf_cell($width_owing, '', 'C', TRUE);
                $this->draw_pdf_cell($width_comment, '', 'C', TRUE);
                $this->draw_pdf_cell($width_attr, 'in price', 'C', TRUE);
                $this->draw_pdf_cell($width_attr, 'by driver', 'C', TRUE);
                $this->draw_pdf_cell($width_attr, 'by other', 'C', TRUE);
        
    }
    
    public function Footer() {

        $this->SetY(-8);
        $page_total = $this->getAliasNbPages();
        $footer = 'Page ' . $page_current . ' of ' . $page_total;
        //$footer = 'row ' . $row_current . ' of ' . $row_max;

        $this->draw_pdf_cell($this, 260, $footer);

        $page_next = TRUE;
            
    }


    public function draw_pdf_cell($w, $txt, $align = 'L', $fill = false, $border = '0', $h = 5){
            

        $pos = 0 ; //documented as $ln, stands for position: 0 = next to (reading order) ; 1 = begiinning of next line ; 2 below
     //   $align = 'L' ; // (L)eft, (C)enter, (R)ight, (J)ustify
        //$fill = FALSE ; //	(boolean) Indicates if the cell background must be painted (true) or transparent (false). 
        $link = '' ; //URL, works with AddLink()
        $ignore_min_height = FALSE ;// if true ignore automatic minimum height value. 

    //    $border = '1' ; 
                    /*Indicates if borders must be drawn around the cell. The value can be a number:

                    0: no border (default)
                    1: frame

                or a string containing some or all of the following characters (in any order):

                    L: left
                    T: top
                    R: right
                    B: bottom

                or an array of line styles for each border group - 
                     * for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 
                     * 'dash' => 0, 'color' => array(0, 0, 0))) 
                     * */



        $stretch = 1; /* 0 = disabled
                       * 1 = horizontal scaling only if text is larger than cell width
                       * 2 = forced horizontal scaling to fit cell width
                       * 3 = character spacing only if text is larger than cell width
                       * 4 = forced character spacing to fit cell width
                       */            

        $calign = 'T'; /* cell vertical alignment relative to the specified Y value. Possible values are:
                        * T : cell top
                        * C : center
                        * B : cell bottom
                        * A : font top
                        * L : font baseline
                        * D : font bottom
                        */

        $valign = 'M';/* text vertical alignment inside the cell. Possible values are:
                        * T : top
                        * C : center
                        * B : bottom
                        */

        //  echo 'pos= ' .$pos;
        $this->SetFillColor(216,216,216);
    
        $this->Cell($w, $h, $txt, $border, $pos, $align, $fill, $link, $stretch, $ignore_min_height, $calign, $valign);
    }
}

Open in new window


Any help will be greatly appreciated.

Thank you,
APD TorontoSoftware DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
Are you sure that the parent class is being imported properly?  In other words, if you save the following as test.php, what's the outcome?
<?php
//This class creates custom Header for Dispatch

//Connect to TCPDF
    //Temporarily Change directory to TCPDF to load files...
    $currentDIR = getcwd();
    chdir("../tcpdf");
    require_once('examples/tcpdf_include.php');
    require_once('/tcpdf.php');

    //Change DIR back
    chdir($currentDIR);

if (class_exists('TCPDF')) {
 echo 'Success';
}
else
{
 echo 'Failure';
}
exit;

Open in new window

APD TorontoSoftware DeveloperAuthor Commented:
Its "Success". I think I maybe using $this incorrectly, or my function definition?
hieloCommented:
FYI: on line 102:
 $this->draw_pdf_cell( $this, 260, $footer);
I expect the first argument to be a number (for the $width), not an object reference.  

>> $row_current++;
>> $footer = 'Page ' . $page_current . ' of ' . $page_total;
You haven't initialized $row_current, $page_current, nor $page_total.  Try declaring at the top of the class and prefix the above variables with $this->

Is the code not "compiling" at all or did you manage to create an instance of the object and it fails at runtime?
What do you have in /tcpdf.php?
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

APD TorontoSoftware DeveloperAuthor Commented:
I'm currently away, but what do you mean by


Is the code not "compiling" at all or did you manage to create an instance of the object and it fails at runtime?
What do you have in /tcpdf.php?

tcpdf.php is a third party library for creating PDFs through php

I'm not the greatest with classes what do you mean by instance?
hieloCommented:
>> I'm currently away, but what do you mean by Is the code not "compiling" at all
I was trying to find out if it errors out before you create an instance of the object.

>> what do you mean by instance?
When you execute:
$page = new DispatchPDF();

then $page is an instance of DispatchPDF().  What I want to know is if you are successfully executing a line similar to the one above.
APD TorontoSoftware DeveloperAuthor Commented:
From your example:
$page = new DispatchPDF(...)
This line is not erroring out.

Here is the whole story...  Below is the function that calls the above code, a function that is a part of another class.

Before, the function below used to work, I had draw_pdf_cell part of the calling class as a private static function, and the first parameter was &$this.  With this setup everything worked, and I was creating my object as $pdf = new TCPDF(...), and at this point DispatchPDF did not exist.

However, I decided to create DispatchPDF extending TCPDF as above because you will see in my Header I need 1 header for page 1 and another for the many pages. When I did a prototype to test the different headers, everything worked fine and dandy, but in my prototype I did not have draw_pdf_cell, so my suspicion is on that function.

Below, once again is my calling function, and under it is my prototype that works. I am also suspecting if I am using static function correctly and the $this variable, and -> vs ::.

My calling function
    public static function generate_pdf(){

    /*****************************
     * CRITERIA is sql
     * SEARCH is selcted options
     ****************************/
    
        $criteria = $_SESSION['criteria'];
        $search = $_SESSION['search'];
        
        $date_obj = new DateTime($search['date']);
        
        //Load Custom PDF
        require_once 'dispatchPDF.php';
            
        //Setup PDF File
        $pdf = new DispatchPDF('L', 'mm', array(215.9, 279.4));
        $pdf->SetTitle($search['date'] . ' Manifest');
        $pdf->SetMargins(5, 5);
        $pdf->SetAutoPageBreak(TRUE);
        $pdf->setPrintHeader(FALSE);
        
        $pdf->setDispatchDate($date_obj->format('F d, Y;'));

        //General Settings & Widths
                
        $width_conf = 24;
        $width_type = 9;
        $width_group = 28;
        $width_room = 12;
        $width_tot = 11;
        $width_pax = 7;
        $width_deposit = 19;
        $width_owing = 16;
        $width_comment = 72;
        $width_attr = 15;
        
        $width_group_time = 17;
        $width_group_desc = 248;     
        
        $fill = FALSE;
        $default_font_family = 'Helvetica';
        $default_font_size = 8;
                
        //Generate Groups
                
        $groups = self::get_groups_for_pdf($criteria);
        $bookings = self::get_bookings_for_pdf($criteria);
        
        $group_current = '';
        $group_index = -1;
        
        $after_header = TRUE;
        
        foreach ($bookings as $book):
            
            //Check if group have changed,  and add group HTML if so
            if ($book['group_id'] != $group_current){
                $group_current = $book['group_id'];
                $group_index++;
            
                $group_time = $groups[$group_index]['pu_time'] . ' - ';
                $group_desc = $groups[$group_index]['pax'] . ' at ' . 
                $groups[$group_index]['pu_loc_name'] . ' by ' . $groups[$group_index]['driver_name'];

                $pdf->SetFont($default_font_family, 'B', 8);
                
                $pdf::draw_pdf_cell($pdf, $width_group_time, $group_time, 'L', TRUE, 'TB', 5);
                $pdf::draw_pdf_cell($pdf, $width_group_desc, $group_desc, 'L', FALSE, '0', 5);
                
                $pdf->Ln();                
                $fill = FALSE; //alt colora
            }
            
            $pdf->SetFont($default_font_family, '', $default_font_size);
                        
            //List body columns
            $conf = $book['user_initials'] . ' ' . $book['id'];
            $type = $book['display_as'];
            $group = $book['group_name'];
            $room = $book['room'];
            $total_pax = $book['total_qty']; //Includes infants
            $ad = $book['adult_qty']; //Includes family x 2
            $sn = $book['senior_qty'];
            $st = $book['student_qty'];
            $ch = $book['child_qty']; //Includes family x 2
            $inf = $book['infant_qty'];
            $deposit = '$' . number_format($book['deposit'], 2);
            $owing = '$' . number_format($book['owing'], 2);
            $trans_status = $book['trans_status'];
            $comment = $book['reserv_comment'];
            
            if ($trans_status != 'Pending'){
                $owing = '$0.00';
            }
            
            if ($type == 'E'){
                $sn = '';
                $st = '';
                $ch = '';
                $inf = '';
                $deposit = '';
                $owing = '';
            }
            
            //Cut everyhting after first carriage return, up to maximum characters        
            $newline_pos = strpos($comment, PHP_EOL);           
            if ($newline_pos !== FALSE){
                $comment = substr($comment, 0, $newline_pos + 1);
            }
            
            $comment = substr($comment, 0, 85);
 
            //Output to PDF
            $pdf::draw_pdf_cell($pdf, $width_conf, $conf, 'L', $fill, 'TBL');
            $pdf::draw_pdf_cell($pdf, $width_type, $type, 'C', $fill, 'TB');
            $pdf::draw_pdf_cell($pdf, $width_group, $group, 'L', $fill, 'TB');
            $pdf::draw_pdf_cell($pdf, $width_room, $room, 'L', $fill, 'TB');
            
            $pdf->SetFont($default_font_family, 'B', $default_font_size);
            $pdf::draw_pdf_cell($pdf, $width_tot, $total_pax, 'C', $fill, 'TB');
            $pdf->SetFont($default_font_family, '', $default_font_size);
            
            $pdf::draw_pdf_cell($pdf, $width_pax, $ad, 'C', $fill, 'TB');
            $pdf::draw_pdf_cell($pdf, $width_pax, $sn, 'C', $fill, 'TB');
            $pdf::draw_pdf_cell($pdf, $width_pax, $st, 'C', $fill, 'TB');
            $pdf::draw_pdf_cell($pdf, $width_pax, $ch, 'C', $fill, 'TB');
            $pdf::draw_pdf_cell($pdf, $width_pax, $inf, 'C', $fill, 'TB');
            
            $pdf::draw_pdf_cell($pdf, $width_deposit, $deposit, 'R', $fill, 'TB');
            
            $pdf->SetFont($default_font_family, 'B', $default_font_size);
            $pdf::draw_pdf_cell($pdf, $width_owing, $owing, 'R', $fill, 'TB');
            $pdf->SetFont($default_font_family, '', $default_font_size);
            
            $pdf::draw_pdf_cell($pdf, $width_comment, $comment, 'L', $fill, 'TB');
            
            $pdf::draw_pdf_cell($pdf, $width_attr, '', 'L', $fill, '1');
            $pdf::draw_pdf_cell($pdf, $width_attr, '', 'L', $fill, '1');
            $pdf::draw_pdf_cell($pdf, $width_attr, '', 'L', $fill, '1');
                        
            $fill = !$fill;
            $pdf->Ln();
            
        endforeach; 

        //$pdf->writeHTML($html);
          //Generate File
            $file = 'Manifest_' . $search['date'] . '.pdf';
            $pdf->Output($file);    
            
            //echo $html;
    }

Open in new window


My working prototype
<?php
//Temporarily Change directory to TCPDF to load files...
    $currentDIR = getcwd();
    chdir("../tcpdf");
    require_once('examples/tcpdf_include.php');
    
    // LOAD THE TCPDF CLASS AND CONFIGURATION
//require_once('/config/lang/eng.php');
require_once('/tcpdf.php');


    //Change DIR back
    chdir($currentDIR);

class MYPDF extends TCPDF {

    private $Datetitle;
    
    public function setDateTitle($yourTitle){
        $this->Datetitle = $yourTitle;
    }


    //Page header
	public function Header() {
		// Title
                $this->Ln();
                
                $page = $this->getPage();
                //echo '<br>page=' . $page;
                if ($page == 1){
                    $this->Cell(0, 15, 'Page 1', 0, false, 'C', 0, '', 0, false, 'M', 'M');
                    $this->Ln();
                } else {
                    $header = $page . ' ' . $this->Datetitle;
                    $this->Cell(0, 15, $header, 0, false, 'C', 0, '', 0, false, 'M', 'M');
                }
	}

	// Page footer
	public function Footer() {
		// Position at 15 mm from bottom
		$this->SetY(-15);
		// Set font
		$this->SetFont('helvetica', 'I', 8);
		// Page number
		$this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
	}
}

    $pdf = new MYPDF('L', 'mm', array(215.9, 279.4));
        $pdf->SetTitle('Manifest');
        
        $pdf->setDateTitle('xyz');
        
        $pdf->SetMargins(5, 5);
        $pdf->SetAutoPageBreak(TRUE);
        $pdf->SetPrintHeader(TRUE);
        $pdf->SetHeaderMargin(10);
            $pdf->SetFont('Helvetica', '', 9);
            $pdf->AddPage();
            $pdf->AddPage();
            $pdf->AddPage();
            
             $file = 'Manifest.pdf';
      $pdf->Output($file); 
?>

Open in new window

hieloCommented:
On line 17 you have:
$pdf = new DispatchPDF('L', 'mm', array(215.9, 279.4));

that makes $pdf an instance of DispatchPDF().  When you have an instance of an object you need to use the arrow notation to invoke non-static methods - ex:
$pdf->draw_pdf_cell(...);

Conversely, you cannot use it to invoke static methods -ex: (the following is incorrect)
$pdf::draw_pdf_cell(...);

As a side note, IF your method were a static method, to invoke it, you would need to prefix it with the class name and two colons to execute/invoke it--ex:
DispatchPDF::draw_pdf_cel(...);

However, since in your current implementation the method is not defined as a static method:
public function draw_pdf_cell(...){...}

then you need to use the arrow notation.

>> Before, the function below used to work, I had draw_pdf_cell part of the calling class as a private static function, and the first parameter was &$this.
OK. A static method is not "tied/associated" to an instance of an object.  So "$this" made sense in your previous implementation if you had to manipulate a specific instance.  However, in your current implementation it is not a static method, so passing $this doesn't make sense.  As a matter of fact, at the end of draw_pdf_cell() you are using the first parameter of draw_pdf_cell() (namely $w) and use it as the first parameter to invoke Cell(). However, Cell() expects the first parameter to be a number.  If you keep calling the method as:
$pdf->draw_pdf_cell( $this, ...);

then you are implicitly calling Cell() with $this, which, again, is not what it expects.  So change:
$pdf::draw_pdf_cel( $this, ...);

to:
$pdf->draw_pdf_cel( ... );

    public static function generate_pdf(){

    /*****************************
     * CRITERIA is sql
     * SEARCH is selcted options
     ****************************/
    
        $criteria = $_SESSION['criteria'];
        $search = $_SESSION['search'];
        
        $date_obj = new DateTime($search['date']);
        
        //Load Custom PDF
        require_once 'dispatchPDF.php';
            
        //Setup PDF File
        $pdf = new DispatchPDF('L', 'mm', array(215.9, 279.4));
        $pdf->SetTitle($search['date'] . ' Manifest');
        $pdf->SetMargins(5, 5);
        $pdf->SetAutoPageBreak(TRUE);
        $pdf->setPrintHeader(FALSE);
        
        $pdf->setDispatchDate($date_obj->format('F d, Y;'));

        //General Settings & Widths
                
        $width_conf = 24;
        $width_type = 9;
        $width_group = 28;
        $width_room = 12;
        $width_tot = 11;
        $width_pax = 7;
        $width_deposit = 19;
        $width_owing = 16;
        $width_comment = 72;
        $width_attr = 15;
        
        $width_group_time = 17;
        $width_group_desc = 248;     
        
        $fill = FALSE;
        $default_font_family = 'Helvetica';
        $default_font_size = 8;
                
        //Generate Groups
                
        $groups = self::get_groups_for_pdf($criteria);
        $bookings = self::get_bookings_for_pdf($criteria);
        
        $group_current = '';
        $group_index = -1;
        
        $after_header = TRUE;
        
        foreach ($bookings as $book):
            
            //Check if group have changed,  and add group HTML if so
            if ($book['group_id'] != $group_current){
                $group_current = $book['group_id'];
                $group_index++;
            
                $group_time = $groups[$group_index]['pu_time'] . ' - ';
                $group_desc = $groups[$group_index]['pax'] . ' at ' . 
                $groups[$group_index]['pu_loc_name'] . ' by ' . $groups[$group_index]['driver_name'];

                $pdf->SetFont($default_font_family, 'B', 8);
                
                $pdf->draw_pdf_cell( $width_group_time, $group_time, 'L', TRUE, 'TB', 5);
                $pdf->draw_pdf_cell( $width_group_desc, $group_desc, 'L', FALSE, '0', 5);
                
                $pdf->Ln();                
                $fill = FALSE; //alt colora
            }
            
            $pdf->SetFont($default_font_family, '', $default_font_size);
                        
            //List body columns
            $conf = $book['user_initials'] . ' ' . $book['id'];
            $type = $book['display_as'];
            $group = $book['group_name'];
            $room = $book['room'];
            $total_pax = $book['total_qty']; //Includes infants
            $ad = $book['adult_qty']; //Includes family x 2
            $sn = $book['senior_qty'];
            $st = $book['student_qty'];
            $ch = $book['child_qty']; //Includes family x 2
            $inf = $book['infant_qty'];
            $deposit = '$' . number_format($book['deposit'], 2);
            $owing = '$' . number_format($book['owing'], 2);
            $trans_status = $book['trans_status'];
            $comment = $book['reserv_comment'];
            
            if ($trans_status != 'Pending'){
                $owing = '$0.00';
            }
            
            if ($type == 'E'){
                $sn = '';
                $st = '';
                $ch = '';
                $inf = '';
                $deposit = '';
                $owing = '';
            }
            
            //Cut everyhting after first carriage return, up to maximum characters        
            $newline_pos = strpos($comment, PHP_EOL);           
            if ($newline_pos !== FALSE){
                $comment = substr($comment, 0, $newline_pos + 1);
            }
            
            $comment = substr($comment, 0, 85);
 
            //Output to PDF
            $pdf->draw_pdf_cell( $width_conf, $conf, 'L', $fill, 'TBL');
            $pdf->draw_pdf_cell( $width_type, $type, 'C', $fill, 'TB');
            $pdf->draw_pdf_cell( $width_group, $group, 'L', $fill, 'TB');
            $pdf->draw_pdf_cell( $width_room, $room, 'L', $fill, 'TB');
            
            $pdf->SetFont($default_font_family, 'B', $default_font_size);
            $pdf->draw_pdf_cell( $width_tot, $total_pax, 'C', $fill, 'TB');
            $pdf->SetFont($default_font_family, '', $default_font_size);
            
            $pdf->draw_pdf_cell( $width_pax, $ad, 'C', $fill, 'TB');
            $pdf->draw_pdf_cell( $width_pax, $sn, 'C', $fill, 'TB');
            $pdf->draw_pdf_cell( $width_pax, $st, 'C', $fill, 'TB');
            $pdf->draw_pdf_cell( $width_pax, $ch, 'C', $fill, 'TB');
            $pdf->draw_pdf_cell( $width_pax, $inf, 'C', $fill, 'TB');
            
            $pdf->draw_pdf_cell( $width_deposit, $deposit, 'R', $fill, 'TB');
            
            $pdf->SetFont($default_font_family, 'B', $default_font_size);
            $pdf->draw_pdf_cell( $width_owing, $owing, 'R', $fill, 'TB');
            $pdf->SetFont($default_font_family, '', $default_font_size);
            
            $pdf->draw_pdf_cell( $width_comment, $comment, 'L', $fill, 'TB');
            
            $pdf->draw_pdf_cell( $width_attr, '', 'L', $fill, '1');
            $pdf->draw_pdf_cell( $width_attr, '', 'L', $fill, '1');
            $pdf->draw_pdf_cell( $width_attr, '', 'L', $fill, '1');
                        
            $fill = !$fill;
            $pdf->Ln();
            
        endforeach; 

        //$pdf->writeHTML($html);
          //Generate File
            $file = 'Manifest_' . $search['date'] . '.pdf';
            $pdf->Output($file);    
            
            //echo $html;
    }    

Open in new window


Also, on your original post...
Line 45:
$this->draw_pdf_cell($this, 265, $this->date, '0', FALSE, '0', 10);

should be:
$this->draw_pdf_cell( 265, $this->date, '0', FALSE, '0', 10);

Line 102:
 $this->draw_pdf_cell($this, 260, $footer);

should be:
$this->draw_pdf_cell( 260, $footer);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
APD TorontoSoftware DeveloperAuthor Commented:
Hielo,

I understood everything above, and now I'm error-free, but in my PDF I only see my Header and Footer (ie- what I set in DispatchPDF), but I don't see anything what I set from my caller function.  I'm attaching my resulting PDF.

Below is what my calling function looks like now.  As you'll see I commented out Lines 74-141, just to focus on the first 2 cells on Lines 67-68.

You'll also see that I added  Li. 70, and I did get on screen data.  So, now the question is, why does draw_pdf_cell (...) now works within DispatchPDF, but not outside it even though I think I'm calling it the same way?

    public static function generate_pdf(){

    /*****************************
     * CRITERIA is sql
     * SEARCH is selcted options
     ****************************/
    
        $criteria = $_SESSION['criteria'];
        $search = $_SESSION['search'];
        
        $date_obj = new DateTime($search['date']);
        
        //Load Custom PDF
        require_once 'dispatchPDF.php';
            
        //Setup PDF File
        $pdf = new DispatchPDF('L', 'mm', array(215.9, 279.4));
        $pdf->SetTitle($search['date'] . ' Manifest');
        $pdf->SetMargins(5, 5);
        $pdf->SetAutoPageBreak(TRUE);
        $pdf->setPrintHeader(TRUE);
        
        $pdf->setDispatchDate($date_obj->format('F d, Y'));

        //General Settings & Widths
                
        $width_conf = 24;
        $width_type = 9;
        $width_group = 28;
        $width_room = 12;
        $width_tot = 11;
        $width_pax = 7;
        $width_deposit = 19;
        $width_owing = 16;
        $width_comment = 72;
        $width_attr = 15;
        
        $width_group_time = 17;
        $width_group_desc = 248;     
        
        $fill = FALSE;
        $default_font_family = 'Helvetica';
        $default_font_size = 8;
                
        //Generate Groups
                
        $groups = self::get_groups_for_pdf($criteria);
        $bookings = self::get_bookings_for_pdf($criteria);
        
        $group_current = '';
        $group_index = -1;
        
        
        foreach ($bookings as $book):
            
            //Check if group have changed,  and add group HTML if so
            if ($book['group_id'] != $group_current){
                $group_current = $book['group_id'];
                $group_index++;
            
                $group_time = $groups[$group_index]['pu_time'] . ' - ';
                $group_desc = $groups[$group_index]['pax'] . ' at ' . 
                $groups[$group_index]['pu_loc_name'] . ' by ' . $groups[$group_index]['driver_name'];

                $pdf->SetFont($default_font_family, 'B', 16);
                
                $pdf->draw_pdf_cell($width_group_time, $group_time, 'L', TRUE, 'TB', 10);
                $pdf->draw_pdf_cell($width_group_desc, $group_desc, 'L', FALSE, '0', 10);
                
               // echo $group_desc . '<br>';
                $pdf->Ln();                
                $fill = FALSE; //alt colors
            }
            /*
            $pdf->SetFont($default_font_family, '', $default_font_size);
                        
            //List body columns
            $conf = $book['user_initials'] . ' ' . $book['id'];
            $type = $book['display_as'];
            $group = $book['group_name'];
            $room = $book['room'];
            $total_pax = $book['total_qty']; //Includes infants
            $ad = $book['adult_qty']; //Includes family x 2
            $sn = $book['senior_qty'];
            $st = $book['student_qty'];
            $ch = $book['child_qty']; //Includes family x 2
            $inf = $book['infant_qty'];
            $deposit = '$' . number_format($book['deposit'], 2);
            $owing = '$' . number_format($book['owing'], 2);
            $trans_status = $book['trans_status'];
            $comment = $book['reserv_comment'];
            
            if ($trans_status != 'Pending'){
                $owing = '$0.00';
            }
            
            if ($type == 'E'){
                $sn = '';
                $st = '';
                $ch = '';
                $inf = '';
                $deposit = '';
                $owing = '';
            }
            
            //Cut everyhting after first carriage return, up to maximum characters        
            $newline_pos = strpos($comment, PHP_EOL);           
            if ($newline_pos !== FALSE){
                $comment = substr($comment, 0, $newline_pos + 1);
            }
            
            $comment = substr($comment, 0, 85);
 
            //Output to PDF
            $pdf->draw_pdf_cell($width_conf, $conf, 'L', $fill, 'TBL');
            $pdf->draw_pdf_cell($width_type, $type, 'C', $fill, 'TB');
            $pdf->draw_pdf_cell($width_group, $group, 'L', $fill, 'TB');
            $pdf->draw_pdf_cell($width_room, $room, 'L', $fill, 'TB');
            
            $pdf->SetFont($default_font_family, 'B', $default_font_size);
            $pdf->draw_pdf_cell($width_tot, $total_pax, 'C', $fill, 'TB');
            $pdf->SetFont($default_font_family, '', $default_font_size);
            
            $pdf->draw_pdf_cell($width_pax, $ad, 'C', $fill, 'TB');
            $pdf->draw_pdf_cell($width_pax, $sn, 'C', $fill, 'TB');
            $pdf->draw_pdf_cell($width_pax, $st, 'C', $fill, 'TB');
            $pdf->draw_pdf_cell($width_pax, $ch, 'C', $fill, 'TB');
            $pdf->draw_pdf_cell($width_pax, $inf, 'C', $fill, 'TB');
            
            $pdf->draw_pdf_cell($width_deposit, $deposit, 'R', $fill, 'TB');
            
            $pdf->SetFont($default_font_family, 'B', $default_font_size);
            $pdf->draw_pdf_cell($width_owing, $owing, 'R', $fill, 'TB');
            $pdf->SetFont($default_font_family, '', $default_font_size);
            
            $pdf->draw_pdf_cell($width_comment, $comment, 'L', $fill, 'TB');
            
            $pdf->draw_pdf_cell($width_attr, '', 'L', $fill, '1');
            $pdf->draw_pdf_cell($width_attr, '', 'L', $fill, '1');
            $pdf->draw_pdf_cell($width_attr, '', 'L', $fill, '1');
              */          
            $fill = !$fill;
            $pdf->Ln();
            
        endforeach; 

        //$pdf->writeHTML($html);
          //Generate File
            $file = 'Manifest_' . $search['date'] . '.pdf';
            $pdf->Output($file);    
            
            //echo $html;
    }

Open in new window

Manifest-2014-07-06-7.pdf
APD TorontoSoftware DeveloperAuthor Commented:
Please ignore my last post.  In order to start outputing data with TCPDF. You must call AddPage at least once
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.