Hi
I need to generate six tables one by one but i am not able to generate with proper alignment its running out of page,I also checked pageheight,but its not working,My code is as follows and its very urgent some expert please hep me out in this issue.Thanks in advance

function BuildTable2($header2,$data2) 
            {     
            $this->SetFillColor(255,255,255);
            $this->SetTextColor(0);
            $this->SetDrawColor(0);
            $this->SetLineWidth(.3);
            $this->SetFont('','B');
//Header
            $w=array(20,20,40,18,50,12);
            $this->SetFont('Arial','B',7);
            for($i=0;$i<count($header2);$i++)
                $this->Cell($w[$i],7,$header2[$i],1,0,'C',true);
                $this->Ln();
//Color and font restoration
                $this->SetFillColor(224,235,255);
                $this->SetTextColor(0);
                $this->SetFont('');
//Data
                $fill=false;
                $i = 0;
                $x0=$x = $this->GetX();
                $y = $this->GetY();
                foreach($data2 as $row)
                {
                for ($i=0; $i<6; $i++) //Avoid very lengthy texts
                {
                    $row[$i]=substr($row[$i],0,160);
                }
                $yH=9; //height of the row
//Issue a page break first if needed
                $this->SetXY($x, $y);
                $this->Cell($w[0], $yH, "", 'LRB',0,'',$fill);
                $this->SetXY($x, $y);
                $this->MultiCell($w[0],6,$row[0],0,'L');

                $this->SetXY($x + $w[0], $y);
                $this->Cell($w[1], $yH, "", 'LRB',0,'',$fill);
                $this->SetXY($x + $w[0], $y);
                $this->MultiCell($w[1],4,$row[1],0,'L');

                $x =$x+$w[0];
                $this->SetXY($x + $w[1], $y);
                $this->Cell($w[2], $yH, "", 'LRB',0,'',$fill);
                $this->SetXY($x + $w[1], $y);
                $this->MultiCell($w[2],5,$row[2],0,'L');

                $x =$x+$w[1];
                $this->SetXY($x + $w[2], $y);
                $this->Cell($w[3], $yH, "", 'LRB',0,'',$fill);
                $this->SetXY($x + $w[2], $y);
                $this->MultiCell($w[3],6,$row[3],0,'L');

                $x =$x+$w[2];
                $this->SetXY($x + $w[3], $y);
                $this->Cell($w[4], $yH, "", 'LRB',0,'',$fill);
                $this->SetXY($x + $w[3], $y);
                $this->MultiCell($w[4],6,$row[4],0,'L');

                $x =$x+$w[3];
                $this->SetXY($x + $w[4], $y);
                $this->Cell($w[5], $yH, "", 'LRB',0,'',$fill);
                $this->SetXY($x + $w[4], $y);
                $this->MultiCell($w[5],6,$row[5],0,'L');

                $y=$y+$yH; //move to next row
                $x=$x0; //start from firt column
                }
            }



            $pdf=new PDF();
        $pdf->Addpage();
        $page_height = 300;
        $bottom_margin = 20;
        $yH=12;
        $pdf->SetAutoPageBreak(auto,false);


        //Second Table
$j=0;
$i=1;
if (count($data2)!=0)
{
    foreach ($data2 as $row)
    {
        $pagedata[$j]=$row;
        if ((count($data2)<=20 && $i== count($data2)))
        {
            $x=$pdf->GetX();
            $y=$pdf->GetY();
            $pdf->SetXY($x,$y+10);
            $pdf->SetFont('Arial','B',7);
            $pdf->cell(10,10,"Research Scholars(C - Current, P - Graduated)",0,0);
            $pdf->SetXY($x,$y+20);

            $space_left = $page_height - $pdf->GetY(); // space left on page
            $space_left -= $bottom_margin; // less the bottom margin
            if ($yH >= $space_left) 
            {
                $pdf->Ln(5);                        
                $pdf->AddPage(); // page break
            }   
                $pdf->BuildTable2($header2,$pagedata);  
                $j=0;
                $pagedata="";
        }
        if (($i%20)==0)
        {
            $pdf->AddPage();
            if($i==20)
            {
                $x1=$pdf->GetX();
                $y1=$pdf->GetY();
                $pdf->SetXY($x1,$y1+10);
                $pdf->SetFont('Arial','B',7);
                $pdf->cell(10,10,"Research Scholars(C - Current, P - Graduated)",0,0);
                $x2=$pdf->GetX();
                $y2=$pdf->GetY();
                $pdf->SetXY($x2,$y2+10);
                $space_left = $page_height - $pdf->GetY(); // space left on page
                $space_left -= $bottom_margin; // less the bottom margin
                if ($yH >= $space_left) 
                {
                $pdf->Ln(5);                        
                $pdf->AddPage(); // page break
                }   
                $pdf->BuildTable3($header3,$pagedata);
                $j=0;
                $pagedata="";
            }
        }
        if((count($data2)>20) && $i==count($data2) && ($i%20) !=0)
        {
            $pdf->AddPage();
            $pdf->BuildTable2($header2,$pagedata);
            $j=0;
            $pagedata="";
        }   
        $i++;
        $j++;
    }
}

