1
1
"use strict" ;
2
2
3
3
const PDFDocument = require ( "pdfkit" ) ;
4
+ const path = require ( "path" ) ;
4
5
const fs = require ( "fs" ) ;
5
6
const _merge = require ( "lodash.merge" ) ;
6
7
const getStream = require ( "get-stream" ) ;
@@ -23,6 +24,21 @@ module.exports = class Microinvoice {
23
24
marginRight : 30 ,
24
25
marginTop : 30
25
26
} ,
27
+ fonts : {
28
+ normal : {
29
+ name : "Helvetica"
30
+ } ,
31
+ bold : {
32
+ name : "Helvetica-Bold"
33
+ } ,
34
+ fallback : {
35
+ name : "Noto Sans" ,
36
+ path : path . join (
37
+ __dirname , "../res/fonts/" , "NotoSans-Regular.ttf"
38
+ ) ,
39
+ enabled : true
40
+ }
41
+ } ,
26
42
header : {
27
43
backgroundColor : "#F8F8FA" ,
28
44
height : 150 ,
@@ -32,8 +48,7 @@ module.exports = class Microinvoice {
32
48
table : {
33
49
headerColor : "#F8F8FA"
34
50
} ,
35
- font : {
36
- family : "helvetica" ,
51
+ text : {
37
52
primaryColor : "#000100" ,
38
53
secondaryColor : "#8F8F8F" ,
39
54
headingSize : 15 ,
@@ -91,10 +106,76 @@ module.exports = class Microinvoice {
91
106
seller : {
92
107
height : 0
93
108
} ,
109
+ fonts : {
110
+ fallback : {
111
+ loaded : false
112
+ }
113
+ } ,
94
114
document : null
95
115
} ;
116
+
117
+ this . specialCharsRegex = / [ ^ \x00 - \xFF ] / m;
118
+ }
119
+
120
+
121
+ /**
122
+ * Load custom fonts
123
+ *
124
+ * @private
125
+ * @return void
126
+ */
127
+ loadCustomFonts ( ) {
128
+ // Register custom fonts
129
+ if ( this . options . style . fonts . normal . path ) {
130
+ this . document . registerFont (
131
+ this . options . style . fonts . normal . name ,
132
+ this . options . style . fonts . normal . path
133
+ ) ;
134
+ }
135
+
136
+ if ( this . options . style . fonts . bold . path ) {
137
+ this . document . registerFont (
138
+ this . options . style . fonts . bold . name ,
139
+ this . options . style . fonts . bold . path
140
+ ) ;
141
+ }
96
142
}
97
143
144
+
145
+ /**
146
+ * Load fallback font (unicode chars)
147
+ *
148
+ * @private
149
+ * @return void
150
+ */
151
+ getFontOrFallback ( type , value ) {
152
+ if ( type !== "normal" && type !== "bold" ) {
153
+ type = "normal" ;
154
+ }
155
+
156
+ // Return default font
157
+ if ( this . options . style . fonts . fallback . enabled === false ) {
158
+ return this . options . style . fonts [ type ] . name ;
159
+ }
160
+
161
+ // Return default font if not special chars are found
162
+ if ( ! this . specialCharsRegex . test ( ( value || "" ) . toString ( ) ) ) {
163
+ return this . options . style . fonts [ type ] . name ;
164
+ }
165
+
166
+ if ( this . storage . fonts . fallback . loaded === false ) {
167
+ this . document . registerFont (
168
+ this . options . style . fonts . fallback . name ,
169
+ this . options . style . fonts . fallback . path
170
+ ) ;
171
+ this . storage . fonts . fallback . loaded = true ;
172
+ }
173
+
174
+ // Return fallback font
175
+ return this . options . style . fonts . fallback . name ;
176
+ }
177
+
178
+
98
179
/**
99
180
* Generates the header
100
181
*
@@ -133,12 +214,14 @@ module.exports = class Microinvoice {
133
214
134
215
this . setText ( this . options . data . invoice . name , {
135
216
fontSize : "heading" ,
136
- fontWeight : "bold"
217
+ fontWeight : "bold" ,
218
+ color : this . options . style . header . regularColor
137
219
} ) ;
138
220
139
221
this . options . data . invoice . header . forEach ( line => {
140
222
this . setText ( `${ line . label } :` , {
141
223
fontWeight : "bold" ,
224
+ color : this . options . style . header . regularColor ,
142
225
marginTop : _fontMargin
143
226
} ) ;
144
227
@@ -153,6 +236,7 @@ module.exports = class Microinvoice {
153
236
_values . forEach ( ( value ) => {
154
237
this . setText ( value , {
155
238
colorCode : "secondary" ,
239
+ color : this . options . style . header . secondaryColor ,
156
240
marginTop : _fontMargin
157
241
} ) ;
158
242
} ) ;
@@ -213,7 +297,7 @@ module.exports = class Microinvoice {
213
297
* @return void
214
298
*/
215
299
generateTableRow ( type , columns ) {
216
- let _fontWeight = "regular " ;
300
+ let _fontWeight = "normal " ;
217
301
218
302
this . storage . cursor . y += 17 ;
219
303
@@ -281,7 +365,7 @@ module.exports = class Microinvoice {
281
365
* @return void
282
366
*/
283
367
generateLine ( ) {
284
- this . storage . cursor . y += this . options . style . font . regularSize + 2 ;
368
+ this . storage . cursor . y += this . options . style . text . regularSize + 2 ;
285
369
286
370
this . document
287
371
. strokeColor ( "#F0F0F0" )
@@ -372,7 +456,7 @@ module.exports = class Microinvoice {
372
456
373
457
this . setText ( legal . value , {
374
458
fontWeight : legal . weight ,
375
- colorCode : legal . code || "primary" ,
459
+ colorCode : legal . color || "primary" ,
376
460
align : "center" ,
377
461
marginTop : 10
378
462
} ) ;
@@ -423,30 +507,30 @@ module.exports = class Microinvoice {
423
507
let _colorCode = options . colorCode || "primary" ;
424
508
let _fontSize = options . fontSize || "regular" ;
425
509
let _textAlign = options . align || "left" ;
510
+ let _color = options . color || "" ;
426
511
let _marginTop = options . marginTop || 0 ;
427
512
let _maxWidth = options . maxWidth ;
428
- let _color = "" ;
429
513
let _fontSizeValue = 0 ;
430
514
431
515
this . storage . cursor . y += _marginTop ;
432
516
433
- if ( _colorCode === "primary" ) {
434
- this . document . fillColor ( this . options . style . font . primaryColor ) ;
435
- } else {
436
- this . document . fillColor ( this . options . style . font . secondaryColor ) ;
517
+ if ( ! _color ) {
518
+ if ( _colorCode === "primary" ) {
519
+ this . document . fillColor ( this . options . style . text . primaryColor ) ;
520
+ } else {
521
+ this . document . fillColor ( this . options . style . text . secondaryColor ) ;
522
+ }
437
523
}
438
524
439
525
if ( _fontSize === "regular" ) {
440
- _fontSizeValue = this . options . style . font . regularSize ;
526
+ _fontSizeValue = this . options . style . text . regularSize ;
441
527
} else {
442
- _fontSizeValue = this . options . style . font . headingSize ;
528
+ _fontSizeValue = this . options . style . text . headingSize ;
443
529
}
444
530
445
- if ( _fontWeight === "bold" ) {
446
- this . document . font ( "Helvetica-Bold" ) ;
447
- } else {
448
- this . document . font ( "Helvetica" ) ;
449
- }
531
+ this . getFontOrFallback ( _fontWeight , text ) ;
532
+
533
+ this . document . font ( this . getFontOrFallback ( _fontWeight , text ) ) ;
450
534
451
535
this . document . fillColor ( _color ) ;
452
536
this . document . fontSize ( _fontSizeValue ) ;
@@ -480,6 +564,7 @@ module.exports = class Microinvoice {
480
564
size : "A4"
481
565
} ) ;
482
566
567
+ this . loadCustomFonts ( ) ;
483
568
this . generateHeader ( ) ;
484
569
this . generateDetails ( "customer" ) ;
485
570
this . generateDetails ( "seller" ) ;
0 commit comments