@le-space/eigenbeleg 0.5.0 → 0.6.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/src/pdf.js CHANGED
@@ -16,10 +16,11 @@ import {
16
16
  deDate,
17
17
  deMoney,
18
18
  deNumber,
19
+ deRate,
19
20
  noInvoice,
20
21
  sourceName
21
22
  } from './document.js';
22
- import { A4, BLACK, GREY, WARN, embedIssuerLogo, pageLayout } from './layout.js';
23
+ import { A4, BLACK, GREY, MARK_SIZE, TYPE, WARN, embedIssuerLogo, pageLayout } from './layout.js';
23
24
 
24
25
  /** What made the file, unless the caller says otherwise. */
25
26
  export const CREATOR = 'eigenbeleg';
@@ -28,8 +29,8 @@ export const PRODUCER = 'eigenbeleg (Le Space UG), pdf-lib';
28
29
  /**
29
30
  * @param {import('./document.js').EigenbelegDocument} doc
30
31
  * @param {{ creator?: string, producer?: string, logo?: boolean, issuerLogo?: Uint8Array | null }} [o] logo: the chain's
31
- * mark, small, beside the title (off unless asked for; @le-space/pdf's LOGOS). issuerLogo: the issuer's own
32
- * logo, a PNG or JPEG file's bytes, in the head's top right corner (none unless given; an
32
+ * mark, very small, beside the title (off unless asked for; @le-space/pdf's LOGOS). issuerLogo: the issuer's own
33
+ * logo, a PNG or JPEG file's bytes, large as a letterhead: top right, the text below it (none unless given; an
33
34
  * ImageError of @le-space/pdf when the file is no small PNG or JPEG)
34
35
  * @returns {Promise<Uint8Array>}
35
36
  */
