Skip to content

Format numbers, amounts, dates and addresses as a country writes them - #370

Closed
jakejackson1 wants to merge 1 commit into
einvoice/2-invoice-modelfrom
einvoice/3-formatter
Closed

jakejackson1 wants to merge 1 commit into
einvoice/2-invoice-modelfrom
einvoice/3-formatter

Conversation

@jakejackson1

@jakejackson1 jakejackson1 commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Third of four stacked PRs replacing #327. It's based on #369, so this diff shows only its own changes.

Summary

A printed invoice has to write its amounts, rates, dates and addresses the way its reader's country does: 1.021,11 € in Germany, $1,021.11 in the US, and New York, NY 10118 but 75002 Paris. This PR adds Formatter, which does that, and presets for 21 conventions. The HTML writer in #371 uses it, and it works just as well on the model's getters for a layout of your own.

A Formatter must be given a preset, because there's no default convention:

use Mpdf\Invoice\Formatter;
use Mpdf\Invoice\Preset\GermanyPreset;

$format = new Formatter(new GermanyPreset());
$format->money(1021.11, 'EUR');                // 1.021,11 €
$format->percent(19);                          // 19 %
$format->date($date = new DateTime('2026-09-23')); // 23.09.2026
$format->withLongDates()->date($date);         // 23. September 2026
$format->address($party);                      // ['Hauptstraße 1', '10115 Berlin', 'DE']

Presets (Mpdf\Invoice\Preset)

Preset Amount Rate Date
UnitedStatesPreset $1,021.11 5.5% 09/23/2026
CanadaEnglishPreset $1,021.11 5.5% 2026-09-23
CanadaQuebecPreset 1 021,11 $ 5,5 % 2026-09-23
AustraliaPreset $1,021.11 5.5% 23/09/2026
NewZealandPreset $1,021.11 5.5% 23/09/2026
UnitedKingdomPreset £1,021.11 5.5% 23/09/2026
ChinaPreset ¥1,021.11 5.5% 2026-09-23
JapanPreset ¥1,021 5.5% 2026/09/23
IndiaPreset ₹12,34,567.25 5.5% 23/09/2026
GermanyPreset 1.021,11 € 5,5 % 23.09.2026
FrancePreset 1 021,11 € 5,5 % 23/09/2026
ItalyPreset 1.021,11 € 5,5% 23/09/2026
SpainPreset 1021,11 € (10.211,11 € from five digits) 5,5 % 23/09/2026
PolandPreset 1021,11 zł (10 211,11 zł from five digits) 5,5% 23.09.2026
RomaniaPreset 1.021,11 lei 5,5 % 23.09.2026
NetherlandsPreset € 1.021,11 5,5% 23-09-2026
BelgiumDutchPreset € 1.021,11 5,5% 23/09/2026
BelgiumFrenchPreset 1.021,11 € 5,5 % 23/09/2026
CzechiaPreset 1 021,11 Kč 5,5 % 23.09.2026
PortugalPreset 1021,11 € (10 211,11 € from five digits) 5,5% 23/09/2026
SwedenPreset 1 021,11 kr 5,5 % 2026-09-23

These are the ten most populous EU countries plus Sweden, the UK, the US, Canada, Australia, New Zealand, China, Japan and India. A country with two conventions has a preset for each. They follow CLDR, and their spaces are no-break spaces.

  • Other currencies: a preset formats its own country's currency, and writes any other with its code after the amount, e.g. 1,021.11 EUR under UnitedStatesPreset.

  • Decimals:

    • An amount has at least its currency's minor unit: two decimals, or none for the yen and other currencies without one.
    • It keeps up to four decimals when it has more, so a unit price of 0.125 prints as £0.125, the same figure the XML carries.
  • Sign: the minus sign goes before any symbol: -$100.00.

  • Dates in words:

    • Each preset has its language's month names in the form a date uses, e.g. the genitive in Polish and Czech.
    • It also has its written-out date, which withLongDates() switches to: 23 de septiembre de 2026, September 23, 2026, 2026年9月23日.
    • Any date format can use {month}: withDateFormat('jS {month} Y') gives 1st January 2020.
    • Chinese and Japanese dates need a CJK font configured in mPDF.
  • Adjusting: withDateFormat(), withLongDates(), withCurrencyFormat(), withPercentFormat() and withAddressFormat() each return an adjusted copy:

    $formatter = (new Formatter(new GermanyPreset()))->withCurrencyFormat('GBP', '£%s'); // £1.021,11
  • Another country: extend AbstractPreset, whose settings are properties.

    • A preset must set its date format, which has no default.
    • Everything else defaults to a decimal point, commas between groups of three digits and 5.5%.
    • Or implement PresetInterface from scratch.
    class SwitzerlandGermanPreset extends AbstractPreset
    {
    	protected $thousandsSeparator = '’';
    	protected $dateFormat = 'd.m.Y';
    	protected $currencyFormats = ['CHF' => 'CHF' . self::NBSP . '%s']; // CHF 1’234’567.50
    }

