PhpOffice-PhpPresentation : Row height issue on creating PPT with tables

968 Views Asked by At

I am writing code on PHP for creating a PPT document. In this, I am showing data in table format all is good except for one issue. For the large string in a cell height of the row is resizing automatically while I set 40px height at the time of row creation, due to this some rows of tables plotting outside the slide. Is there any way to calculate the table height to compare slide height?

Screensot :enter image description here

My code is:

<?php 
use PhpOffice\PhpPresentation\IOFactory;
use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\Shape;
use PhpOffice\PhpPresentation\Shape\Drawing;
use PhpOffice\PhpPresentation\Style\Alignment;
use PhpOffice\PhpPresentation\Style\Bullet;
use PhpOffice\PhpPresentation\Style\Color;
use PhpOffice\PhpPresentation\Style\Border;
use PhpOffice\PhpPresentation\Style\Fill;

$rowHeight = 0;
$rowCount = count($valuesArray);
$SlideHeight = 514;
$tableHeaderTop = 115;
$HeaderRowHeight = 50;
$AccountName = 'XYZ';
$currentSlide = createTemplatedSlide($objPHPPresentation);
createCommonHeading($objPHPPresentation, $currentSlide, "TEAM", $AccountName);
foreach ($valuesArray as $key => $val) {
    $row = $tableShape->createRow();

    $row->setHeight(40);
    $rowHeight += $row->getHeight();
    $rowCount --;


        $rowNumber++;
        
        if ($key % 2 == 0)
            $row->getFill()->setFillType(Fill::FILL_SOLID)->setRotation(90)->setStartColor(new Color('FFcbcdd4'))->setEndColor(new Color('FFcbcdd4'));
        else
            $row->getFill()->setFillType(Fill::FILL_SOLID)->setRotation(90)->setStartColor(new Color('FFe7e8eb'))->setEndColor(new Color('FFe7e8eb'));
 
        foreach ($val as $k => $v) {
            $textAlignment = Alignment::HORIZONTAL_CENTER;
             $oCell = $row->nextCell();
            $oCell->setWidth(200);
             $oCell->getActiveParagraph()->getAlignment()
                ->setMarginLeft(3)
                ->setMarginRight(3)
                ->setMarginTop(3)
                ->setMarginBottom(3);
                 createCommonTextRun($oCell, 7, $bold, "Montserrat", 0, $v);
            }
            
            
            $currentHeight = $tableHeaderTop + $HeaderRowHeight + $rowHeight;
              if ($currentHeight > $SlideHeight && $rowCount > 0) {
                    $currentSlide = createTemplatedSlide($objPHPPresentation);
                    createCommonHeading($objPHPPresentation, $currentSlide, $headingText, $AccountName);
                    $tableShape = createCommonTable($currentSlide, count($tableHeaderData));
                    createCommonTableHeaderRows($tableShape, $tableHeaderData, $Module);
              
                $rowHeight = 0;
              }
            
            
}


function createCommonTextRun($shape, $size, $setBold, $textStyle, $characterSpacing, $content, $color = null) {

$textRun = $shape->createTextRun($content);
$textRun->getFont()->setBold($setBold)
                   ->setName($textStyle);
$textRun->getFont()->setCharacterSpacing($characterSpacing);
$textRun->getFont()->setSize($size);

if ($color != null)
    $textRun->getFont()->setColor($color);
}

function createTemplatedSlide(PhpOffice\PhpPresentation\PhpPresentation $objPHPPresentation) {
// Create slide
$slide = $objPHPPresentation->createSlide();

// Return slide
return $slide;
}

function createCommonTable($currentSlide, $columnsCount, $continue = FALSE, $Module = "") {
$tableShape = $currentSlide->createTableShape($columnsCount);


return $tableShape;
}
function createCommonTextShape($slide, $height, $width, $offsetX, $offsetY, $horizontalAlignment) {
$shape = $slide->createRichTextShape();
$shape->setHeight($height);
$shape->setWidth($width);
$shape->setOffsetX($offsetX);
$shape->setOffsetY($offsetY);
$shape->getActiveParagraph()->getAlignment()->setHorizontal($horizontalAlignment);

return $shape;
 }

 function createCommonHeading($objPHPPresentation, $currentSlide, $headingText, $AccountName, $Module = "") {
$slideIndex = $objPHPPresentation->getSlideCount();
$headingTextColor = '#000';
$headingAlignment = Alignment::HORIZONTAL_CENTER;
$headingShape = createCommonTextShape($currentSlide, 100, 700, 125, 20, $headingAlignment);
createCommonTextRun($headingShape, 18, false, "Montserrat", 0, $headingText, $headingTextColor);

}
  
function createCommonTableHeaderRows($tableShape, $tableHeaderData, $Module) {
$totalColumns = count($tableHeaderData);
$topWidth = 2;
$row = $tableShape->createRow();

$row->setHeight(50);
$row->getFill()->setFillType(Fill::FILL_SOLID)->setRotation(90)->setStartColor(new Color('FF002f6c'))->setEndColor(new Color('FF002f6c'));
for ($i = 0; $i < $totalColumns; $i++) {
$oCell = $row->nextCell();
    createCommonTextRun($oCell, 9, false, "Montserrat", 0, $tableHeaderData[$i], $cellTextColor);
    $oCell->getActiveParagraph()->getAlignment()->setVertical(Alignment::VERTICAL_CENTER)->setHorizontal( Alignment::HORIZONTAL_CENTER );
}

}
1

There are 1 best solutions below

1
On

Set row height after creating cell content.

$row->getFill()->setFillType(Fill::FILL_SOLID)->setRotation(90)->setStartColor(new Color('FF002f6c'))->setEndColor(new Color('FF002f6c'));
for ($i = 0; $i < $totalColumns; $i++) {
    $oCell = $row->nextCell();
    createCommonTextRun($oCell, 9, false, "Montserrat", 0, $tableHeaderData[$i], $cellTextColor);
    $oCell->getActiveParagraph()->getAlignment()->setVertical(Alignment::VERTICAL_CENTER)->setHorizontal( Alignment::HORIZONTAL_CENTER );
}

$row->setHeight(50);