@@ -62,16 +63,8 @@ export async function eigenbelegPdf(
62
63
  let current = doc.number;
63
64
  const footerRight = `Daten: ${DATA_SOURCES[doc.chain] ?? doc.chainName} · Kurse: siehe Kursquelle`;
64
65
 
65
- const footer = (/** @type {import('./layout.js').Layout} */ l) => {
66
- const s = 7;
67
- regular.draw(l.page, `${current} · Seite ${pageNo}`, { x: M, y: M / 2, size: s, color: GREY });
68
- regular.draw(l.page, footerRight, {
69
- x: A4[0] - M - regular.width(footerRight, s),
70
- y: M / 2,
71
- size: s,
72
- color: GREY
73
- });
74
- };
66
+ const footer = (/** @type {import('./layout.js').Layout} */ l) =>
67
+ l.foot(l.page, `${current} · Seite ${pageNo}`, footerRight);
75
68
  const l = pageLayout(pdf, fonts, {
76
69
  margin: M,
77
70
  leavePage: footer,
@@ -79,55 +72,50 @@ export async function eigenbelegPdf(
79
72
  pageNo++;
80
73
  }
81
74
  });
82
- const { text, gap, field, title: heading } = l;
75
+ const { text, gap, field, label } = l;
83
76
  const width = l.width;
84
77
 
85
78
  /** @param {string} number @param {string} subtitle */
86
79
  function head(number, subtitle) {
87
80
  current = number;
81
+ // The issuer's logo as a letterhead, above everything else on the page.
82
+ l.letterhead(own);
88
83
  if (doc.testnet) {
89
84
  text(
90
85
  'TESTNET – keine echte Zahlung. Kurse des Mainnet-Assets nur zur Veranschaulichung; dieser Beleg ist kein Buchungsbeleg.',
91
- { face: bold, size: 9, color: WARN }
86
+ { face: bold, size: TYPE.table, color: WARN }
92
87
  );
93
- gap(4);
88
+ gap(6);
94
89
  }
95
- l.issuerLogo(own, l.y);
96
- heading('Eigenbeleg', { face: bold, size: 20 }, mark);
97
- text(`Nr. ${number}`, { face: bold, size: 12 });
98
- gap(2);
99
- text(subtitle, { size: 10 });
100
- gap(10);
90
+ // The chain's mark only small: the document is the issuer's, not the network's.
91
+ l.head('Eigenbeleg', { number, subtitle, image: mark, mark: MARK_SIZE.small });
101
92
  // payer | issue data, two columns
102
93
  const top = l.y;
103
- text('Zahlender und Aussteller', { face: bold, size: 8.5, color: GREY, max: width / 2 - 10 });
104
- text(doc.payer.name, { face: bold, size: 10, max: width / 2 - 10 });
105
- text(doc.payer.address, { size: 9.5, max: width / 2 - 10 });
94
+ label('Zahlender und Aussteller', { max: width / 2 - 10 });
95
+ text(doc.payer.name, { face: bold, size: TYPE.lead, max: width / 2 - 10 });
96
+ text(doc.payer.address, { max: width / 2 - 10 });
106
97
  const leftEnd = l.y;
107
98
  l.y = top;
108
99
  const rx = M + width / 2 + 10;
109
- text('Ausgestellt am', { x: rx, face: bold, size: 8.5, color: GREY });
100
+ label('Ausgestellt am', { x: rx });
110
101
  text(`${deDate(doc.issuedAt)}, ${doc.issuedAt.slice(11, 16)} UTC`, { x: rx });
111
- gap(3);
112
- text('Empfänger', { x: rx, face: bold, size: 8.5, color: GREY });
102
+ gap(5);
103
+ label('Empfänger', { x: rx });
113
104
  text(noInvoice(doc.chain, doc.chainName).recipient, { x: rx });
114
105
  l.y = Math.min(l.y, leftEnd);
115
106
  gap(8);
116
- text(doc.payer.addresses.length > 1 ? 'Eigene Zahlungsadressen' : 'Eigene Zahlungsadresse', {
117
- face: bold,
118
- size: 8.5,
119
- color: GREY
120
- });
121
- for (const a of doc.payer.addresses) text(a, { face: mono, size: 8 });
122
- gap(10);
123
- l.box(doc.note);
107
+ label(doc.payer.addresses.length > 1 ? 'Eigene Zahlungsadressen' : 'Eigene Zahlungsadresse');
108
+ for (const a of doc.payer.addresses) text(a, { face: mono, size: TYPE.table });
124
109
  gap(12);
110
+ l.note(doc.note);
111
+ gap(16);
125
112
  }
126
113
 
127
114
  const columns = [
128
115
  { title: 'Bewegung', width: 150 },
129
- { title: 'Menge', width: 118, right: true },
130
- { title: `Kurs ${cur}`, width: 52, right: true },
116
+ { title: 'Menge', width: 112, right: true },
117
+ // Six significant digits (deRate): the smallest rate, Aleph's credits, still fits on one line.
118
+ { title: `Kurs ${cur}`, width: 58, right: true },
131
119
  { title: `Wert ${cur}`, width: 66, right: true },
132
120
  { title: 'Auf dem Beleg', width: width - 386 }
133
121
  ];
@@ -139,30 +127,23 @@ export async function eigenbelegPdf(
139
127
  * @param {import('./document.js').Transaction} t @param {{ big?: boolean }} [o]
140
128
  */
141
129
  function transaction(t, { big = false } = {}) {
142
- const side = big ? 96 : 58;
143
- const textMax = width - side - 12;
130
+ // The code is a convenience beside the link, not the picture of the page.
131
+ const side = big ? 72 : 50;
132
+ const textMax = width - side - 16;
144
133
  l.ensure(big ? 150 : 120);
145
134
  const top = l.y;
146
135
  const title = `${big ? 'Zahlung' : `Position ${t.pos}`} · ${deDate(t.time)} ${t.time.slice(11, 19)} UTC`;
147
- text(title, { face: bold, size: 9.5, max: textMax });
148
- gap(2);
149
- field('Transaktion', t.hash, { face: mono, size: 7.5, max: textMax });
150
- field('Von', `${t.from.address} (${t.from.label})`, { face: mono, size: 7.5, max: textMax });
151
- field('An', t.to.label ? `${t.to.address} (${t.to.label})` : t.to.address, {
152
- face: mono,
153
- size: 7.5,
154
- max: textMax
155
- });
156
- field('Block-Explorer', t.explorerUrl, { face: mono, size: 6.5, max: textMax });
157
- const qrTop = top - 2;
158
- l.qr(t.explorerUrl, A4[0] - M - side, qrTop, side);
159
- l.y = Math.min(l.y, qrTop - side - 4);
136
+ text(title, { face: bold, size: TYPE.lead, max: textMax });
160
137
  gap(3);
161
- l.row(
162
- columns,
163
- columns.map((c) => c.title),
164
- { face: bold, color: GREY }
165
- );
138
+ const hash = { face: mono, size: TYPE.small, max: textMax, labelWidth: 76 };
139
+ field('Transaktion', t.hash, hash);
140
+ field('Von', `${t.from.address} (${t.from.label})`, hash);
141
+ field('An', t.to.label ? `${t.to.address} (${t.to.label})` : t.to.address, hash);
142
+ field('Block-Explorer', t.explorerUrl, { ...hash, size: TYPE.label });
143
+ l.qr(t.explorerUrl, A4[0] - M - side, top, side);
144
+ l.y = Math.min(l.y, top - side);
145
+ gap(10);
146
+ l.tableHead(columns);
166
147
  for (const m of t.movements) {
167
148
  const e = m.entry;
168
149
  l.row(
@@ -170,51 +151,46 @@ export async function eigenbelegPdf(
170
151
  [
171
152
  `${m.what}${e.type === 'received' ? ' (Eingang)' : ''}`,
172
153
  `${deNumber(e.amount)} ${e.asset}`,
173
- e.eur ? deNumber(e.eur.rate) : '—',
154
+ e.eur ? deRate(e.eur.rate) : '—',
174
155
  e.eur ? money(e.eur.value, 4) : '—',
175
156
  m.onIt ? 'ja' : `nein – ${m.why}`
176
157
  ],
177
158
  { color: m.onIt ? BLACK : GREY }
178
159
  );
179
160
  }
180
- l.ensure(12);
181
- l.right(`Auf dem Beleg: ${money(t.eurExact, 4)}`, A4[0] - M, 8.5, bold);
182
- l.y -= 14;
161
+ l.ensure(14);
162
+ l.y -= 1;
163
+ l.rule();
164
+ l.y -= 4;
165
+ // Not bold: the amount below is the figure to read; this is how it came about.
166
+ l.right(`Auf dem Beleg: ${money(t.eurExact, 4)}`, A4[0] - M, TYPE.table, regular);
167
+ l.y -= 16;
183
168
  }
184
169
 
185
170
  /** @param {{ why: string }[]} excluded @param {string[]} sources */
186
171
  function sourcesAndSignature(excluded, sources) {
187
- gap(6);
188
- text('Kursquelle', { face: bold, size: 8.5, color: GREY });
172
+ gap(8);
173
+ label('Kursquelle');
189
174
  text(sources.length ? `${sources.join('; ')} – jeweils für den Tag der Zahlung (UTC).` : '—', {
190
- size: 8.5
175
+ size: TYPE.table
191
176
  });
192
177
  if (cur !== 'EUR' && sources.length)
193
- text(`Werte in ${cur}: ${conversionText(cur)}.`, { size: 8.5 });
194
- gap(6);
178
+ text(`Werte in ${cur}: ${conversionText(cur)}.`, { size: TYPE.table });
179
+ gap(8);
195
180
  if (excluded.length) {
196
- text('Nicht auf diesem Eigenbeleg', { face: bold, size: 8.5, color: GREY });
181
+ label('Nicht auf diesem Eigenbeleg');
197
182
  /** @type {Record<string, number>} */
198
183
  const why = {};
199
184
  for (const x of excluded) why[x.why] = (why[x.why] ?? 0) + 1;
200
- for (const [w, n] of Object.entries(why)) text(`${n} × ${w}`, { size: 8.5 });
185
+ for (const [w, n] of Object.entries(why)) text(`${n} × ${w}`, { size: TYPE.table });
201
186
  text('Alle Bewegungen des Zeitraums stehen in der CSV-Datei zu diesem Eigenbeleg.', {
202
- size: 8.5,
187
+ size: TYPE.table,
203
188
  color: GREY
204
189
  });
205
- gap(6);
190
+ gap(8);
206
191
  }
207
- text(doc.notInvoice, { size: 8.5, color: GREY });
208
- gap(24);
209
- l.ensure(40);
210
- l.page.drawLine({
211
- start: { x: M, y: l.y },
212
- end: { x: M + 230, y: l.y },
213
- thickness: 0.6,
214
- color: BLACK
215
- });
216
- l.y -= 4;
217
- text('Ort, Datum, Unterschrift des Zahlenden', { size: 8, color: GREY });
192
+ text(doc.notInvoice, { size: TYPE.table, color: GREY });
193
+ l.signature('Ort, Datum, Unterschrift des Zahlenden');
218
194
  }
219
195
 
220
196
  /** @param {import('./document.js').Transaction[]} ts */
@@ -228,12 +204,7 @@ export async function eigenbelegPdf(
228
204
  )
229
205
  ];
230
206
  /** The amount line: a label on the left, the amount on the right. @param {string} label @param {string} total */
231
- const amountLine = (label, total) => {
232
- l.ensure(40);
233
- l.at(label, M, 10, bold);
234
- l.right(total, A4[0] - M, 11, bold);
235
- l.y -= 16;
236
- };
207
+ const amountLine = (label, total) => l.total(label, total);
237
208
  const period = `${deDate(doc.period.from)} – ${deDate(doc.period.to)}`;
238
209
 
239
210
  if (doc.layout === 'single' && doc.transactions.length > 0) {
@@ -254,9 +225,10 @@ export async function eigenbelegPdf(
254
225
  const what = [...new Set(t.movements.filter((m) => m.onIt).map((m) => m.what))].join(', ');
255
226
  amountLine('Betrag', money(t.eurExact));
256
227
  if (round(t.eurExact, places) === '0' && t.eurExact !== '0')
257
- text(`Ungerundet ${money(t.eurExact, 8)} (${below}).`, { size: 8, color: GREY });
258
- gap(4);
228
+ text(`Ungerundet ${money(t.eurExact, 8)} (${below}).`, { size: TYPE.table, color: GREY });
229
+ gap(6);
259
230
  field('Was wurde bezahlt', `${what} an ${t.to.label || t.to.address}`);
231
+ gap(2);
260
232
  field('Warum kein Fremdbeleg', noInvoice(doc.chain, doc.chainName).why);
261
233
  sourcesAndSignature([], sourcesOf([t]));
262
234
  });
@@ -266,11 +238,11 @@ export async function eigenbelegPdf(
266
238
  `Sammel-Eigenbeleg für Zahlungen auf ${doc.chainName} (${doc.network}), Zeitraum ${period}`
267
239
  );
268
240
  if (doc.transactions.length === 0) {
269
- text('Keine Zahlungen an das Netzwerk in diesem Zeitraum.', { size: 10 });
241
+ text('Keine Zahlungen an das Netzwerk in diesem Zeitraum.', { size: TYPE.lead });
270
242
  } else {
271
243
  for (const t of doc.transactions) {
272
244
  transaction(t);
273
- l.rule();
245
+ // Room between two positions; the rules belong to their tables.
274
246
  gap(8);
275
247
  }
276
248
  const qty = Object.entries(doc.totals.quantities)
@@ -287,7 +259,7 @@ export async function eigenbelegPdf(
287
259
  const subCent = doc.totals.eur === '0' && doc.totals.eurExact !== '0';
288
260
  text(
289
261
  `Ungerundet ${exact}${subCent ? ` (${below})` : ''}; jede Position zum Tageskurs ihres Zahlungstages.`,
290
- { size: 8, color: GREY }
262
+ { size: TYPE.table, color: GREY }
291
263
  );
292
264
  }
293
265
  gap(4);
@@ -12,7 +12,7 @@
12
12
  import { createPdfA, savePdfA } from '@le-space/pdf';
13
13
 
14
14
  import { deDate } from './document.js';
15
- import { A4, BLACK, GREY, pageLayout } from './layout.js';
15
+ import { GREY, TYPE, embedIssuerLogo, pageLayout } from './layout.js';
16
16
 
17
17
  /** The label column of a field: wide enough for "Übernachtungspauschalen". */
18
18
  const LABEL = 120;
@@ -26,15 +26,20 @@ const LABEL = 120;
26
26
  * @param {string} p.continuation printed on top of every further page
27
27
  * @param {string} [p.subject]
28
28
  * @param {string} [p.creator] what made it: `Belege` when not given
29
+ * @param {Uint8Array | null} [p.issuerLogo] the issuer's own logo, the bytes of a PNG or JPEG
30
+ * file: a letterhead, top right above the title, fitted into ISSUER_LOGO_BOX (layout.js); a file
31
+ * that cannot be one is an ImageError (@le-space/pdf) before anything is drawn
29
32
  */
30
33
  export async function statementLayout({
31
34
  title,
32
35
  created,
33
36
  continuation,
34
37
  subject = '',
35
- creator = 'Belege'
38
+ creator = 'Belege',
39
+ issuerLogo = null
36
40
  }) {
37
41
  const { pdf, fonts } = await createPdfA();
42
+ const own = await embedIssuerLogo(pdf, issuerLogo);
38
43
  const { regular, bold, mono } = fonts;
39
44
  const M = 48;
40
45
  let number = '';
@@ -42,7 +47,7 @@ export async function statementLayout({
42
47
  const l = pageLayout(pdf, fonts, {
43
48
  margin: M,
44
49
  enterPage: (p) => {
45
- p.at(continuation, M, 8, regular, GREY);
50
+ p.at(continuation, M, TYPE.label, regular, GREY);
46
51
  p.y -= 20;
47
52
  }
48
53
  });
@@ -60,19 +65,17 @@ export async function statementLayout({
60
65
  function head(no, by, subtitle, heading = 'Eigenbeleg') {
61
66
  number = no;
62
67
  issuer = by;
63
- l.text(heading, { face: bold, size: 20 });
64
- l.text(`Nr. ${no}`, { face: bold, size: 12 });
65
- l.gap(2);
66
- l.text(subtitle, { size: 10 });
67
- l.gap(10);
68
+ // The issuer's logo as a letterhead, the title below it.
69
+ l.letterhead(own);
70
+ l.head(heading, { number: no, subtitle });
68
71
  const top = l.y;
69
72
  const half = width / 2 - 10;
70
- l.text('Aussteller', { face: bold, size: 8.5, color: GREY, max: half });
71
- l.text(by || '—', { face: bold, size: 10, max: half });
73
+ l.label('Aussteller', { max: half });
74
+ l.text(by || '—', { face: bold, size: TYPE.lead, max: half });
72
75
  const leftEnd = l.y;
73
76
  l.y = top;
74
77
  const rx = M + width / 2 + 10;
75
- l.text('Erstellt am', { x: rx, face: bold, size: 8.5, color: GREY });
78
+ l.label('Erstellt am', { x: rx });
76
79
  l.text(`${deDate(created.toISOString())}, ${created.toISOString().slice(11, 16)} UTC`, {
77
80
  x: rx
78
81
  });
@@ -85,7 +88,7 @@ export async function statementLayout({
85
88
  function field(label, value, style = false) {
86
89
  l.field(label, value, {
87
90
  face: style === 'mono' ? mono : style ? bold : regular,
88
- size: style === 'mono' ? 7.5 : 8.5,
91
+ size: style === 'mono' ? TYPE.small : TYPE.body,
89
92
  labelWidth: LABEL
90
93
  });
91
94
  l.gap(3);
@@ -101,8 +104,8 @@ export async function statementLayout({
101
104
  /** @param {string} t */
102
105
  function heading(t) {
103
106
  l.ensure(40);
104
- l.text(t, { face: bold, size: 10 });
105
- l.gap(3);
107
+ l.text(t, { face: bold, size: TYPE.lead });
108
+ l.gap(4);
106
109
  }
107
110
 
108
111
  /**
@@ -112,44 +115,24 @@ export async function statementLayout({
112
115
  * @param {string[][]} rows
113
116
  */
114
117
  function table(columns, rows) {
115
- const header = () =>
116
- l.row(
117
- columns.map((c) => ({ ...c, mono: false })),
118
- columns.map((c) => c.title),
119
- { face: bold, color: GREY }
120
- );
118
+ const header = () => l.tableHead(columns);
121
119
  l.ensure(30);
122
120
  header();
123
121
  for (const row of rows) l.row(columns, row, { header });
124
- l.gap(8);
122
+ l.gap(1);
123
+ l.rule();
124
+ l.gap(10);
125
125
  }
126
126
 
127
127
  function signature() {
128
- l.gap(24);
129
- l.ensure(40);
130
- l.page.drawLine({
131
- start: { x: M, y: l.y },
132
- end: { x: M + 230, y: l.y },
133
- thickness: 0.6,
134
- color: BLACK
135
- });
136
- l.y -= 4;
137
- l.text('Ort, Datum, Unterschrift', { size: 8, color: GREY });
128
+ l.signature('Ort, Datum, Unterschrift');
138
129
  }
139
130
 
140
131
  async function save() {
141
132
  const n = l.pages.length;
142
- l.pages.forEach((p, i) => {
143
- const foot = `${number ? `${number} · ` : ''}Seite ${i + 1} von ${n}`;
144
- regular.draw(p, foot, { x: M, y: M / 2, size: 7, color: GREY });
145
- const made = `Erstellt mit ${creator}`;
146
- regular.draw(p, made, {
147
- x: A4[0] - M - regular.width(made, 7),
148
- y: M / 2,
149
- size: 7,
150
- color: GREY
151
- });
152
- });
133
+ l.pages.forEach((p, i) =>
134
+ l.foot(p, `${number ? `${number} · ` : ''}Seite ${i + 1} von ${n}`, `Erstellt mit ${creator}`)
135
+ );
153
136
  return savePdfA(pdf, {
154
137
  title,
155
138
  author: issuer,