I need to generate six tables one by one but i am not able to generate with proper alignment its running out of page

Hi, can you provide some sample data, for example how the array in $data2 is structured, and a screenshot or a generated PDF could be useful for us to understand the issue. Also, with the PDF class are you extending the FPDF library, correct? Can you show the full code?

I did a test and I can display a table of 19 rows, by using something like this:

$data = range('a','f');

for($z = 0; $z < 19; $z++)
{
    $data2[] = $data;
}

$header2 = range('A','F');

After that your script requires the BuildTable3() method, which is missing from the above.

Hi,
For table3 i am using same code as buildtable2,I just put one example for you to understand,I am unable to put screenshot in this,how to add screenshot in this....
For example i am displaying three lines before the first table and the first table heading followed by first table .If first table contains 17 rows then two or three spaces then i start of the second table by getting getX and GetY values then i calculate spaceleft if the second table contains five records means then no problem if it contains more row means its running out of page that is each line comes in one one page and after 42 nd page the third table is printing.its very urgent i dont know what to do please try to help me as early as possible or else send your mailid i'll send you the screen shot of the pdf

Thanks in advance

In practice you're checking for widows: if at the end you have only five (or less) rows then you add them to the previous page, if you have more, then you create a new one. Correct?

If yes, then this example should work:

<?php

require_once './pdf.class.php';

# basic settings
$pdf = new PDF();
$pdf->Addpage();
$pdf->SetAutoPageBreak('auto',false);

# generating some random data
# REPLACE these two lines with your $data2 and $header2
for($z = 0; $z < 45; $z++) { $data2[] = array_merge(array($z), range(10, 15)); }
$header2 = array_merge(array('#'), range('A','E'));

# split $data2 in arrays of 20 rows each
$chunked = array_chunk($data2, 20);
$count   = count($chunked);
$i       = 1;

# get the last array
$lastrows   = end($chunked);
$lastcount  = count($lastrows);

if($count > 0)
{
    $state = false;

    foreach($chunked as $rows)
    {
        # avoid to add the last array two times:
        # once merged and once alone in a new page
        if($state === true) break;

        # check last array before looping it
        if($count == $i + 1 && $lastcount <= 5)
        {
            $rows = array_merge($rows, $lastrows);
            $state = true;
        }

        # do not append a new page at the end of this loop
        if($count == $i) $state = true;

        # generate table
        $x = $pdf->GetX();
        $y = $pdf->GetY();
        $pdf->SetXY($x,$y+10);
        $pdf->SetFont('Arial','B',7);
        $pdf->cell(10,10,"Research Scholars(C - Current, P - Graduated)",0,0);
        $pdf->SetXY($x,$y+20);
        $pdf->BuildTable2($header2,$rows);

        # add pages
        if($state === false) $pdf->AddPage();

        $i++;
    }
}

$pdf->Output();

Regarding the code inside:

require_once './pdf.class.php';

Check this link: http://pastebin.com/nKRx4Ryt in practice is part of your code, without changes, you can replace it with you library.

Regarding:

I am unable to put screenshot in this,how to add screenshot in this...

You can upload a file / screenshot by clicking on the fifth icon from the right side of your textarea, check this screenshot:

Otherwise, if you have problems use dropbox, imgur, imageshack or a service of this kind to share an example. Hope it helps, bye!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.