Skip to content

Commit

Permalink
Merge pull request #10 from evosys21/feature/multicell-pagebrake
Browse files Browse the repository at this point in the history
Implement Multicell Disable PageBrake and MinHeight
  • Loading branch information
klodoma authored Oct 29, 2024
2 parents b5eec76 + 573ce73 commit a757321
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 7 deletions.
22 changes: 22 additions & 0 deletions dev/test-multicell-disable-pagebreak.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use EvoSys21\PdfLib\Dev\DevFactory;

require_once __DIR__ . '/autoload.php';

$factory = new DevFactory();

$multicell = $factory->multicell();
$pdf = $multicell->getPdfObject();

$txt = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";

$pdf->AutoPageBreak = false;
$multicell->disablePageBreak();

for ($i = 0; $i < 25; $i++) {
$multicell->multiCell(100, 5, $txt);
}

// output the pdf
$pdf->Output();
17 changes: 17 additions & 0 deletions dev/test-multicell-min-height.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use EvoSys21\PdfLib\Dev\DevFactory;

require_once __DIR__ . '/autoload.php';

$factory = new DevFactory();

$multicell = $factory->multicell();
$pdf = $multicell->getPdfObject();

$txt = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";

$multicell->multiCell(100, 5, $txt, 1, 'J', 1, 3, 3, 3, 3, 50);

// output the pdf
$pdf->Output();
31 changes: 25 additions & 6 deletions src/Multicell.php
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,8 @@ public function multiCell(
float $paddingLeft = 0,
float $paddingTop = 0,
float $paddingRight = 0,
float $paddingBottom = 0
float $paddingBottom = 0,
float $minHeight = 0
) {
$this->multicellData = new MulticellData($this->pdf);
$this->multicellData->width = $width;
Expand All @@ -784,25 +785,36 @@ public function multiCell(
$lines = $this->stringToLines($this->multicellData);
$iCounter = 9999; //avoid infinite loop for any reasons

$linesCount = count($lines);
$cellHeight = $height * $linesCount + $paddingTop + $paddingBottom;

if ($cellHeight < $minHeight) {
$diff = $minHeight - $cellHeight;
$paddingBottom += $diff;
$this->multicellData->paddingBottom = $paddingBottom;
}

$doBreak = false;

do {
$iLeftHeight = $this->pdf->h - $this->pdf->bMargin - $this->pdf->GetY() - $paddingTop - $paddingBottom;
$addPage = false;

//Number of rows that have space on this page:
$iRows = floor($iLeftHeight / $height);
$rows = floor($iLeftHeight / $height);
// Added check for 'AcceptPageBreak'
if (count($lines) > $iRows && $this->pdf->AcceptPageBreak()) {
$printLines = array_slice($lines, 0, $iRows);
$lines = array_slice($lines, $iRows);
if (count($lines) > $rows && $this->pdf->AcceptPageBreak() && !$this->options->disablePageBreak) {
$printLines = array_slice($lines, 0, $rows);
$lines = array_slice($lines, $rows);
$addPage = true;
} else {
$printLines = &$lines;
$doBreak = true;
}

$this->multiCellSec($this->multicellData, $printLines);
if (!is_null($this->multicellData)) {
$this->multiCellSec($this->multicellData, $printLines);
}

if ($addPage) {
$this->beforeAddPage();
Expand Down Expand Up @@ -1443,4 +1455,11 @@ public function resetSpacers(): self
$this->options->spacers = [];
return $this;
}

public function disablePageBreak(bool $disablePageBreak = true): self
{
$this->options->disablePageBreak = $disablePageBreak;
return $this;
}

}
2 changes: 2 additions & 0 deletions src/MulticellOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class MulticellOptions
{
public $styles = [];
public $spacers = [];

public bool $disablePageBreak = false;
protected $stylesBackup = null;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Table/Cell/Multicell.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function split($rowHeight, $maxHeight): array

public function getText(): string
{
return $this->TEXT;
return $this->TEXT ?? '';
}


Expand Down
3 changes: 3 additions & 0 deletions tests/Feature/ExamplesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use EvoSys21\PdfLib\Tests\BaseExamplesTestCase;

/**
* @covers \EvoSys21\PdfLib\Multicell
* @covers \EvoSys21\PdfLib\MulticellOptions
* @covers \EvoSys21\PdfLib\Table
*/
class ExamplesTest extends BaseExamplesTestCase
{
Expand Down
2 changes: 2 additions & 0 deletions tests/Feature/ProviderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public function getDevSources(): array
'test-multicell-shrinking.php',
'test-multicell-shrinking2.php',
'test-multicell-style.php',
'test-multicell-disable-pagebreak.php',
'test-multicell-min-height.php',
];

$contexts = [
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit a757321

Please sign in to comment.