-->

How do I make a data automatically move to a new p

2019-08-16 01:48发布

问题:

I have dynamic data. So my data can change besides that I also have a data signature that cannot be separated. This signature data must be on the same page. See the red mark in this image :

It is a unit and cannot be separated. Signature data must be on the same page

My problem is because my data is dynamic. This makes the position of the signature data can be located in any position. See image below :

Because my data increases, the position of the headmaster in the signature data is separate

How do I make signature data (see picture 1), which red marks automatically move to the next page if the data is separate?

回答1:

You can set manual page breaks with something like this:

<?php
//All lines are written from the sheet code at this moment 
//The code will insert a page break and the repeated header 

//Page margins
$sheet->getPageMargins()->setTop(0.5);
$sheet->getPageMargins()->setRight(0.75);
$sheet->getPageMargins()->setLeft(0.75);
$sheet->getPageMargins()->setBottom(1);

//Use fit to page for the horizontal direction
$sheet->getPageSetup()->setFitToWidth(1);
$sheet->getPageSetup()->setFitToHeight(0);

$headerItems = array(); //add your header items here as array
$headerRowsHeight = 0;// calculated height of header and images of the top
$rowCounter = 100; //get last written row

//add (other) modifier of page hight here

$pageHeight=25+50 + $headerRowsHeight; //Current used page height Header + Footer + $headerRowsHeight
$reset = $pageHeight; //If you will have the firstpage diffrent change reset value and/or pageheight
$pageMaxHeight = 980 ; //Maximale page height DIN A4 arround this
$pageClearance = 15; //Clearance of footer

//Iterate trough all written lines
for ($row = 1; $row <= $rowCounter; ++$row) {
    $height=15.1; //standard row height

    //get the row height 
    $dim = $sheet->getRowDimension($row)->getRowHeight();

    //get special cell heights (non standard)
    if($dim != -1){
        $height=$dim;
    }

    //add height for line to pageheight = get current used space
    $pageHeight = $pageHeight + $height;

    //Check if the space is still in the range of page 
    $leftOverSpace = $pageMaxHeight-$pageHeight;

    //Change $pageClearance  to your preferd space before footer
    if( $leftOverSpace < $pageClearance){
        //Set pagebraek
        $sheet->setBreak('A'.$row, \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::BREAK_ROW);
        //Reset page height
        $pageHeight=$reset;
        //Add page header to new page
        createHeader($sheet, $row+1, $headeritems);
    }
}

//Creates a header for every page
function createHeader($sheet, $row, $texts){
    $count = $row;
    // Iterate trough the header text array
    foreach($texts as $text){
        //Insert a new line with header
        $sheet->insertNewRowBefore($count, 1);

        //Do your header stuff here
        $count++;
    }
    //Add two lines after the header
    $sheet->insertNewRowBefore($count, 2);
    //Return row number
    return $count;
}
?>

Place not that the calculation is not 100% exact and that you must adapt this to your code.