Skip to content

Commit

Permalink
automatic page breaking on div elements bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalpospiech committed Jan 31, 2019
1 parent 3ea7f8e commit 51d4790
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 18 deletions.
14 changes: 8 additions & 6 deletions examples/Pages.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
<div style="border:1px solid pink;padding:40px;">Divide 4</div>
<div style="border:1px solid pink;padding:40px;">Divide 5</div>
<div style="border:1px solid pink;padding:40px;">Divide 6</div>
<div style="border:1px solid pink;padding:40px;">Divide 7</div>
<div style="border:1px solid pink;padding:40px;">Divide 8</div>
<div style="border:1px solid pink;padding:40px;">Divide 9</div>
<div style="border:1px solid blue;padding:40px;">Divide 7</div>
<div style="border:1px solid green;padding:40px;">Divide 8</div>
<div style="border:1px solid yellow;padding:40px;">Divide 9</div>
<div style="border:1px solid orange;">test</div>
<!--
<div style="border:1px solid pink;padding:40px;">Divide 10</div>
<div style="border:1px solid pink;padding:40px;">Divide 10</div>
<div style="border:1px solid pink;padding:40px;">Divide 12</div>
Expand All @@ -17,7 +19,7 @@
<div style="border:1px solid pink;padding:40px;">Divide 16</div>
<div style="border:1px solid pink;padding:40px;">Divide 17</div>
<div style="border:1px solid pink;padding:40px;">Divide 18</div>
<div style="border:1px solid pink;padding:40px;">Divide 19</div>
<div style="border:1px solid pink;padding:40px;">Divide 19</div>-->
<!--<table style="border:1px solid green;border-collapse:separate;">
<thead>
<tr style="background-color:yellow">
Expand Down Expand Up @@ -54,7 +56,7 @@
</tr>
</tfoot>
</table>-->
<div style="border:1px solid green;padding:20px;">
<!--<div style="border:1px solid green;padding:20px;">
This is the wrapper of the wrapper of page breaking element that should not be copied
<div style="border:1px solid orange;">
This should not be copied
Expand All @@ -63,4 +65,4 @@
</div>
<div style="font-size:24px;border:1px solid red;">This is the brand new page with number 2.</div>
<div style="page-break-after:always;"></div>
<div style="font-size:24px;border:1px solid red;">This is the brand new page with number 3.</div>
<div style="font-size:24px;border:1px solid red;">This is the brand new page with number 3.</div>-->
Binary file modified examples/Pages.pdf
Binary file not shown.
Binary file modified examples/PagesTables.pdf
Binary file not shown.
15 changes: 12 additions & 3 deletions lib/Layout/BlockBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function getNewLineBox($before = null)
* Close line box.
*
* @param \YetiForcePDF\Layout\LineBox|null $lineBox
* @param bool $createNew
* @param bool $createNew
*
* @return \YetiForcePDF\Layout\LineBox
*/
Expand Down Expand Up @@ -520,6 +520,9 @@ public function divideLines()
*/
public function measureHeight(bool $afterPageDividing = false)
{
if ($this->wasCut() === \YetiForcePDF\Page::CUT_BELOW) {
return $this;
}
$this->applyStyleHeight();
foreach ($this->getChildren() as $child) {
$child->measureHeight($afterPageDividing);
Expand All @@ -542,6 +545,9 @@ public function measureHeight(bool $afterPageDividing = false)
*/
public function measureOffset()
{
if ($this->wasCut() === \YetiForcePDF\Page::CUT_BELOW) {
return $this;
}
$top = $this->document->getCurrentPage()->getCoordinates()->getY();
$left = $this->document->getCurrentPage()->getCoordinates()->getX();
$marginTop = $this->getStyle()->getRules('margin-top');
Expand Down Expand Up @@ -577,6 +583,9 @@ public function measureOffset()
*/
public function measurePosition()
{
if ($this->wasCut() === \YetiForcePDF\Page::CUT_BELOW) {
return $this;
}
$x = $this->document->getCurrentPage()->getCoordinates()->getX();
$y = $this->document->getCurrentPage()->getCoordinates()->getY();
if ($parent = $this->getParent()) {
Expand Down Expand Up @@ -623,11 +632,11 @@ public function replacePageNumbers()
if ($child instanceof TextBox) {
if (mb_stripos($child->getTextContent(), '{p}') !== false) {
$pageNumber = $this->document->getCurrentPage()->getPageNumber();
$child->setText(preg_replace('/{p}/ui', (string) $pageNumber, $child->getTextContent()));
$child->setText(preg_replace('/{p}/ui', (string)$pageNumber, $child->getTextContent()));
$child->getClosestByType('BlockBox')->layout();
}
if (mb_stripos($child->getTextContent(), '{a}') !== false) {
$pages = (string) $this->document->getCurrentPage()->getPageCount();
$pages = (string)$this->document->getCurrentPage()->getPageCount();
$child->setText(preg_replace('/{a}/ui', $pages, $child->getTextContent()));
$child->getClosestByType('BlockBox')->layout();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Layout/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,11 @@ public function wasCut()
/**
* Set cut.
*
* @param bool $cut
* @param int $cut
*
* @return $this
*/
public function setCut(bool $cut)
public function setCut(int $cut)
{
$this->cut = $cut;
return $this;
Expand Down
24 changes: 17 additions & 7 deletions lib/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ class Page extends \YetiForcePDF\Objects\Basic\DictionaryObject
* Landscape page orientation.
*/
const ORIENTATION_LANDSCAPE = 'L';
/**
* After page breaking box was cut below
*/
const CUT_BELOW = 1;
/**
* After page breaking box was cut above
*/
const CUT_ABOVE = 2;
/**
* Current page format.
*
Expand Down Expand Up @@ -965,7 +973,7 @@ public function cutAbove(Box $child, string $yPos)
->setRule('border-top-width', '0')
->setRule('padding-top', '0')
->setRule('margin-top', '0');
$child->setCut(true);
$child->setCut(static::CUT_ABOVE);
return $this;
}

Expand All @@ -989,7 +997,7 @@ public function cutBelow(Box $child, string $yPos)
->setRule('border-bottom-width', '0')
->setRule('margin-bottom', '0')
->setRule('padding-bottom', '0');
$child->setCut(true);
$child->setCut(static::CUT_BELOW);
return $this;
}

Expand All @@ -1005,7 +1013,7 @@ public function cloneAndDivideChildrenAfterY(string $yPos, array $boxes = null)
if ($boxes === null) {
$boxes = [];
foreach ($this->getBox()->getChildren() as $child) {
if (Math::comp($child->getCoordinates()->getEndY(), $yPos) > 0) {
if (Math::comp($child->getCoordinates()->getEndY(), $yPos) >= 0) {
$boxes[] = $child;
}
}
Expand All @@ -1026,9 +1034,9 @@ public function cloneAndDivideChildrenAfterY(string $yPos, array $boxes = null)
continue;
}
$childCoords = $child->getCoordinates();
if (Math::comp($childCoords->getEndY(), $yPos) > 0) {
$boxes = $this->cloneAndDivideChildrenAfterY($yPos, [$child]);
foreach ($boxes as $childBox) {
if (Math::comp($childCoords->getEndY(), $yPos) >= 0) {
$childBoxes = $this->cloneAndDivideChildrenAfterY($yPos, [$child]);
foreach ($childBoxes as $childBox) {
$cloned->appendChild($childBox);
}
}
Expand All @@ -1039,6 +1047,8 @@ public function cloneAndDivideChildrenAfterY(string $yPos, array $boxes = null)
if (Math::comp($box->getCoordinates()->getY(), $yPos) < 0 && Math::comp($box->getCoordinates()->getEndY(), $yPos) > 0) {
$this->cutBelow($box, $yPos);
$this->cutAbove($cloned, $yPos);
} elseif (Math::comp($box->getCoordinates()->getY(), $yPos) >= 0) {
$box->setRenderable(false)->setForMeasurement(false);
}
}
$clonedBoxes[] = $cloned;
Expand Down Expand Up @@ -1223,7 +1233,7 @@ public function breakOverflow()
$newBox->appendChild($clonedBox->getParent()->removeChild($clonedBox));
}
$this->getBox()->getStyle()->fixDomTree();
$this->getBox()->measureHeight()->measureOffset()->alignText()->measurePosition();
$this->getBox()->measureHeight(true)->measureOffset()->alignText()->measurePosition();
$newBox->layout(true);
$this->document->setCurrentPage($newPage);
if (Math::comp($newBox->getDimensions()->getHeight(), $this->getDimensions()->getHeight()) > 0) {
Expand Down

0 comments on commit 51d4790

Please sign in to comment.