Project

General

Profile

Pagebreak only works for first page with Table

Added by Dario Zadro about 5 years ago

I'm creating a header using:

//Build the address section within the header
private function build_address()
{
//Settings
$address_area_min_height = 20;
$line_height = 5;

    //Reset the position to top
    $this->SetX(0);
    $this->SetY(10);

    //Set font
    $this->SetFont($this->font, '', 9);
    $c = $this->grey_color;
    $this->SetTextColor($c['r'], $c['g'], $c['b']);

    //Keep track of address height
    $address_height = 0;

    //Loop each line in address
    foreach ($this->config['pdf_header_address'] as $line) {
        //Add line
        $this->Cell(0, $line_height, $line, 0, 2, 'R');
        //Count height
        $address_height += $line_height;
    }

    //Set the size of the address area to make sure sub header is in position
    if ($address_height < $address_area_min_height) {
        //Get difference
        $blank_cell_height = $address_area_min_height - $address_height;
        //Add blank cell to make up height
        $this->Cell(0, $blank_cell_height, '', 0, 2, 'R');
    }
}

Footer:

// Page footer
function Footer(){
    //Set text color
    $c = $this->grey_color;
    $this->SetTextColor($c['r'], $c['g'], $c['b']);

    // Position at 1.5 cm from bottom
    $this->SetY(-15);
    $this->SetFont($this->font,'',10);
    $this->Cell(0,10,$this->config['pdf_footer_text'], 0, 0, 'L');
    $this->SetFont($this->font,'',8);

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

Then just outputing a random table:

$linkTable = new Interpid\PdfLib\Table($this->fpdf);

        //$linkTable->setStyle( "p", $this->fpdf->getDefaultFontName(), "", 7, "130,0,30" );
        //$linkTable->setStyle( "b", $this->fpdf->getDefaultFontName(), "B", 7, "130,0,30" );

        $linkTable->initialize($col_widths);

        $header = array(
            [ 'TEXT' => 'Header #1' ],
            [ 'TEXT' => 'Header #2' ],
            [ 'TEXT' => 'Header #3' ]
        );

        //add the header row
        $linkTable->addHeader( $header );

        //add some table rows
        for ($j=1; $j<105; $j++)
        {
            $aRow = Array();
            $aRow[0]['TEXT'] = "Line $j Text 1";    //text for column 0
            $aRow[1]['TEXT'] = "Line $j Text 2";    //text for column 1
            $aRow[2]['TEXT'] = "Line $j Text 3";    //text for column 2

            //override some settings for row 2
            if (2 == $j){
                $aRow[1]['TEXT_ALIGN'] = 'L';
                $aRow[1]['TEXT'] = "<p>This is a <b>Multicell</b></p>";
            }
            //add the row
            $linkTable->addRow($aRow);
        }

        $linkTable->close();

Header, footer, and table are OK on first page. Every page after, the table is over the footer.

Can you provide some suggestions for fix? Thank you!


Replies (7)

RE: Pagebreak only works for first page with Table - Added by Andrei Bintintan about 5 years ago

Hi,

Make sure of the following:

  • the top and bottom margins are the same height as your header/footer functions
  • save and restore the X and Y when you enter and leave the header function.

The main problem is the Y (starting height of the table) on a new page, that is determined by the header and normally should be the top margin. If this Y is modified trough the Header function then the table is shifted.

Give it a try.
Andrei

RE: Pagebreak only works for first page with Table - Added by Dario Zadro about 5 years ago

Thanks for your reply. Where or how do I set top/bottom margins for the table? The top margin is fine for each page, it's the bottom that is being drawn over the footer.

RE: Pagebreak only works for first page with Table - Added by Andrei Bintintan about 5 years ago

Dario Zadro wrote:

Thanks for your reply. Where or how do I set top/bottom margins for the table? The top margin is fine for each page, it's the bottom that is being drawn over the footer.

The top and bottom margins of the page are determining the height of the table.

Your top margin is incorrect compared to the header height. Those should match in size.

RE: Pagebreak only works for first page with Table - Added by Dario Zadro about 5 years ago

Ok, so what do I need to do to fix it? Can you provide specific details?

RE: Pagebreak only works for first page with Table - Added by Andrei Bintintan about 5 years ago

In your case it might be only the header that messes up the things.

//assuming your header height is 50, left and right 20
$pdf->SetMargins(20, 50, 20);

RE: Pagebreak only works for first page with Table - Added by Dario Zadro about 5 years ago

The header is ok, it's the footer that's getting written over. I'll try some things and let you know. If you know a way to set a bottom margin, please provide. Thanks again.

RE: Pagebreak only works for first page with Table - Added by Andrei Bintintan about 5 years ago

If you don't manage, please send me a full working example per email and I'll fix it. >> andy@interpid.eu

    (1-7/7)