Skip to content

Commit fea9f80

Browse files
jakejackson1claude
andcommitted
Read break-before, break-after and break-inside as their page-break-* equivalents
The CSS3 break properties were never read, so `break-before: page` and `break-inside: avoid` did nothing. They are now read as `page-break-*`: `page` as `always`, `recto` and `verso` as `right` and `left`, and `avoid-page` as `avoid`. Column and region breaks are not page breaks, so they are read as `auto`. Any `page-break-before` value on a block, including `auto` and `avoid`, started a forced break. No page was added, but every open block was closed and opened again, so a bordered parent was drawn as two boxes. Any `page-break-after` value on a table added a page. Only `always`, `left` and `right` break the page now. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2c9a90b commit fea9f80

9 files changed

Lines changed: 302 additions & 2 deletions

File tree

‎CHANGELOG-GRAVITYPDF.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ New features
8787

8888
### Paged media
8989

90+
* **`break-before`, `break-after` and `break-inside`.** They are read as their `page-break-*` equivalents: `page` as `always`, `recto` and `verso` as `right` and `left`, and `avoid-page` as `avoid`. Column and region breaks are not page breaks, so they are read as `auto`. [#552] [#569]
9091
* **Side margins on `:first`, `:left` and `:right` pages.** The `:first`, `:left` and `:right` pages of an `@page` rule, named or not, can set `margin-left` and `margin-right`. Text flowing from page to page is set in the page area of each page, and columns are laid out across it. A side margin on a pseudo page belongs to that side of the physical page, so it is not mirrored. [#476] [#510]
9192
* Text beside a float, and the blocks around it, keep to the page area of each page the float runs over. [#549]
9293
* A block with a set width keeps its place in each page area: a centred block stays centred, and a block pushed right by `margin-left: auto` or set right to left stays against the right margin. [#550] [#551] [#553]
@@ -113,6 +114,7 @@ Bugfixes
113114
* A table with no background left the next table painted twice. [#43]
114115
* `@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]
115116
* `@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]
117+
* `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]
116118
* `background-size: cover` scaled by the wrong ratio when the image came out shorter than the area. [mpdf/mpdf#833] [#22]
117119
* Only double-quoted attributes were read. Single-quoted, unquoted and bare attributes are now read as well. [mpdf/mpdf#2030] [#24]
118120
* 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]
@@ -723,3 +725,4 @@ These changes do not change output.
723725
[#556]: https://github.com/GravityPDF/mpdf/pull/556
724726
[#558]: https://github.com/GravityPDF/mpdf/pull/558
725727
[#562]: https://github.com/GravityPDF/mpdf/pull/562
728+
[#569]: https://github.com/GravityPDF/mpdf/pull/569

‎src/Css/NormalizeProperties.php‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@
1313
class NormalizeProperties
1414
{
1515

16+
/** The page-break-* value each break-before, break-after and break-inside value is read as */
17+
const PAGE_BREAK_VALUES = [
18+
'auto' => 'auto',
19+
'avoid' => 'avoid',
20+
'avoid-page' => 'avoid',
21+
'page' => 'always',
22+
'left' => 'left',
23+
'right' => 'right',
24+
'recto' => 'right',
25+
'verso' => 'left',
26+
'column' => 'auto',
27+
'avoid-column' => 'auto',
28+
'region' => 'auto',
29+
'avoid-region' => 'auto',
30+
];
31+
1632
/**
1733
* @var Mpdf
1834
*/
@@ -94,6 +110,8 @@ public function normalize($prop)
94110
$this->processTextOutlineProperty($v);
95111
} elseif ($k === 'SIZE' || $k === 'SHEET-SIZE') {
96112
$this->processPageSizeProperty($k, $v);
113+
} elseif ($k === 'BREAK-BEFORE' || $k === 'BREAK-AFTER' || $k === 'BREAK-INSIDE') {
114+
$this->processBreakProperty($k, $v);
97115
} elseif (in_array($k, ['BACKGROUND', 'BACKGROUND-IMAGE', 'BACKGROUND-REPEAT', 'BACKGROUND-POSITION'], true)) {
98116
$this->processBackgroundProperty($k, $v);
99117
} elseif ($k === 'IMAGE-ORIENTATION') {
@@ -930,6 +948,23 @@ private function processPageSizeName(array $value)
930948
$this->properties['SIZE'] = 'AUTO';
931949
}
932950

951+
/**
952+
* Read break-before, break-after and break-inside as the page-break-* property of the same name
953+
*
954+
* Recto and verso pages are the odd and even pages, which page-break-* calls right and left. Column and region
955+
* breaks are not page breaks, so they are read as auto. Any other value is dropped.
956+
*
957+
* @param string $property BREAK-BEFORE, BREAK-AFTER or BREAK-INSIDE
958+
* @param string $value
959+
* @return void
960+
*/
961+
private function processBreakProperty($property, $value)
962+
{
963+
if (array_key_exists($value, self::PAGE_BREAK_VALUES)) {
964+
$this->properties['PAGE-' . $property] = self::PAGE_BREAK_VALUES[$value];
965+
}
966+
}
967+
933968
/**
934969
* Process image orientation CSS properties.
935970
*

‎src/Tag/BlockTag.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ public function open($attr, &$ahtml, &$ihtml)
393393
// If page-box has changed AND/OR PAGE-BREAK-BEFORE
394394
// mPDF 6 (uses $p - preview of properties so blklvl can be incremented after page-break)
395395
if (!$this->mpdf->tableLevel && (($pagesel && (!$this->mpdf->page_box['current'] || $pagesel != $this->mpdf->page_box['current']))
396-
|| (isset($p['PAGE-BREAK-BEFORE']) && $p['PAGE-BREAK-BEFORE']))) {
396+
|| $this->forcesPageBreak($p, 'PAGE-BREAK-BEFORE'))) {
397397
// mPDF 6 pagebreaktype
398398
$startpage = $this->mpdf->page;
399399
$pagebreaktype = $this->mpdf->defaultPagebreakType;

‎src/Tag/Table.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ public function open($attr, &$ahtml, &$ihtml)
422422
} elseif ($this->mpdf->tableLevel == 1) {
423423
$this->mpdf->table_keep_together = false;
424424
}
425-
if (isset($properties['PAGE-BREAK-AFTER']) && $this->mpdf->tableLevel == 1) {
425+
if ($this->forcesPageBreak($properties, 'PAGE-BREAK-AFTER') && $this->mpdf->tableLevel == 1) {
426426
$table['page_break_after'] = strtoupper($properties['PAGE-BREAK-AFTER']);
427427
}
428428

‎src/Tag/Tag.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ protected function getAlign($property)
121121
return array_key_exists($property, self::ALIGN) ? self::ALIGN[$property] : '';
122122
}
123123

124+
/**
125+
* Whether a page-break-before or page-break-after value starts a new page: always, left or right
126+
*
127+
* @param string[] $properties the element's computed CSS
128+
* @param string $property PAGE-BREAK-BEFORE or PAGE-BREAK-AFTER
129+
*
130+
* @return bool
131+
*/
132+
protected function forcesPageBreak(array $properties, $property)
133+
{
134+
return isset($properties[$property]) && in_array(strtoupper($properties[$property]), ['ALWAYS', 'LEFT', 'RIGHT'], true);
135+
}
136+
124137
/**
125138
* An object's attributes with the visibility of the span it is in. Printing reads a span's visibility from its
126139
* text buffer entry, and an object put straight into the buffer has none.

‎tests/Mpdf/BreakPropertiesTest.php‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
namespace Mpdf;
4+
5+
/**
6+
* break-before, break-after and break-inside act as the page-break-* properties they replace, and a break value that
7+
* forces no page leaves the block or table where it is.
8+
*/
9+
class BreakPropertiesTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
10+
{
11+
12+
/**
13+
* A forced break puts the marked text on the page the value names
14+
*
15+
* @dataProvider providerForcedBreak
16+
*/
17+
public function testForcedBreak($html, $pages)
18+
{
19+
$this->assertSame($pages, $this->pagesOf($html, ['mirrorMargins' => true]));
20+
}
21+
22+
/**
23+
* Forced breaks before and after blocks and tables, from inline styles and style sheets
24+
*
25+
* @return array
26+
*/
27+
public function providerForcedBreak()
28+
{
29+
return [
30+
'break-before: page' => ['<p>MA</p><p style="break-before: page">MB</p>', ['MA' => 1, 'MB' => 2]],
31+
'break-after: page' => ['<p style="break-after: page">MA</p><p>MB</p>', ['MA' => 1, 'MB' => 2]],
32+
'break-before: right' => ['<p>MA</p><p style="break-before: right">MB</p>', ['MA' => 1, 'MB' => 3]],
33+
'break-before: left' => ['<p>MA</p><p style="break-before: left">MB</p>', ['MA' => 1, 'MB' => 2]],
34+
'break-before: recto' => ['<p>MA</p><p style="break-before: recto">MB</p>', ['MA' => 1, 'MB' => 3]],
35+
'break-before: verso' => ['<p>MA</p><p style="break-before: verso">MB</p>', ['MA' => 1, 'MB' => 2]],
36+
'break-after: page on a table' => ['<table style="break-after: page"><tr><td>MA</td></tr></table><p>MB</p>', ['MA' => 1, 'MB' => 2]],
37+
'style sheet' => ['<style>h2 { break-before: page; }</style><p>MA</p><h2>MB</h2>', ['MA' => 1, 'MB' => 2]],
38+
'a later auto cancels it' => ['<style>h2 { break-before: page; } h2.x { break-before: auto; }</style><p>MA</p><h2 class="x">MB</h2>', ['MA' => 1, 'MB' => 1]],
39+
'a column break is not a page break' => ['<p>MA</p><p style="break-before: column">MB</p>', ['MA' => 1, 'MB' => 1]],
40+
'break-after: column on a table' => ['<table style="break-after: column"><tr><td>MA</td></tr></table><p>MB</p>', ['MA' => 1, 'MB' => 1]],
41+
'break-after: avoid on a table' => ['<table style="break-after: avoid"><tr><td>MA</td></tr></table><p>MB</p>', ['MA' => 1, 'MB' => 1]],
42+
'page-break-after: auto on a table' => ['<table style="page-break-after: auto"><tr><td>MA</td></tr></table><p>MB</p>', ['MA' => 1, 'MB' => 1]],
43+
];
44+
}
45+
46+
/**
47+
* A block that would be split over two pages moves whole to the second when breaks inside it are avoided
48+
*
49+
* @dataProvider providerBreakInside
50+
*/
51+
public function testBreakInside($style, $pages)
52+
{
53+
$html = str_repeat('<p>Filler</p>', 26) . '<div style="' . $style . '"><p>MA</p><p>MB</p><p>MC</p></div>';
54+
55+
$this->assertSame($pages, $this->pagesOf($html));
56+
}
57+
58+
/**
59+
* The values of break-inside, against a block with none
60+
*
61+
* @return array
62+
*/
63+
public function providerBreakInside()
64+
{
65+
return [
66+
'no break-inside' => ['', ['MA' => 1, 'MB' => 2, 'MC' => 2]],
67+
'avoid' => ['break-inside: avoid', ['MA' => 2, 'MB' => 2, 'MC' => 2]],
68+
'avoid-page' => ['break-inside: avoid-page', ['MA' => 2, 'MB' => 2, 'MC' => 2]],
69+
'avoid-column' => ['break-inside: avoid-column', ['MA' => 1, 'MB' => 2, 'MC' => 2]],
70+
];
71+
}
72+
73+
/**
74+
* A break value that forces no page, on a block inside a bordered block, draws the page as if it were not there
75+
* rather than closing and reopening the block around it
76+
*
77+
* @dataProvider providerUnforcedBreak
78+
*/
79+
public function testUnforcedBreakBeforeLeavesTheBlockAlone($style)
80+
{
81+
$page = function ($style) {
82+
$mpdf = new Mpdf(['compress' => false]);
83+
$mpdf->WriteHTML('<div style="border: 1mm solid red; padding: 5mm"><p>One</p><p style="' . $style . '">Two</p></div>');
84+
$mpdf->Close();
85+
86+
return $mpdf->pages[1];
87+
};
88+
89+
$this->assertSame($page(''), $page($style));
90+
}
91+
92+
/**
93+
* Values of break-before and page-break-before that do not force a page
94+
*
95+
* @return array
96+
*/
97+
public function providerUnforcedBreak()
98+
{
99+
return [
100+
['break-before: auto'],
101+
['break-before: avoid'],
102+
['break-before: avoid-page'],
103+
['page-break-before: auto'],
104+
['page-break-before: avoid'],
105+
];
106+
}
107+
108+
/**
109+
* Render a document and find the page each piece of text starting with "M" is drawn on
110+
*
111+
* @param string $html
112+
* @param array $config
113+
* @return int[] The page of each marker, keyed by the marker
114+
*/
115+
private function pagesOf($html, array $config = [])
116+
{
117+
$mpdf = new TextRecordingMpdf($config);
118+
$mpdf->WriteHTML($html);
119+
120+
$pages = [];
121+
foreach ($mpdf->drawnText as $i => $text) {
122+
if (preg_match('/^M[A-Z]$/', trim($text))) {
123+
$pages[trim($text)] = $mpdf->drawnBoxes[$i][0];
124+
}
125+
}
126+
127+
return $pages;
128+
}
129+
130+
}

‎tests/Mpdf/Css/NormalizePropertiesTest.php‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,51 @@ public function providerInvalidPageSizeName()
379379
];
380380
}
381381

382+
/**
383+
* break-before, break-after and break-inside are read as the page-break-* property of the same name
384+
*
385+
* @dataProvider providerBreakProperty
386+
*/
387+
public function testBreakPropertyIsReadAsPageBreak($value, $expected)
388+
{
389+
foreach (['BEFORE', 'AFTER', 'INSIDE'] as $side) {
390+
$this->assertSame(['PAGE-BREAK-' . $side => $expected], $this->normalizeProperties->normalize(['BREAK-' . $side => $value]));
391+
}
392+
}
393+
394+
/**
395+
* Each break value and the page-break-* value it becomes
396+
*
397+
* @return array
398+
*/
399+
public function providerBreakProperty()
400+
{
401+
return [
402+
['auto', 'auto'],
403+
['avoid', 'avoid'],
404+
['avoid-page', 'avoid'],
405+
['page', 'always'],
406+
['left', 'left'],
407+
['right', 'right'],
408+
['recto', 'right'],
409+
['verso', 'left'],
410+
['column', 'auto'],
411+
['avoid-column', 'auto'],
412+
['region', 'auto'],
413+
['avoid-region', 'auto'],
414+
['PAGE', 'always'],
415+
];
416+
}
417+
418+
/**
419+
* A break value that is not a CSS one is dropped
420+
*/
421+
public function testUnknownBreakValueIsDropped()
422+
{
423+
$this->assertSame([], $this->normalizeProperties->normalize(['BREAK-BEFORE' => 'always']));
424+
$this->assertSame([], $this->normalizeProperties->normalize(['BREAK-AFTER' => 'sideways']));
425+
}
426+
382427
/**
383428
* Keywords and lengths are read as they were before page-size names
384429
*/
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Snapshots;
4+
5+
/**
6+
* The break-before, break-after and break-inside properties, read as their page-break-* equivalents, and break values
7+
* that force no page leaving the blocks around them alone.
8+
*
9+
* @group snapshot
10+
*/
11+
class BreakPropertiesSnapshotTest extends Snapshot
12+
{
13+
14+
/**
15+
* The fixture's name
16+
*
17+
* @return string
18+
*/
19+
public function getId()
20+
{
21+
return 'break-properties';
22+
}
23+
24+
/**
25+
* Sections each starting on the page their break names, under a caption saying what should be seen
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+
div.case { border: 0.3mm solid #c0392b; padding: 2mm; margin-bottom: 4mm; }
36+
div.case p { margin: 1mm 0; }
37+
div.keep { border: 0.3mm solid #1f3a93; background-color: #eaf2fb; padding: 2mm; }
38+
div.keep p { margin: 1mm 0; }
39+
p.filler { margin: 0 0 3mm 0; color: #909090; }
40+
</style>
41+
42+
<h2>Page 1: break-after</h2>
43+
<p class="caption">Each red box has one paragraph with a break value that forces no page. Each box is drawn once, with both paragraphs inside it.</p>
44+
<div class="case"><p>First paragraph</p><p style="break-before: auto">Second paragraph, break-before: auto</p></div>
45+
<div class="case"><p>First paragraph</p><p style="break-before: avoid">Second paragraph, break-before: avoid</p></div>
46+
<div class="case"><p>First paragraph</p><p style="break-before: column">Second paragraph, break-before: column</p></div>
47+
<p style="break-after: page">This paragraph has break-after: page, so the next section starts on page 2.</p>
48+
49+
<h2>Page 2: break-before: recto</h2>
50+
<p class="caption">Margins are mirrored. The next heading has break-before: recto, so it starts on the next odd page, page 3, and page 2 is otherwise empty.</p>
51+
52+
<h2 style="break-before: recto">Page 3: break-before: verso</h2>
53+
<p class="caption">The next heading has break-before: verso, so it starts on the next even page, page 4.</p>
54+
55+
<h2 style="break-before: verso">Page 4: break-inside: avoid-page</h2>
56+
<p class="caption">The blue box after the grey lines has break-inside: avoid-page. It would start at the foot of this page, so it is moved whole to page 5.</p>
57+
<?php for ($i = 1; $i <= 32; $i++) { ?>
58+
<p class="filler">Filler line <?php echo $i; ?></p>
59+
<?php } ?>
60+
<div class="keep" style="break-inside: avoid-page">
61+
<h2>Page 5: the kept box</h2>
62+
<?php for ($i = 1; $i <= 6; $i++) { ?>
63+
<p>Line <?php echo $i; ?> of the box, all on page 5.</p>
64+
<?php } ?>
65+
</div>
66+
<?php
67+
$html = ob_get_clean();
68+
69+
$this->mpdf = $this->createMpdf(['mirrorMargins' => true]);
70+
71+
$this->mpdf->WriteHTML($html);
72+
}
73+
74+
}
38.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)