Put Coffee below Almond croissant with apricot jam on a narrow receipt. The croissant name wraps onto a second line. If the price column advances once per item, the coffee price ends up next to apricot jam.

Repceipt's generator draws names and prices in separate text blocks. The two-column format is a familiar pattern on paper receipts, as shown in Repceipt's receipt design notes.

Reserve space for the price

The 58 mm preset gives the text 30 columns. The 80 mm preset gives it 42. Dividing the width between the margins by the column count gives the space available for each character.

The generator sizes its monospaced fonts to fit that space. For example, a font with an advance of 0.6em moves 12 pixels from one character to the next at a font size of 20 pixels. The generator divides the target character width by the font's advance in ems to get the font size.

The amount gets formatted before the label wraps. $6.50 takes up five columns. Leave one column between the label and the amount, and a 30-column receipt has 24 columns left for the label. With the bundled Thermal font, the formatter writes 6,50 EUR. That takes eight columns and leaves 21 for the label.

Give the columns matching line breaks

The layout function works through pairs of labels and formatted amounts. wrapText packs whole words onto each line and splits any word longer than the available space.

const leftLines: string[] = []; const rightLines: string[] = []; for (const [label, amount] of rows) { const labelWidth = amount ? columns - amount.length - 1 : columns; const lines = wrapText(label, labelWidth); lines.forEach((line, index) => { leftLines.push(line); rightLines.push(index === 0 ? amount : ''); }); } 

On the 30-column preset, the two items take up three lines.

Almond croissant with $6.50 apricot jam Coffee $3.00 

The price column contains ['$6.50', '', '$3.00']. That empty entry puts the coffee price on the third line, where the coffee label is.

Use the same layout for the preview and export

The generator joins each list with \n and creates two text blocks. Both start at the same position, span the printable width, and use the same line height. The labels align left, and the amounts align right. The preview and the export renderer read the blocks from the same document, so both use the line breaks the generator chose.

On the 42-column preset, the croissant name fits on a single line. Changing the paper setting runs the layout again, and the sections below the items move to match the new height.

The regression test gives a long item name to the 58 mm preset. It checks that both blocks start at the same y coordinate and that the price column gets a blank entry for every continuation line. A quick visual test is to add a short item right after the long one. A single item can look fine with a missing blank line. The next price gives it away.