TCPDF Cell Position

Hi Experts,

In the following code I am reworking the cell functions just to make it more convenient for my application purposes.

As you will see, I am trying to draw 2 cells one next to the other (success), but the third cell I ma trying to make it double in size and put it on the next line. In theory cell 3 should span across cell 1, 2 and center, but it doesn't.

Any help will be greatly appreciated.

<?php

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

            //Change DIR back
            chdir($currentDIR);
            
//Setup PDF File
            $pdf = new TCPDF('L', 'mm', array(215.9, 279.4));
            $pdf->SetTitle('Manifest');
            $pdf->SetMargins(5, 5);
            $pdf->SetAutoPageBreak(TRUE);
            $pdf->setPrintHeader(FALSE);
            $pdf->SetFont('Helvetica', '', 9);
            $pdf->AddPage();
            

            draw_pdf_cell($pdf, 10, 'cell1', 'R', '1');
            draw_pdf_cell($pdf, 10, 'cell2', 'L', 'R');        
            draw_pdf_cell($pdf, 20, 'cell3', 'C', '1', 1);  
            
    //Generate File
      $file = 'Manifest.pdf';
      $pdf->Output($file);    

function draw_pdf_cell(&$pdf, $w, $txt, $align = 'L', $border = '0', $pos = 0, $h = 3.5){
            
    
//    $pos = 1 ; //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 = 3; /* 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
                    */

    
    $pdf->Cell($w, $h, $txt, $border, $pos, $align, $fill, $link, $stretch, $ignore_min_height, $calign, $valign);
}

Open in new window

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.

APD TorontoSoftware DeveloperAuthor Commented:
I just wanted to add that if on line 80 I add:

$pos = 1; //I get all 3 cells on separate lines, as expected

echo 'pos= ' . $pos; //I get pos= 0pos= 0pos= 1, also as expected

so...why doesn't my cell 3 does not fall under next line?
Ray PaseurCommented:
Is this what you're trying to get?
http://iconoun.com/demo/temp_adp_toronto.php
<?php // demo/temp_adp_toronto.php

/**
 * http://www.experts-exchange.com/questions/28693437/TCPDF-Cell-Position.html?cid=1752
 *
 * Two cells on one line and a third cell on the next line
 *
 * See http://www.tcpdf.org/doc/code/annotated.html
 */
error_reporting(E_ALL);

// A DATE SETTING MAY BE REQUIRED - DEPENDS ON PHP INSTALLATION SETTINGS
date_default_timezone_set('America/Chicago');

// SYNTHESIZE THE PDF FILE URL AND FILE NAME
$pdf_file_link    = 'storage/tcpdf_example.pdf';
$pdf_file_name    = getcwd() . DIRECTORY_SEPARATOR . $pdf_file_link;

// LOAD THE TCPDF CLASS AND CONFIGURATION
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');

// EXTEND THE TCPDF OBJECT SO WE CAN SUBSTITUTE OUR OWN METHODS
class PDF extends TCPDF
{
    // NULLIFY AUTOMATIC FOOTER
    public function Footer() {}

    // ADD CUSTOM HEADER
    public function Header()
    {
        $this->SetFont('helvetica', 'B', 16);
        $this->Cell
        ( 0                // $w width
        , 15               // $h height
        , $this->headerText
        , 0                // border
        , 1                // move cursor to next line after
        , 'L'              // left-align text
        , FALSE            // no fill
        , ''               // link (none)
        , 0                // stretch disabled
        , FALSE            // ignore min height
        , 'T'              // cell align
        , 'C'              // text align
        )
        ;
    }
}

// INSTANTIATE THE OBJECT
$pdf = new PDF('P', 'mm', 'LETTER', true, 'UTF-8', false);

// SET THE TEXT FOR THIS HEADER
$pdf->headerText = 'The ISO-8601 date/time value is: ' . date('c');

// ADD A PAGE
$pdf->AddPage('P', 'LETTER', TRUE);

$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetAutoPageBreak(FALSE);
$pdf->setLanguageArray($l);
$pdf->SetFont('times', '', 13);
$pdf->setCellPaddings(0,0,0,0);
$pdf->setCellMargins(0,0,0,0);

// A CELL BORDER IS NEEDED FOR CLARITY IN THIS DEMO
$bdr = 1;

// FIRST CELL TOP LEFT
$pdf->cell
( 50               // $w width
, 15               // $h height
, 'Cell #1'
, 1                // border
, 0                // do not move cursor to next line after
, 'L'              // left-align text
, FALSE            // no fill
, ''               // link (none)
, 0                // stretch disabled
, FALSE            // ignore min height
, 'T'              // cell align
, 'C'              // text align
)
;

// SECOND CELL TOP RIGHT
$pdf->cell
( 50               // $w width
, 15               // $h height
, 'Cell #2'
, 1                // border
, 0                // do not move cursor to next line after
, 'L'              // left-align text
, FALSE            // no fill
, ''               // link (none)
, 0                // stretch disabled
, FALSE            // ignore min height
, 'T'              // cell align
, 'C'              // text align
)
;

// FORCE NEW LINE
$pdf->ln();

// THIRD CELL CENTERED BELOW CELL #1,2
$pdf->cell
( 100              // $w width
, 15               // $h height
, 'Cell #3'
, 1                // border
, 1                // move cursor to next line after
, 'C'              // center-align text
, FALSE            // no fill
, ''               // link (none)
, 0                // stretch disabled
, FALSE            // ignore min height
, 'T'              // cell align
, 'C'              // text align
)
;

// WRITE THE PDF FILE TO THE SERVER
$pdf->Output($pdf_file_name, 'F');

// PRESENT A CLICKABLE LINK SO WE CAN D/L AND PRINT THE PDF
echo '<a target="my_PDF" href="' . $pdf_file_link . '"><strong>Print the PDF</strong></a>';

Open in new window

APD TorontoSoftware DeveloperAuthor Commented:
Hi Ray,

Your code does produce what I need, but my question is what is wrong with my code above?
Ray PaseurCommented:
Honestly, I don't know what is wrong, and this answer is not intended to be flippant.  I don't look at code that does not work if I can avoid it.  I've written enough unworking code that I'm more interested in getting something to work, and reading unworking code is usually not the fast way to a solution.  When you start with a clean slate you can build the solution one step at a time, with automated testing along the way.  When you start with unworking code, you don't know how to write the automated tests because you don't know what the code should do - you just know that it doesn't work.  So I never read code samples, except to scan for obvious code smells, preferring instead to concentrate on the inputs and desired outputs.  Your description of the expected outputs was clear enough to me, so I just put together a sample that seemed to get a good outcome.
Ray PaseurCommented:
You might want to try adding this line to your script between line 22 and 23, so that you get the cursor to a new line.
$pdf->ln(); // FORCE CURSOR ONTO NEW LINE

Open in new window

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
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.