Skip to content

Commit 7071ba7

Browse files
jakejackson1claude
andauthored
Start a table with page-break-before on a new page (#576)
`page-break-before` and `break-before` on a table were never read, so `table { page-break-before: always }` left the table on the page before. On a top-level table, `always`, `left` and `right` now break the page as they do before a block. The break comes before the table closes the blocks it opens in, so those blocks are opened again around the table on the new page. The steps of the forced break move to `Tag::forcePageBreak()`, which `page-break-after` on blocks and tables uses as well. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c6883ab commit 7071ba7

8 files changed

Lines changed: 218 additions & 62 deletions

File tree

‎CHANGELOG-GRAVITYPDF.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ Bugfixes
119119
* `@page { size: A4 }`, or a `size` of two lengths with no `margin`, gave 63 pages with one character on each. A page-size name such as `A4`, `letter` or `A5 landscape` now sets the sheet, as a browser sets the paper, and the space around a page box given as two lengths is no longer counted twice. [mpdf/mpdf#1220] [#552] [#562]
120120
* `@page { size: landscape }`, or a page box wider than it is tall, left the first page portrait, and `size: portrait` left a landscape document landscape. The first page now turns. A page box and percentage margins are measured on the turned sheet, so `size: 250mm 150mm` on A4 is no longer cut to 210 mm wide. [#552] [#562]
121121
* `page-break-before: auto` or `avoid` on a block inside another block closed the outer block and opened it again, so its border was drawn around each part. `page-break-after: auto` or `avoid` on a table started a new page. [#552] [#569]
122+
* `page-break-before` on a table was ignored. On a top-level table, `always`, `left` and `right` now start it on a new page, inside the blocks around it. [#552] [#576]
122123
* `background-size: cover` scaled by the wrong ratio when the image came out shorter than the area. [mpdf/mpdf#833] [#22]
123124
* Only double-quoted attributes were read. Single-quoted, unquoted and bare attributes are now read as well. [mpdf/mpdf#2030] [#24]
124125
* A shadow colour written without spaces, such as `rgba(255,0,0,0.5)`, fell back to grey. Whitespace in shadows is now parsed as CSS writes it too. [#25]
@@ -750,6 +751,7 @@ These changes do not change output.
750751
[#563]: https://github.com/GravityPDF/mpdf/pull/563
751752
[#565]: https://github.com/GravityPDF/mpdf/pull/565
752753
[#569]: https://github.com/GravityPDF/mpdf/pull/569
754+
[#576]: https://github.com/GravityPDF/mpdf/pull/576
753755
[#577]: https://github.com/GravityPDF/mpdf/pull/577
754756
[#568]: https://github.com/GravityPDF/mpdf/pull/568
755757
[#567]: https://github.com/GravityPDF/mpdf/pull/567

‎src/Tag/BlockTag.php‎

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,37 +1405,7 @@ public function close(&$ahtml, &$ihtml)
14051405
$this->mpdf->x = $this->mpdf->lMargin + $this->mpdf->blk[$this->mpdf->blklvl]['outer_left_margin'];
14061406

14071407
if (!$this->mpdf->tableLevel && $page_break_after) {
1408-
$save_blklvl = $this->mpdf->blklvl;
1409-
$save_blk = $this->mpdf->blk;
1410-
$save_silp = $this->mpdf->saveInlineProperties();
1411-
$save_ilp = $this->mpdf->InlineProperties;
1412-
$save_bflp = $this->mpdf->InlineBDF;
1413-
$save_bflpc = $this->mpdf->InlineBDFctr; // mPDF 6
1414-
// mPDF 6 pagebreaktype
1415-
$startpage = $this->mpdf->page;
1416-
$pagebreaktype = $this->mpdf->defaultPagebreakType;
1417-
if ($this->mpdf->ColActive) {
1418-
$pagebreaktype = 'cloneall';
1419-
}
1420-
1421-
// mPDF 6 pagebreaktype
1422-
$this->mpdf->_preForcedPagebreak($pagebreaktype);
1423-
1424-
if ($page_break_after === 'RIGHT') {
1425-
$this->mpdf->AddPage($this->mpdf->CurOrientation, 'NEXT-ODD');
1426-
} elseif ($page_break_after === 'LEFT') {
1427-
$this->mpdf->AddPage($this->mpdf->CurOrientation, 'NEXT-EVEN');
1428-
} else {
1429-
$this->mpdf->AddPage($this->mpdf->CurOrientation);
1430-
}
1431-
1432-
// mPDF 6 pagebreaktype
1433-
$this->mpdf->_postForcedPagebreak($pagebreaktype, $startpage, $save_blk, $save_blklvl);
1434-
1435-
$this->mpdf->InlineProperties = $save_ilp;
1436-
$this->mpdf->InlineBDF = $save_bflp;
1437-
$this->mpdf->InlineBDFctr = $save_bflpc; // mPDF 6
1438-
$this->mpdf->restoreInlineProperties($save_silp);
1408+
$this->forcePageBreak($page_break_after);
14391409
}
14401410
// mPDF 6 bidi
14411411
// Block

‎src/Tag/Table.php‎

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ public function open($attr, &$ahtml, &$ihtml)
1616
if ($this->mpdf->ColActive) {
1717
$this->mpdf->colvAlign = '';
1818
} // *COLUMNS*
19+
20+
// The page break comes first, so the blocks it opens again on the new page are started below
21+
if (!$this->mpdf->tableLevel) {
22+
$preview = $this->cssManager->PreviewBlockCSS('TABLE', $attr);
23+
if ($this->forcesPageBreak($preview, 'PAGE-BREAK-BEFORE')) {
24+
$this->forcePageBreak(strtoupper($preview['PAGE-BREAK-BEFORE']));
25+
}
26+
}
27+
1928
if ($this->mpdf->lastblocklevelchange == 1) {
2029
$blockstate = 1;
2130
} // Top margins/padding only
@@ -1238,37 +1247,7 @@ public function close(&$ahtml, &$ihtml)
12381247
}
12391248

12401249
if ($page_break_after) {
1241-
$save_blklvl = $this->mpdf->blklvl;
1242-
$save_blk = $this->mpdf->blk;
1243-
$save_silp = $this->mpdf->saveInlineProperties();
1244-
$save_ilp = $this->mpdf->InlineProperties;
1245-
$save_bflp = $this->mpdf->InlineBDF;
1246-
$save_bflpc = $this->mpdf->InlineBDFctr; // mPDF 6
1247-
// mPDF 6 pagebreaktype
1248-
$startpage = $this->mpdf->page;
1249-
$pagebreaktype = $this->mpdf->defaultPagebreakType;
1250-
if ($this->mpdf->ColActive) {
1251-
$pagebreaktype = 'cloneall';
1252-
}
1253-
1254-
// mPDF 6 pagebreaktype
1255-
$this->mpdf->_preForcedPagebreak($pagebreaktype);
1256-
1257-
if ($page_break_after === 'RIGHT') {
1258-
$this->mpdf->AddPage($this->mpdf->CurOrientation, 'NEXT-ODD');
1259-
} elseif ($page_break_after === 'LEFT') {
1260-
$this->mpdf->AddPage($this->mpdf->CurOrientation, 'NEXT-EVEN');
1261-
} else {
1262-
$this->mpdf->AddPage($this->mpdf->CurOrientation);
1263-
}
1264-
1265-
// mPDF 6 pagebreaktype
1266-
$this->mpdf->_postForcedPagebreak($pagebreaktype, $startpage, $save_blk, $save_blklvl);
1267-
1268-
$this->mpdf->InlineProperties = $save_ilp;
1269-
$this->mpdf->InlineBDF = $save_bflp;
1270-
$this->mpdf->InlineBDFctr = $save_bflpc; // mPDF 6
1271-
$this->mpdf->restoreInlineProperties($save_silp);
1250+
$this->forcePageBreak($page_break_after);
12721251
}
12731252
}
12741253

‎src/Tag/Tag.php‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,49 @@ protected function forcesPageBreak(array $properties, $property)
134134
return isset($properties[$property]) && in_array(strtoupper($properties[$property]), ['ALWAYS', 'LEFT', 'RIGHT'], true);
135135
}
136136

137+
/**
138+
* Start a new page for a page-break-before or page-break-after, closing the open blocks and opening them again on
139+
* the new page as the defaultPagebreakType option says
140+
*
141+
* @param string $pageBreak ALWAYS, LEFT or RIGHT
142+
*
143+
* @return void
144+
*/
145+
protected function forcePageBreak($pageBreak)
146+
{
147+
$save_blklvl = $this->mpdf->blklvl;
148+
$save_blk = $this->mpdf->blk;
149+
$save_silp = $this->mpdf->saveInlineProperties();
150+
$save_ilp = $this->mpdf->InlineProperties;
151+
$save_bflp = $this->mpdf->InlineBDF;
152+
$save_bflpc = $this->mpdf->InlineBDFctr; // mPDF 6
153+
// mPDF 6 pagebreaktype
154+
$startpage = $this->mpdf->page;
155+
$pagebreaktype = $this->mpdf->defaultPagebreakType;
156+
if ($this->mpdf->ColActive) {
157+
$pagebreaktype = 'cloneall';
158+
}
159+
160+
// mPDF 6 pagebreaktype
161+
$this->mpdf->_preForcedPagebreak($pagebreaktype);
162+
163+
if ($pageBreak === 'RIGHT') {
164+
$this->mpdf->AddPage($this->mpdf->CurOrientation, 'NEXT-ODD');
165+
} elseif ($pageBreak === 'LEFT') {
166+
$this->mpdf->AddPage($this->mpdf->CurOrientation, 'NEXT-EVEN');
167+
} else {
168+
$this->mpdf->AddPage($this->mpdf->CurOrientation);
169+
}
170+
171+
// mPDF 6 pagebreaktype
172+
$this->mpdf->_postForcedPagebreak($pagebreaktype, $startpage, $save_blk, $save_blklvl);
173+
174+
$this->mpdf->InlineProperties = $save_ilp;
175+
$this->mpdf->InlineBDF = $save_bflp;
176+
$this->mpdf->InlineBDFctr = $save_bflpc; // mPDF 6
177+
$this->mpdf->restoreInlineProperties($save_silp);
178+
}
179+
137180
/**
138181
* An object's attributes with the visibility of the span it is in. Printing reads a span's visibility from its
139182
* text buffer entry, and an object put straight into the buffer has none.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Mpdf;
4+
5+
/**
6+
* page-break-before and break-before on a top-level table start it on a new page, as they do for a block. The blocks
7+
* around the table are closed and opened again on the new page, with the table inside them.
8+
*/
9+
class TablePageBreakBeforeTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
10+
{
11+
12+
/**
13+
* The table's text is drawn on the page the break names, and the text after the table follows it
14+
*
15+
* @dataProvider providerBreakBefore
16+
*/
17+
public function testBreakBeforeTable($html, $pages)
18+
{
19+
$lines = $this->lines($html, ['mirrorMargins' => true]);
20+
21+
$this->assertSame($pages, array_map(function ($line) {
22+
return $line[0];
23+
}, $lines));
24+
}
25+
26+
/**
27+
* Break values on a table, written inline and in a style sheet, and on a nested table
28+
*
29+
* @return array
30+
*/
31+
public function providerBreakBefore()
32+
{
33+
$table = '<table style="%s"><tr><td>MB</td></tr></table>';
34+
35+
return [
36+
'always' => ['<p>MA</p>' . sprintf($table, 'page-break-before: always') . '<p>MC</p>', ['MA' => 1, 'MB' => 2, 'MC' => 2]],
37+
'break-before: page' => ['<p>MA</p>' . sprintf($table, 'break-before: page') . '<p>MC</p>', ['MA' => 1, 'MB' => 2, 'MC' => 2]],
38+
'right' => ['<p>MA</p>' . sprintf($table, 'page-break-before: right'), ['MA' => 1, 'MB' => 3]],
39+
'left' => ['<p>MA</p>' . sprintf($table, 'page-break-before: left'), ['MA' => 1, 'MB' => 2]],
40+
'style sheet' => ['<style>table.new { page-break-before: always; }</style><p>MA</p><table class="new"><tr><td>MB</td></tr></table>', ['MA' => 1, 'MB' => 2]],
41+
'auto' => ['<p>MA</p>' . sprintf($table, 'page-break-before: auto'), ['MA' => 1, 'MB' => 1]],
42+
'avoid' => ['<p>MA</p>' . sprintf($table, 'page-break-before: avoid'), ['MA' => 1, 'MB' => 1]],
43+
'nested table' => ['<table><tr><td>MA' . sprintf($table, 'page-break-before: always') . '</td></tr></table>', ['MA' => 1, 'MB' => 1]],
44+
];
45+
}
46+
47+
/**
48+
* A table that breaks inside a bordered block stays inside it on the new page, below its border and padding
49+
*/
50+
public function testTableStaysInsideTheBlockAroundIt()
51+
{
52+
$html = '<table style="page-break-before: always"><tr><td>MB</td></tr></table>';
53+
54+
$alone = $this->lines('<p>MA</p>' . $html);
55+
$inside = $this->lines('<div style="border: 1mm solid red; padding: 3mm"><p>MA</p>' . $html . '<p>MC</p></div>');
56+
57+
$this->assertSame(2, $inside['MB'][0]);
58+
$this->assertSame(2, $inside['MC'][0]);
59+
$this->assertEqualsWithDelta($alone['MB'][1] + 1 + 3, $inside['MB'][1], 0.001);
60+
$this->assertGreaterThan($inside['MB'][1], $inside['MC'][1]);
61+
}
62+
63+
/**
64+
* Render a document and find where each piece of text starting with "M" is drawn
65+
*
66+
* @param string $html
67+
* @param array $config
68+
* @return array[] The page and top of each marker, keyed by the marker
69+
*/
70+
private function lines($html, array $config = [])
71+
{
72+
$mpdf = new TextRecordingMpdf($config);
73+
$mpdf->WriteHTML($html);
74+
75+
$lines = [];
76+
foreach ($mpdf->drawnText as $i => $text) {
77+
if (preg_match('/^M[A-Z]$/', trim($text))) {
78+
$lines[trim($text)] = [$mpdf->drawnBoxes[$i][0], $mpdf->drawnY[$i]];
79+
}
80+
}
81+
82+
return $lines;
83+
}
84+
85+
}

‎tests/Mpdf/TextRecordingMpdf.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class TextRecordingMpdf extends Mpdf
2929
/** The page each of those lines was drawn on, its left and right edges and its top, in the same order. */
3030
public $drawnBoxes = [];
3131

32+
/** The top of each of those lines, in the same order. */
33+
public $drawnY = [];
34+
3235
function Cell($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = 0, $link = '', $currentx = 0, $lcpaddingL = 0, $lcpaddingR = 0, $valign = 'M', $spanfill = 0, $exactWidth = false, $OTLdata = false, $textvar = 0, $lineBox = false)
3336
{
3437
if (is_string($txt) && trim($txt) !== '') {
@@ -37,6 +40,7 @@ function Cell($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill =
3740
$this->drawnFontFamily[] = $this->FontFamily;
3841
$this->drawnFontSize[] = $this->FontSizePt;
3942
$this->drawnBoxes[] = [$this->page, $this->x, $this->x + $w, $this->y];
43+
$this->drawnY[] = $this->y;
4044
}
4145

4246
return parent::Cell($w, $h, $txt, $border, $ln, $align, $fill, $link, $currentx, $lcpaddingL, $lcpaddingR, $valign, $spanfill, $exactWidth, $OTLdata, $textvar, $lineBox);
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Snapshots;
4+
5+
/**
6+
* page-break-before and break-before on a table start it on a new page, inside the blocks around it, while the same
7+
* property on a nested table is ignored.
8+
*
9+
* @group snapshot
10+
*/
11+
class TablePageBreakBeforeSnapshotTest extends Snapshot
12+
{
13+
14+
/**
15+
* The fixture's name
16+
*
17+
* @return string
18+
*/
19+
public function getId()
20+
{
21+
return 'table-page-break-before';
22+
}
23+
24+
/**
25+
* Tables that start a page, each captioned with the page it should be on
26+
*/
27+
public function generatePdf()
28+
{
29+
ob_start();
30+
?>
31+
<style>
32+
body { font-size: 9pt; }
33+
h2 { font-size: 12pt; margin: 0 0 2mm 0; }
34+
p.caption { margin: 0 0 2mm 0; font-size: 8pt; color: #606060; }
35+
table { border-collapse: collapse; }
36+
td { border: 0.2mm solid #1f3a93; background-color: #eaf2fb; padding: 1mm 3mm; }
37+
div.box { border: 0.3mm solid #c0392b; padding: 3mm; }
38+
table.new { page-break-before: always; }
39+
</style>
40+
41+
<h2>Page 1</h2>
42+
<p class="caption">The table after this caption has page-break-before: always in a style sheet, so page 1 holds only this heading and caption.</p>
43+
44+
<table class="new">
45+
<tr><td>Page 2: this table starts the page.</td><td>Second cell</td></tr>
46+
<tr><td>Second row</td><td>Second row</td></tr>
47+
</table>
48+
49+
<div class="box">
50+
<p class="caption">This red box starts on page 2. The table in it has break-before: page, so it moves to page 3, and the box is drawn again around it there.</p>
51+
<table style="break-before: page">
52+
<tr><td>Page 3: this table is inside the red box, below its border and padding.</td></tr>
53+
</table>
54+
<p class="caption">This caption follows the table inside the red box on page 3.</p>
55+
</div>
56+
57+
<table style="width: 100%; margin-top: 4mm">
58+
<tr>
59+
<td>
60+
Page 3: the table in this cell has page-break-before: always, which a nested table ignores. It stays in the cell.
61+
<table style="page-break-before: always"><tr><td>Nested table</td></tr></table>
62+
</td>
63+
</tr>
64+
</table>
65+
<?php
66+
$html = ob_get_clean();
67+
68+
$this->mpdf = $this->createMpdf();
69+
70+
$this->mpdf->WriteHTML($html);
71+
}
72+
73+
}
32.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)