PDFPipe

Numbers, tables and totals / Carried-forward subtotal

Carried forward and brought forward totals on a long table

The running figure printed at the foot of each page and repeated at the top of the next, so a multi-page table can be checked page by page instead of only at the end.

Why this block is harder than it looks

This is the one block in the cluster that a stylesheet cannot produce. Carried forward is a sum of exactly the rows that landed on this page, and which rows land on which page is decided during pagination, after the CSS has run. Nothing in CSS can read that. So either the block is generated by paginating in your own code before rendering, which means committing to a row height, or it is not done at all. Most documents that appear to have it are paginated server side into fixed-size chunks.

The decisions that make it work

Reasons rather than a description of the code. Each of these is a choice that has a wrong answer, and the wrong answer is usually the default.

  • Decide the rows-per-page number yourself and chunk the data in code. This is the only reliable route, and it means the table is really N tables, one per page, each followed by a forced break.
  • Fix the row height so the chunk size is predictable. A description that wraps to two lines breaks the arithmetic of a fixed chunk, so either clamp the description to one line or accept a shorter chunk and leave slack at the bottom.
  • Print carried forward at the foot of every page except the last, and brought forward at the head of every page except the first. Printing both on the same page reads as an error.
  • Make the last page's foot the real total rather than another carried forward, and label it differently so the reader can see the document ended.
  • Use tfoot for the carried-forward row while you are keeping one table per page, so it sits at the bottom of that table without absolute positioning.
  • If you cannot chunk in code, do not fake it. A carried-forward figure that is wrong is far worse than one that is absent, because it is the number a reader uses to check the others.

The fragment

Markup and the CSS it needs, and nothing else. It expects a document that already has a page rule and a base stylesheet, so paste it into one rather than opening it on its own.

html
<!-- One table per page, produced by chunking the rows in code. -->
<table class="ledger">
  <thead>
    <tr><th>Date</th><th>Detail</th><th class="num">Amount</th></tr>
    <tr class="bf"><td colspan="2">Brought forward</td><td class="num">12,480.00</td></tr>
  </thead>
  <tbody>
    <tr><td>04 Mar</td><td>Invoice 2026-118</td><td class="num">1,240.00</td></tr>
    <!-- exactly as many rows as fit -->
  </tbody>
  <tfoot>
    <tr class="cf"><td colspan="2">Carried forward</td><td class="num">18,320.00</td></tr>
  </tfoot>
</table>
<div class="page-break"></div>

<style>
  .ledger { width: 100%; border-collapse: collapse; table-layout: fixed; }
  .ledger td, .ledger th { padding: 4pt 3pt; }
  .ledger .num { text-align: right; font-variant-numeric: tabular-nums; }
  .ledger .bf td, .ledger .cf td {
    border-top: 0.5pt solid currentColor;
    font-style: italic;
  }
  .ledger tbody tr { height: 16pt; }     /* fixed, so chunking is predictable */
  .page-break { break-after: page; }
</style>

What breaks when it is built the obvious way

Trying to do it with a CSS counter. Counters increment in document order and know nothing about pages, so the figure at the foot of page two is the sum of everything so far rather than the sum of that page. It happens to be correct on page one, which is why the bug ships.

How to prove it survived pagination

Take the brought-forward figure on page three and subtract page two's carried forward: the difference should be zero. Then add the rows visible on page three to the brought-forward figure and confirm it equals page three's carried forward. Both checks, on a document with descriptions long enough to wrap.

Where it sits in a finished document

This page is one block. The document it belongs to has a page rule, a base stylesheet, a header and everything else around it, and repeating all of that here would make thirty pages that say the same thing. The statement of account template is a complete file with this block already in it, so take that and change the fragment rather than assembling one from parts. The property doing most of the work here is covered on its own page, with the support caveats that belong there rather than here.

Frequently asked

Will this fragment work on its own?

Not as a whole document. It has no page rule, no margins and no base type, because those belong to the document rather than to the block, and duplicating them in every fragment would mean thirty copies to keep in step. Paste it into a template that already has them.

Why does it look right in a browser and wrong in the PDF?

Because a browser window is one continuous surface and a document is a stack of fixed rectangles. Nothing in a scrolling view exercises a page boundary, so every break rule in the fragment is inert until the content is paginated. Render the real thing with enough content to cross two or three boundaries, then look.

Do I need a special option on the render request for this?

No. Everything on this page is CSS and markup, which is your side of the boundary. What the render has to give you is a real page size with real margins, and after that the layout is decided by the stylesheet.

Other parts of a document

The blocks that sit next to this one, and one from the next group along so you are not sealed inside a single kind of problem.

Paste the fragment into the playground inside a page rule and see what it does at a real page size. That is the only way any of this gets confirmed.