@facturion/invoice-renderer 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/NOTICE +20 -0
- package/README.md +43 -0
- package/index.d.ts +33 -0
- package/package.json +55 -0
- package/src/document.js +43 -0
- package/src/i18n.js +140 -0
- package/src/index.js +6 -0
- package/src/render.js +421 -0
- package/styles/utilities.css +227 -0
- package/styles/variables.css +89 -0
- package/styles/view.css +902 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nicola Crivellin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
@facturion/invoice-renderer
|
|
2
|
+
===========================
|
|
3
|
+
|
|
4
|
+
This package is licensed under the MIT License (see LICENSE).
|
|
5
|
+
|
|
6
|
+
The renderer, layout, stylesheet, and the `view.*` document strings are the
|
|
7
|
+
original work of this package. It builds on:
|
|
8
|
+
|
|
9
|
+
--------------------------------------------------------------------------------
|
|
10
|
+
@facturion/invoice
|
|
11
|
+
The simplified-invoice JSON data model, validation, and the net/total
|
|
12
|
+
computation helpers (lineNet, computeTotals).
|
|
13
|
+
|
|
14
|
+
--------------------------------------------------------------------------------
|
|
15
|
+
@facturion/codelists
|
|
16
|
+
Friendly multilingual labels for unit-of-measure (UN/ECE Rec 20/21) and
|
|
17
|
+
payment-means (UNTDID 4461) codes referenced by the rendered document.
|
|
18
|
+
|
|
19
|
+
The EN 16931 business terms the layout reflects are defined by CEN
|
|
20
|
+
(EN 16931-1).
|
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# @facturion/invoice-renderer
|
|
2
|
+
|
|
3
|
+
Render the [`@facturion/invoice`](https://www.npmjs.com/package/@facturion/invoice)
|
|
4
|
+
simplified-JSON model to a clean, human-readable **HTML invoice** — the
|
|
5
|
+
presentation layer behind Facturion's invoice PDFs (feed the HTML to headless
|
|
6
|
+
Chromium or any HTML→PDF tool).
|
|
7
|
+
|
|
8
|
+
It owns presentation only: layout, the document stylesheet, and the `view.*`
|
|
9
|
+
document strings. The data model and money math come from `@facturion/invoice`;
|
|
10
|
+
unit and payment-means labels come from `@facturion/codelists`.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npm install @facturion/invoice-renderer
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { renderInvoiceDocument, renderInvoice, makeT } from "@facturion/invoice-renderer";
|
|
22
|
+
|
|
23
|
+
// Batteries-included: a standalone HTML document (stylesheet inlined, labels resolved).
|
|
24
|
+
const html = renderInvoiceDocument(invoice, { lang: "de" });
|
|
25
|
+
|
|
26
|
+
// Or just the fragment, with your own label resolver:
|
|
27
|
+
const fragment = renderInvoice(invoice, { t: makeT("en") });
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
- **`renderInvoiceDocument(invoice, { lang?, t? })`** → full `<!DOCTYPE html>` document.
|
|
31
|
+
- **`renderInvoice(invoice, { t })`** → HTML fragment; `t(key, vars?)` resolves
|
|
32
|
+
`view.*` / `units.*` / `paymentMeans.*` and returns the key on a miss. Use
|
|
33
|
+
`makeT(lang)` for the default resolver, or inject your own.
|
|
34
|
+
|
|
35
|
+
Partial invoices render fine (missing sections simply don't appear), so the
|
|
36
|
+
renderer suits live previews as well as final documents.
|
|
37
|
+
|
|
38
|
+
The bundled stylesheet is also importable directly, e.g.
|
|
39
|
+
`@facturion/invoice-renderer/styles/view.css`.
|
|
40
|
+
|
|
41
|
+
## License
|
|
42
|
+
|
|
43
|
+
MIT. See [LICENSE](./LICENSE) and [NOTICE](./NOTICE).
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Invoice, InvoiceTotals } from "@facturion/invoice";
|
|
2
|
+
|
|
3
|
+
export type Lang = "en" | "de";
|
|
4
|
+
|
|
5
|
+
/** Label resolver: `(key, vars?) => string`, returning the key unchanged on a miss. */
|
|
6
|
+
export type TFunction = (key: string, vars?: Record<string, unknown>) => string;
|
|
7
|
+
|
|
8
|
+
export { lineNet, computeTotals } from "@facturion/invoice";
|
|
9
|
+
|
|
10
|
+
/** HTML-escape a value (`&`, `<`, `>`, `"`). */
|
|
11
|
+
export function esc(s: unknown): string;
|
|
12
|
+
/** Format a number with exactly two fraction digits; non-numerics pass through escaped. */
|
|
13
|
+
export function amt(value: unknown): string;
|
|
14
|
+
/** `amt(value)` optionally suffixed with a currency code. */
|
|
15
|
+
export function curAmt(value: unknown, cur?: string): string;
|
|
16
|
+
|
|
17
|
+
/** Render the invoice as an HTML fragment (no document chrome). */
|
|
18
|
+
export function renderInvoice(invoice: Invoice, opts: { t: TFunction }): string;
|
|
19
|
+
|
|
20
|
+
/** Render a standalone HTML document (stylesheet inlined, default labels). */
|
|
21
|
+
export function renderInvoiceDocument(
|
|
22
|
+
invoice: Invoice,
|
|
23
|
+
opts?: { lang?: Lang; t?: TFunction },
|
|
24
|
+
): string;
|
|
25
|
+
|
|
26
|
+
/** Build the default label resolver for a language (bundled `view.*` + codelists). */
|
|
27
|
+
export function makeT(lang: Lang): TFunction;
|
|
28
|
+
|
|
29
|
+
/** Bundled `view.*` document strings, keyed by language. */
|
|
30
|
+
export const VIEW: Record<Lang, Record<string, string>>;
|
|
31
|
+
export const DEFAULT_LANG: Lang;
|
|
32
|
+
|
|
33
|
+
export type { Invoice, InvoiceTotals };
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@facturion/invoice-renderer",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Render the @facturion/invoice simplified-JSON model to a human-readable HTML invoice — the presentation layer behind Facturion's invoice PDFs.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"en16931",
|
|
7
|
+
"einvoicing",
|
|
8
|
+
"invoice",
|
|
9
|
+
"html",
|
|
10
|
+
"pdf",
|
|
11
|
+
"renderer",
|
|
12
|
+
"zugferd",
|
|
13
|
+
"xrechnung",
|
|
14
|
+
"factur-x"
|
|
15
|
+
],
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/facturion/packages.git",
|
|
20
|
+
"directory": "packages/invoice-renderer"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/facturion/packages/tree/main/packages/invoice-renderer#readme",
|
|
23
|
+
"bugs": "https://github.com/facturion/packages/issues",
|
|
24
|
+
"type": "module",
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./index.d.ts",
|
|
29
|
+
"default": "./src/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./styles/view.css": "./styles/view.css",
|
|
32
|
+
"./styles/utilities.css": "./styles/utilities.css",
|
|
33
|
+
"./styles/variables.css": "./styles/variables.css",
|
|
34
|
+
"./package.json": "./package.json"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"src",
|
|
38
|
+
"styles",
|
|
39
|
+
"index.d.ts",
|
|
40
|
+
"README.md",
|
|
41
|
+
"LICENSE",
|
|
42
|
+
"NOTICE"
|
|
43
|
+
],
|
|
44
|
+
"scripts": {
|
|
45
|
+
"test": "node --test test/*.test.js",
|
|
46
|
+
"prepublishOnly": "npm test"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@facturion/codelists": "^0.1.1",
|
|
50
|
+
"@facturion/invoice": "^0.1.0"
|
|
51
|
+
},
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": ">=20"
|
|
54
|
+
}
|
|
55
|
+
}
|
package/src/document.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Batteries-included wrapper: a standalone HTML document for an invoice, with
|
|
2
|
+
// the bundled stylesheet inlined and a default label resolver. This is what an
|
|
3
|
+
// HTML→PDF service (e.g. headless Chromium) consumes.
|
|
4
|
+
|
|
5
|
+
import { readFileSync } from "node:fs";
|
|
6
|
+
import { dirname, join } from "node:path";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
import { renderInvoice } from "./render.js";
|
|
9
|
+
import { makeT, DEFAULT_LANG } from "./i18n.js";
|
|
10
|
+
|
|
11
|
+
const _stylesDir = join(dirname(fileURLToPath(import.meta.url)), "..", "styles");
|
|
12
|
+
const _variablesCss = readFileSync(join(_stylesDir, "variables.css"), "utf-8");
|
|
13
|
+
const _utilitiesCss = readFileSync(join(_stylesDir, "utilities.css"), "utf-8");
|
|
14
|
+
const _viewCss = readFileSync(join(_stylesDir, "view.css"), "utf-8");
|
|
15
|
+
|
|
16
|
+
const _HTML_LANG = { en: "en", de: "de" };
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Build a standalone HTML document for an invoice.
|
|
20
|
+
*
|
|
21
|
+
* @param invoice Simplified EN 16931 JSON (see @facturion/invoice).
|
|
22
|
+
* @param opts.lang Language for document strings. Default "en".
|
|
23
|
+
* @param opts.t Override the label resolver (advanced; bypasses the bundled
|
|
24
|
+
* strings + codelists default).
|
|
25
|
+
*/
|
|
26
|
+
export function renderInvoiceDocument(invoice, { lang = DEFAULT_LANG, t } = {}) {
|
|
27
|
+
const resolve = t ?? makeT(lang);
|
|
28
|
+
const fragment = renderInvoice(invoice, { t: resolve });
|
|
29
|
+
const htmlLang = _HTML_LANG[lang] || DEFAULT_LANG;
|
|
30
|
+
return `<!DOCTYPE html>
|
|
31
|
+
<html lang="${htmlLang}">
|
|
32
|
+
<head>
|
|
33
|
+
<meta charset="utf-8">
|
|
34
|
+
<style>
|
|
35
|
+
${_variablesCss}
|
|
36
|
+
${_utilitiesCss}
|
|
37
|
+
${_viewCss}
|
|
38
|
+
body { margin: 0; padding: 0; background: #fff; }
|
|
39
|
+
</style>
|
|
40
|
+
</head>
|
|
41
|
+
<body>${fragment}</body>
|
|
42
|
+
</html>`;
|
|
43
|
+
}
|
package/src/i18n.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// Document-chrome strings for the rendered invoice (the `view.*` namespace) and
|
|
2
|
+
// the default label resolver. Vocabulary labels (`units.*`, `paymentMeans.*`)
|
|
3
|
+
// are not bundled here — they resolve through @facturion/codelists so the
|
|
4
|
+
// renderer and the rest of the toolchain share one source of truth.
|
|
5
|
+
|
|
6
|
+
import { codelistLabel, unitLabel } from "@facturion/codelists";
|
|
7
|
+
|
|
8
|
+
export const DEFAULT_LANG = "en";
|
|
9
|
+
|
|
10
|
+
// The document strings the renderer references via `t("view.<key>")`. Sourced
|
|
11
|
+
// from the Facturion app's invoice-view i18n; kept in sync deliberately.
|
|
12
|
+
export const VIEW = {
|
|
13
|
+
en: {
|
|
14
|
+
accountName: "Account holder",
|
|
15
|
+
bic: "BIC",
|
|
16
|
+
cardHolder: "Card holder",
|
|
17
|
+
cardNumber: "Card number",
|
|
18
|
+
contract: "Contract",
|
|
19
|
+
creditNote: "Credit Note",
|
|
20
|
+
creditorId: "Creditor ID",
|
|
21
|
+
currency: "Currency",
|
|
22
|
+
debitedAccount: "Debited account",
|
|
23
|
+
deliverTo: "Deliver to",
|
|
24
|
+
deliveryDate: "Delivery date",
|
|
25
|
+
description: "Description",
|
|
26
|
+
despatchAdvice: "Delivery note",
|
|
27
|
+
documentAllowance: "Discount",
|
|
28
|
+
documentCharge: "Charge",
|
|
29
|
+
dueDate: "Due date",
|
|
30
|
+
from: "From",
|
|
31
|
+
iban: "IBAN",
|
|
32
|
+
invoice: "Invoice",
|
|
33
|
+
invoicingPeriod: "Invoicing period",
|
|
34
|
+
issueDate: "Issue date",
|
|
35
|
+
lineNo: "#",
|
|
36
|
+
linePeriod: "Service period",
|
|
37
|
+
lineTotal: "Total",
|
|
38
|
+
mandateReference: "Mandate reference",
|
|
39
|
+
notes: "Notes",
|
|
40
|
+
objectIdentifier: "Object ID",
|
|
41
|
+
payable: "Amount due",
|
|
42
|
+
payee: "Payee",
|
|
43
|
+
paymentInstructions: "Payment",
|
|
44
|
+
paymentMeans: "Method",
|
|
45
|
+
paymentTerms: "Payment terms",
|
|
46
|
+
preceding: "Preceding invoice",
|
|
47
|
+
prepaid: "Prepaid",
|
|
48
|
+
project: "Project",
|
|
49
|
+
purchaseOrder: "Your order",
|
|
50
|
+
quantity: "Qty",
|
|
51
|
+
receivingAdvice: "Receiving advice",
|
|
52
|
+
remittance: "Reference",
|
|
53
|
+
rounding: "Rounding",
|
|
54
|
+
salesOrder: "Sales order",
|
|
55
|
+
subtotal: "Subtotal",
|
|
56
|
+
taxableAmount: "taxable",
|
|
57
|
+
taxRate: "VAT",
|
|
58
|
+
taxRep: "Tax representative",
|
|
59
|
+
taxTotal: "VAT {{rate}} %",
|
|
60
|
+
tender: "Tender / lot",
|
|
61
|
+
to: "To",
|
|
62
|
+
unitPrice: "Unit price",
|
|
63
|
+
},
|
|
64
|
+
de: {
|
|
65
|
+
accountName: "Kontoinhaber",
|
|
66
|
+
bic: "BIC",
|
|
67
|
+
cardHolder: "Karteninhaber",
|
|
68
|
+
cardNumber: "Kartennummer",
|
|
69
|
+
contract: "Vertrag",
|
|
70
|
+
creditNote: "Gutschrift",
|
|
71
|
+
creditorId: "Gläubiger-ID",
|
|
72
|
+
currency: "Währung",
|
|
73
|
+
debitedAccount: "Belastetes Konto",
|
|
74
|
+
deliverTo: "Lieferadresse",
|
|
75
|
+
deliveryDate: "Leistungsdatum",
|
|
76
|
+
description: "Beschreibung",
|
|
77
|
+
despatchAdvice: "Lieferschein",
|
|
78
|
+
documentAllowance: "Nachlass",
|
|
79
|
+
documentCharge: "Aufschlag",
|
|
80
|
+
dueDate: "Fälligkeitsdatum",
|
|
81
|
+
from: "Von",
|
|
82
|
+
iban: "IBAN",
|
|
83
|
+
invoice: "Rechnung",
|
|
84
|
+
invoicingPeriod: "Leistungszeitraum",
|
|
85
|
+
issueDate: "Rechnungsdatum",
|
|
86
|
+
lineNo: "#",
|
|
87
|
+
linePeriod: "Leistungszeitraum",
|
|
88
|
+
lineTotal: "Gesamt",
|
|
89
|
+
mandateReference: "Mandatsreferenz",
|
|
90
|
+
notes: "Hinweise",
|
|
91
|
+
objectIdentifier: "Objekt-ID",
|
|
92
|
+
payable: "Zahlbetrag",
|
|
93
|
+
payee: "Zahlungsempfänger",
|
|
94
|
+
paymentInstructions: "Zahlung",
|
|
95
|
+
paymentMeans: "Zahlungsart",
|
|
96
|
+
paymentTerms: "Zahlungsbedingungen",
|
|
97
|
+
preceding: "Vorherige Rechnung",
|
|
98
|
+
prepaid: "Bereits gezahlt",
|
|
99
|
+
project: "Projekt",
|
|
100
|
+
purchaseOrder: "Ihre Bestellung",
|
|
101
|
+
quantity: "Menge",
|
|
102
|
+
receivingAdvice: "Empfangsbestätigung",
|
|
103
|
+
remittance: "Verwendungszweck",
|
|
104
|
+
rounding: "Rundung",
|
|
105
|
+
salesOrder: "Auftrag",
|
|
106
|
+
subtotal: "Zwischensumme",
|
|
107
|
+
taxableAmount: "netto",
|
|
108
|
+
taxRate: "MwSt.",
|
|
109
|
+
taxRep: "Steuervertreter",
|
|
110
|
+
taxTotal: "MwSt. {{rate}} %",
|
|
111
|
+
tender: "Ausschreibung",
|
|
112
|
+
to: "An",
|
|
113
|
+
unitPrice: "Einzelpreis",
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Build the default label resolver for a language. Resolves `view.*` from the
|
|
119
|
+
* bundled strings (with `{{var}}` interpolation), and `units.*` /
|
|
120
|
+
* `paymentMeans.*` through @facturion/codelists. Returns the key unchanged on a
|
|
121
|
+
* miss, so the renderer's own fallbacks (to the raw code) still apply.
|
|
122
|
+
*/
|
|
123
|
+
export function makeT(lang) {
|
|
124
|
+
const view = VIEW[lang] ?? VIEW[DEFAULT_LANG];
|
|
125
|
+
return (key, vars = {}) => {
|
|
126
|
+
if (key.startsWith("units.")) {
|
|
127
|
+
let code = key.slice("units.".length);
|
|
128
|
+
const plural = code.endsWith("_plural");
|
|
129
|
+
if (plural) code = code.slice(0, -"_plural".length);
|
|
130
|
+
return unitLabel(code, lang, plural);
|
|
131
|
+
}
|
|
132
|
+
if (key.startsWith("paymentMeans.")) {
|
|
133
|
+
return codelistLabel("paymentMeans", key.slice("paymentMeans.".length), lang);
|
|
134
|
+
}
|
|
135
|
+
const k = key.startsWith("view.") ? key.slice("view.".length) : key;
|
|
136
|
+
const s = view[k];
|
|
137
|
+
if (typeof s !== "string") return key;
|
|
138
|
+
return s.replace(/\{\{(\w+)\}\}/g, (_, v) => String(vars[v] ?? ""));
|
|
139
|
+
};
|
|
140
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// @facturion/invoice-renderer — render the @facturion/invoice simplified-JSON
|
|
2
|
+
// model to a human-readable HTML invoice (the basis for an HTML→PDF flow).
|
|
3
|
+
|
|
4
|
+
export { renderInvoice, esc, amt, curAmt, lineNet, computeTotals } from "./render.js";
|
|
5
|
+
export { renderInvoiceDocument } from "./document.js";
|
|
6
|
+
export { makeT, VIEW, DEFAULT_LANG } from "./i18n.js";
|