Skip to content

Commit a757321

Browse files
authored
Merge pull request #10 from evosys21/feature/multicell-pagebrake
Implement Multicell Disable PageBrake and MinHeight
2 parents b5eec76 + 573ce73 commit a757321

13 files changed

+72
-7
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use EvoSys21\PdfLib\Dev\DevFactory;
4+
5+
require_once __DIR__ . '/autoload.php';
6+
7+
$factory = new DevFactory();
8+
9+
$multicell = $factory->multicell();
10+
$pdf = $multicell->getPdfObject();
11+
12+
$txt = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
13+
14+
$pdf->AutoPageBreak = false;
15+
$multicell->disablePageBreak();
16+
17+
for ($i = 0; $i < 25; $i++) {
18+
$multicell->multiCell(100, 5, $txt);
19+
}
20+
21+
// output the pdf
22+
$pdf->Output();

dev/test-multicell-min-height.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use EvoSys21\PdfLib\Dev\DevFactory;
4+
5+
require_once __DIR__ . '/autoload.php';
6+
7+
$factory = new DevFactory();
8+
9+
$multicell = $factory->multicell();
10+
$pdf = $multicell->getPdfObject();
11+
12+
$txt = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
13+
14+
$multicell->multiCell(100, 5, $txt, 1, 'J', 1, 3, 3, 3, 3, 50);
15+
16+
// output the pdf
17+
$pdf->Output();

src/Multicell.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,8 @@ public function multiCell(
759759
float $paddingLeft = 0,
760760
float $paddingTop = 0,
761761
float $paddingRight = 0,
762-
float $paddingBottom = 0
762+
float $paddingBottom = 0,
763+
float $minHeight = 0
763764
) {
764765
$this->multicellData = new MulticellData($this->pdf);
765766
$this->multicellData->width = $width;
@@ -784,25 +785,36 @@ public function multiCell(
784785
$lines = $this->stringToLines($this->multicellData);
785786
$iCounter = 9999; //avoid infinite loop for any reasons
786787

788+
$linesCount = count($lines);
789+
$cellHeight = $height * $linesCount + $paddingTop + $paddingBottom;
790+
791+
if ($cellHeight < $minHeight) {
792+
$diff = $minHeight - $cellHeight;
793+
$paddingBottom += $diff;
794+
$this->multicellData->paddingBottom = $paddingBottom;
795+
}
796+
787797
$doBreak = false;
788798

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

793803
//Number of rows that have space on this page:
794-
$iRows = floor($iLeftHeight / $height);
804+
$rows = floor($iLeftHeight / $height);
795805
// Added check for 'AcceptPageBreak'
796-
if (count($lines) > $iRows && $this->pdf->AcceptPageBreak()) {
797-
$printLines = array_slice($lines, 0, $iRows);
798-
$lines = array_slice($lines, $iRows);
806+
if (count($lines) > $rows && $this->pdf->AcceptPageBreak() && !$this->options->disablePageBreak) {
807+
$printLines = array_slice($lines, 0, $rows);
808+
$lines = array_slice($lines, $rows);
799809
$addPage = true;
800810
} else {
801811
$printLines = &$lines;
802812
$doBreak = true;
803813
}
804814

805-
$this->multiCellSec($this->multicellData, $printLines);
815+
if (!is_null($this->multicellData)) {
816+
$this->multiCellSec($this->multicellData, $printLines);
817+
}
806818

807819
if ($addPage) {
808820
$this->beforeAddPage();
@@ -1443,4 +1455,11 @@ public function resetSpacers(): self
14431455
$this->options->spacers = [];
14441456
return $this;
14451457
}
1458+
1459+
public function disablePageBreak(bool $disablePageBreak = true): self
1460+
{
1461+
$this->options->disablePageBreak = $disablePageBreak;
1462+
return $this;
1463+
}
1464+
14461465
}

src/MulticellOptions.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class MulticellOptions
1414
{
1515
public $styles = [];
1616
public $spacers = [];
17+
18+
public bool $disablePageBreak = false;
1719
protected $stylesBackup = null;
1820

1921
/**

src/Table/Cell/Multicell.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public function split($rowHeight, $maxHeight): array
226226

227227
public function getText(): string
228228
{
229-
return $this->TEXT;
229+
return $this->TEXT ?? '';
230230
}
231231

232232

tests/Feature/ExamplesTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use EvoSys21\PdfLib\Tests\BaseExamplesTestCase;
66

77
/**
8+
* @covers \EvoSys21\PdfLib\Multicell
9+
* @covers \EvoSys21\PdfLib\MulticellOptions
10+
* @covers \EvoSys21\PdfLib\Table
811
*/
912
class ExamplesTest extends BaseExamplesTestCase
1013
{

tests/Feature/ProviderTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public function getDevSources(): array
3636
'test-multicell-shrinking.php',
3737
'test-multicell-shrinking2.php',
3838
'test-multicell-style.php',
39+
'test-multicell-disable-pagebreak.php',
40+
'test-multicell-min-height.php',
3941
];
4042

4143
$contexts = [
8.97 KB
Binary file not shown.
2.41 KB
Binary file not shown.
29.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)