Skip to content

Commit bf6c835

Browse files
committed
Handle unicode
1 parent d7a28a7 commit bf6c835

File tree

5 files changed

+105
-20
lines changed

5 files changed

+105
-20
lines changed

example.pdf

4.74 KB
Binary file not shown.

examples/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ let myInvoice = new MicroInvoice({
6767
color : "primary"
6868
}, {
6969
value : "sed do eiusmod tempor incididunt ut labore et dolore magna.",
70-
weight : "regular",
70+
weight : "bold",
7171
color : "secondary"
7272
}],
7373

lib/index.js

+103-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

33
const PDFDocument = require("pdfkit");
4+
const path = require("path");
45
const fs = require("fs");
56
const _merge = require("lodash.merge");
67
const getStream = require("get-stream");
@@ -23,6 +24,21 @@ module.exports = class Microinvoice {
2324
marginRight : 30,
2425
marginTop : 30
2526
},
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+
},
2642
header : {
2743
backgroundColor : "#F8F8FA",
2844
height : 150,
@@ -32,8 +48,7 @@ module.exports = class Microinvoice {
3248
table : {
3349
headerColor : "#F8F8FA"
3450
},
35-
font : {
36-
family : "helvetica",
51+
text : {
3752
primaryColor : "#000100",
3853
secondaryColor : "#8F8F8F",
3954
headingSize : 15,
@@ -91,10 +106,76 @@ module.exports = class Microinvoice {
91106
seller : {
92107
height : 0
93108
},
109+
fonts : {
110+
fallback : {
111+
loaded : false
112+
}
113+
},
94114
document : null
95115
};
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+
}
96142
}
97143

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+
98179
/**
99180
* Generates the header
100181
*
@@ -133,12 +214,14 @@ module.exports = class Microinvoice {
133214

134215
this.setText(this.options.data.invoice.name, {
135216
fontSize : "heading",
136-
fontWeight : "bold"
217+
fontWeight : "bold",
218+
color : this.options.style.header.regularColor
137219
});
138220

139221
this.options.data.invoice.header.forEach(line => {
140222
this.setText(`${line.label}:`, {
141223
fontWeight : "bold",
224+
color : this.options.style.header.regularColor,
142225
marginTop : _fontMargin
143226
});
144227

@@ -153,6 +236,7 @@ module.exports = class Microinvoice {
153236
_values.forEach((value) => {
154237
this.setText(value, {
155238
colorCode : "secondary",
239+
color : this.options.style.header.secondaryColor,
156240
marginTop : _fontMargin
157241
});
158242
});
@@ -213,7 +297,7 @@ module.exports = class Microinvoice {
213297
* @return void
214298
*/
215299
generateTableRow(type, columns) {
216-
let _fontWeight = "regular";
300+
let _fontWeight = "normal";
217301

218302
this.storage.cursor.y += 17;
219303

@@ -281,7 +365,7 @@ module.exports = class Microinvoice {
281365
* @return void
282366
*/
283367
generateLine() {
284-
this.storage.cursor.y += this.options.style.font.regularSize + 2;
368+
this.storage.cursor.y += this.options.style.text.regularSize + 2;
285369

286370
this.document
287371
.strokeColor("#F0F0F0")
@@ -372,7 +456,7 @@ module.exports = class Microinvoice {
372456

373457
this.setText(legal.value, {
374458
fontWeight : legal.weight,
375-
colorCode : legal.code || "primary",
459+
colorCode : legal.color || "primary",
376460
align : "center",
377461
marginTop : 10
378462
});
@@ -423,30 +507,30 @@ module.exports = class Microinvoice {
423507
let _colorCode = options.colorCode || "primary";
424508
let _fontSize = options.fontSize || "regular";
425509
let _textAlign = options.align || "left";
510+
let _color = options.color || "";
426511
let _marginTop = options.marginTop || 0;
427512
let _maxWidth = options.maxWidth;
428-
let _color = "";
429513
let _fontSizeValue = 0;
430514

431515
this.storage.cursor.y += _marginTop;
432516

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+
}
437523
}
438524

439525
if (_fontSize === "regular") {
440-
_fontSizeValue = this.options.style.font.regularSize;
526+
_fontSizeValue = this.options.style.text.regularSize;
441527
} else {
442-
_fontSizeValue = this.options.style.font.headingSize;
528+
_fontSizeValue = this.options.style.text.headingSize;
443529
}
444530

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));
450534

451535
this.document.fillColor(_color);
452536
this.document.fontSize(_fontSizeValue);
@@ -480,6 +564,7 @@ module.exports = class Microinvoice {
480564
size : "A4"
481565
});
482566

567+
this.loadCustomFonts();
483568
this.generateHeader();
484569
this.generateDetails("customer");
485570
this.generateDetails("seller");

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "microinvoice",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Fast & elegant PDF invoice generator for Node using PDFKit. No Puppeteer",
55
"homepage": "https://github.com/baptistejamin/node-microinvoice",
66
"main": "lib/index.js",

res/fonts/NotoSans-Regular.ttf

390 KB
Binary file not shown.

0 commit comments

Comments
 (0)