PDFPipe

Numbers, tables and totals / Tax breakdown by rate

A tax breakdown table with one row per rate that stays together

A small table showing taxable amount and tax for each distinct rate in the document, which exists because a single tax line stops being enough as soon as an invoice mixes rates.

Why this block is harder than it looks

The number of rows is data-dependent in a way the totals block is not. A document with one rate has one row and could arguably skip the table. A document with a standard rate, a reduced rate and a zero-rated line has three, and the block is now tall enough to break. Worse, the rows come from grouping the line items, so it is easy to end up with a table that disagrees with the totals block beside it because the two were computed from different places in the code. On paper those figures are twelve millimetres apart and every reader compares them.

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.

  • Compute the breakdown and the totals from the same grouped structure, in one pass, so they cannot disagree. Two separate loops over the line items will eventually round differently.
  • One row per distinct rate present in this document, not one row per rate your system supports. An invoice showing a reduced-rate line with a zero in it invites a phone call.
  • Round per rate group rather than per line. Which rounding rule is correct is a matter for whoever owns the tax rules where the document is issued, but the display has to match whichever was used, so print the rounded group figures rather than recomputing for display.
  • Show the rate in its own column rather than folding it into the label, so the rates read as a column when there are three of them.
  • Put break-inside: avoid on the table. It is short enough that keeping it whole is always possible and always better.
  • Zero-rated and exempt are different things and they print differently. If the document distinguishes them, the breakdown has to as well, and a footnote marker is the usual way to do it without widening the table.

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
<table class="tax-breakdown">
  <thead>
    <tr><th>Rate</th><th class="num">Net</th><th class="num">Tax</th></tr>
  </thead>
  <tbody>
    <tr><td>20%</td><td class="num">3,400.00</td><td class="num">680.00</td></tr>
    <tr><td>5%</td><td class="num">600.00</td><td class="num">30.00</td></tr>
    <tr><td>0%</td><td class="num">180.00</td><td class="num">0.00</td></tr>
  </tbody>
</table>

<style>
  .tax-breakdown {
    break-inside: avoid;
    border-collapse: collapse;
    margin-top: 10pt;
    font-size: 9pt;
  }
  .tax-breakdown th,
  .tax-breakdown td { padding: 3pt 10pt 3pt 0; }
  .tax-breakdown .num {
    text-align: right;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
  }
  .tax-breakdown thead th { border-bottom: 0.5pt solid currentColor; }
</style>

What breaks when it is built the obvious way

Summing the displayed, already-rounded per-line tax figures to produce the group total. Every line rounds independently, the errors accumulate in the same direction more often than not, and on a fifty-line invoice the breakdown ends up a unit or two away from the total sitting beside it. Group first, then round, then display what you rounded.

How to prove it survived pagination

Build a test document with three rates and at least one line whose tax rounds up and one whose tax rounds down, then read the breakdown against the totals block by eye. They agree or they do not, and there is no third outcome.

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 eu vat invoice 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.