Addresses

Formatter::address() lays out an address the way the party's country does, whichever preset the formatter uses:

Country Layout after the street lines
US New York, NY 10118
Canada, Australia Toronto ON M5V 2T6
New Zealand Auckland 1010
United Kingdom London, then SW1A 2AA on its own line
India Mumbai 400020, then Maharashtra on its own line
China, Japan (Latin script) Chiyoda-ku, Tokyo 100-0001
everywhere else 75002 Paris, with any province after: 00144 Roma RM
  • Country: each address ends with the country code.
  • Missing parts: they're taken out of the layout before it's filled, so their gaps and commas close up while the party's own text is left exactly as given.
  • Your own layout: withAddressFormat('BR', ['{street}', '{additional}', '{city} - {subdivision}', '{postcode}', '{country}']) adds or replaces a country's layout.

Your own printed layout

Every value on the invoice has a getter, and so do its totals. Pass them through a Formatter:

$format = new Formatter(new GermanyPreset());
$currency = $invoice->getCurrency();

$html = '<h2>Rechnung ' . htmlspecialchars($invoice->getId()) . ' vom ' . $format->date($invoice->getIssueDate()) . '</h2>';
$html .= '<p>' . implode('<br>', array_map('htmlspecialchars', $format->address($invoice->getBuyer()))) . '</p>';
foreach ($invoice->getLines() as $line) {
	$html .= '<p>' . htmlspecialchars($line->getName()) . ': ' . $format->money($line->getNetAmount(), $currency) . '</p>';
}
$html .= '<p><b>Summe: ' . $format->money($invoice->getGrandTotal(), $currency) . '</b></p>';

$mpdf->WriteHTML($html);
$mpdf->SetEmbeddedInvoice(new FacturX((new CiiInvoiceWriter(FacturX::EN16931))->write($invoice)));

The printed totals match the embedded XML, because both come from the same calculation.

Verification

  • FormatterTest:
    • every preset's number, amount, refund, rate, date and written-out date;
    • digit grouping, including India's and the five-digit rule;
    • other currencies, and the decimals an amount keeps;
    • each country's address layout, and that the party's own text is left alone;
    • the with…() copies;
    • refusing a preset without a date format.
  • The layout example above: a PDF built from it passes veraPDF (--flavour 3b).
  • Gates:
    • The full suite, composer cs and PHPStan pass.
    • The changed files lint clean under PHP 5.6.

🤖 Generated with Claude Code

A Formatter writes a document's numbers, amounts, rates and dates by the
convention of a country's preset, and lays out each party's address as
that party's country does:
- 21 presets following CLDR: the United States, English Canada and
  Quebec, Australia, New Zealand, the United Kingdom, China, Japan, India,
  and the ten most populous EU countries plus Sweden, with Dutch and
  French Belgium. A preset is AbstractPreset's properties, overridden where
  its country differs; PresetInterface is there for any other.
- India's lakh grouping, the whole four-digit numbers of Spain, Poland and
  Portugal, currencies without a minor unit, and each language's month
  names for dates written out.
- withDateFormat(), withLongDates(), withCurrencyFormat(),
  withPercentFormat() and withAddressFormat() adjust a copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jakejackson1

Copy link
Copy Markdown
Member Author

Superseded by #513, the same Formatter rebased onto gravitypdf without #369's model: Formatter::address() now takes an array of the address's parts instead of a Party.

@jakejackson1
jakejackson1 deleted the einvoice/3-formatter 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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant