Skip to content

Let the :first, :left and :right pages set the side margins text flows into (#476) - #510

Merged
jakejackson1 merged 1 commit into
gravitypdffrom
feature/476-page-frame-per-page
Sep 29, 2026
Merged

jakejackson1 merged 1 commit into
gravitypdffrom
feature/476-page-frame-per-page

Conversation

@jakejackson1

@jakejackson1 jakejackson1 commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Closes #476.

SetPagedMediaCSS() dropped margin-left and margin-right from every :first, :left and :right page, so every page of a named page had the same page area: the space between its side margins, where text is set. This PR keeps those margins, so text flowing from page to page is set in the page area of each page. For example, a story can start in a narrow page area under its title on the first page and carry on at full width on the pages after it.

@page story :first { margin-left: 100mm; }
@page story :left { margin-left: 20mm; margin-right: 20mm; }
@page story :right { margin-left: 20mm; margin-right: 20mm; }

Mirroring. A side margin on a pseudo page is the margin of that side of the physical page, as in CSS Paged Media. With mirrorMargins it is not swapped on an even page. pseudoPageProperties() passes it in pre-swapped so the swap in pageSideMargins() puts it back. A side the pseudo page leaves unset keeps the plain @page margin, mirrored as before. Documents that set no side margins on pseudo pages are unchanged: every snapshot is byte-identical.

Text carried over a page break

  • AddPage() records the page area text leaves from, and moveIntoPageArea() moves the text into the page area of the new page:
    • MarginCorrection becomes how far the page area moved.
    • Open blocks are widened or narrowed to match. A block with a set width keeps it.
  • SetColumns() lays columns out from the page's physical margins through pageSideMargins(). ResetMargins() now uses the same helper, and SetCol() no longer adds the even-page correction. Both ask marginsForcedPortrait() whether a landscape page keeps the portrait margins.
  • Where the page areas of a flow differ, turnPageAheadOfLine() turns the page before a line that can't fit is filled, so the line is measured against the page area it is set in. It is gated by CssManager::pseudoPagesSetSideMargins(), and it only fires where a break was already certain.
  • The four copies of the "turn the page and keep your place" block (in Cell, finishFlowingBlock, WriteFlowingBlock and the new look-ahead) are now one helper, turnPageKeepingPlace().

Where the code lives

  • The page area methods (pageSideMargins(), marginsForcedPortrait(), pageArea(), moveIntoPageArea(), turnPageKeepingPlace() and turnPageAheadOfLine()) are in a new PageAreas trait that Mpdf uses. They read and write Mpdf's layout state, so they stay part of it, in a file of their own. The PRs stacked on this one add their methods there too.
  • The two @page lookups read only the style sheet, so they are on CssManager: pseudoPageProperties(), and pseudoPagesSetSideMargins(), which says whether a pseudo page of the plain or current named page sets a side margin.

Not in this PR

  • column-count in @page (the other option in Let text that flows onto a new page take that page's column count and frame #476). Every page-turn site computes the new x as MarginCorrection + ChangeColumn * (ColWidth + ColGap), and block x0 isn't kept up to date inside columns. Changing the count mid-flow needs deeper work in the column code.
  • Tables keep the width they started with when they run into a page with a different page area.
  • The look-ahead uses the minimum line height, so a line made taller by an inline image still breaks at print time.

Tests

  • PseudoPageAreaTest checks the x span of every line drawn on each page:
    • named :first against later pages
    • :left/:right margins as written
    • one side set and the other mirrored
    • the unchanged mirrored case
    • unnamed :first
    • a fixed-width div
    • two columns re-laid out on each page
  • TextRecordingMpdf also records each drawn line's page and edges.
  • CssManagerTest covers the two lookups without rendering: which pseudo pages apply, the sheet size left out, the side margins swapped on an even page, and which rules count as setting a side margin.
  • New snapshot pseudo-page-areas: a story on a named page, set between 100 and 195mm on its first page, 15 and 180mm on left pages and 30 and 195mm on right pages, with two columns after it. On gravitypdf every page differs, and the document is a page shorter.
  • composer test passes (4441 tests, 31 skipped). phpcs and PHPStan are clean.

CHANGELOG-GRAVITYPDF.md gets a New features entry under a new Paged media heading.

I also checked it against a 48-page magazine template. The template renders pixel-identical on this branch, and one article's opening and body pages, previously split with an explicit <pagebreak>, can now be one flow.

Text beside a float that runs over pages of different widths is fixed separately in #549, which is stacked on this one.

🤖 Generated with Claude Code

…s into (#476)

SetPagedMediaCSS() dropped margin-left and margin-right from every pseudo page,
so all pages of a named page shared one page area width. They are now kept. A
side margin on a pseudo page is the margin of that side of the physical page:
with mirrorMargins it is not swapped on an even page, while a side it leaves
unset keeps the mirrored margin of the plain @page rule.

Text carrying on over an automatic page break moves into the page area of the
new page: MarginCorrection becomes how far the page area moved rather than the
mirrored difference, open blocks are widened or narrowed with the page area (a
block with a set width keeps it), and columns are laid out from the physical
margins of each page. When the page areas of a flow differ, a line that cannot
fit turns the page before it is filled, so it is measured against the page area
it is set in.

The page area methods are in a PageAreas trait that Mpdf uses, as they read and
write its layout state. The @page rule lookups read only the style sheet, so
they are on CssManager and tested there without a document.

The pseudo-page-areas snapshot sets a story over :first, :left and :right pages
with their own side margins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jakejackson1
jakejackson1 force-pushed the feature/476-page-frame-per-page branch from 623dc33 to c0d1f83 Compare September 29, 2026 03:13
@jakejackson1
jakejackson1 merged commit 1b454f9 into gravitypdf Sep 29, 2026
32 checks passed
@jakejackson1
jakejackson1 deleted the feature/476-page-frame-per-page branch September 30, 2026 01:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working create-upstream-pr

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Let text that flows onto a new page take that page's column count and frame

1 participant