PDFPipe

Numbers, tables and totals / Quantity and unit columns

Quantity, unit and unit-price columns that do not wrap

The three narrow columns between the description and the amount, which are the first things to break when a description gets long or a unit of measure gets spelled out.

Why this block is harder than it looks

These columns are narrow by design and their content is more variable than it looks. A quantity is usually one or two characters and occasionally 12,500. A unit is usually two characters and occasionally hours at weekend rate. A unit price is usually four digits and occasionally carries four decimal places because the item is priced per thousand. Under automatic table layout each of those exceptions steals width from the description column for every row in the document. Under fixed layout they wrap to two lines and drag the row height with 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.

  • Abbreviate units to a fixed vocabulary and put the long form in a legend or a footnote. A unit column is not the place for prose.
  • Set white-space: nowrap on all three and accept a slight overflow rather than a wrap. A number that overflows a little is legible, a number split across two lines is not a number.
  • Decide the decimal places per document rather than per line. Mixing 12.00 and 12.0450 in one column makes both harder to read even with tabular figures.
  • Keep quantity and unit in separate cells rather than concatenating them, so quantities align on their digits and units align on their left edge.
  • Size the columns from the widest value the data can produce, which you can measure, rather than from the widest value in the sample, which you cannot trust.
  • Where a unit price genuinely needs four decimals, set the extra precision a size smaller on the same baseline rather than widening the column for every other row.

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
<tr>
  <td class="desc">Cold-rolled sheet, 2mm, primed</td>
  <td class="qty">1,250</td>
  <td class="unit">kg</td>
  <td class="price">2.4050</td>
  <td class="amt">3,006.25</td>
</tr>

<style>
  .desc  { width: 46%; }
  .qty   { width: 11%; text-align: right; }
  .unit  { width:  7%; text-align: left; padding-left: 4pt; }
  .price { width: 16%; text-align: right; }
  .amt   { width: 16%; text-align: right; }

  .qty, .price, .amt { font-variant-numeric: tabular-nums; }
  .qty, .unit, .price, .amt { white-space: nowrap; }  /* overflow, never wrap */
</style>

What breaks when it is built the obvious way

Leaving table-layout on auto because the sample data fitted. Automatic layout is a whole-table measurement, so one row on page four with a long unit label narrows the description column on page one. The document looks right until somebody orders something with an awkward unit, and then every page changes.

How to prove it survived pagination

Render a row carrying your longest possible unit label, largest quantity and most decimal places all at once, even if that combination cannot occur in real data. If it survives, every real row will. Then confirm the description column is the same width as in a document without that row.

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 purchase order 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.