-
Notifications
You must be signed in to change notification settings - Fork 13
/
receipt-to-pdf.js
99 lines (87 loc) · 2.25 KB
/
receipt-to-pdf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"use strict";
var fs = require('fs');
var path = require('path');
var PDF = require('pdfkit');
var PromiseA = require('bluebird');
var iconv = require('iconv-lite');
var Jimp = require('jimp');
var npos = require('../');
// example codec to show how add custom codecs
// TODO: more codec to decode escpos protocols
npos.codecs.bold = require('./codecs/text-bold');
var parser = npos.parser();
var raw = fs.readFileSync(path.join(__dirname, 'fixtures', 'receipt.bin'));
// prepare pdf instance
var doc = new PDF();
// pipe to pdf file
doc.pipe(fs.createWriteStream('output/receipt.pdf'));
// default formats
var formats = {
font: 'Times-Roman'
};
// parse raw to ast
parser.parse(raw).then(function (ast) {
return PromiseA.mapSeries(ast.entries, function (entry) {
// entry.type is the codec name
switch (entry.type) {
case 'text':
return renderText(doc, entry.data);
case 'font':
return renderFont(doc, entry.data);
case 'bold':
return renderBold(doc, entry.data);
case 'raster':
return renderImage(doc, entry.data);
// TODO render more esc pos command
default:
console.log('[pdf]', 'Unknown entry type:', entry.type);
break;
}
});
}).finally(function () {
doc.end();
});
function renderText(doc, data) {
console.log('[pdf] text:', data);
var font = formats.font;
if (formats.bold) {
font += '-Bold';
}
doc.font(font);
doc.text(decode(data));
}
// The fonts just for test. They should be real like physical receipt formats
function renderFont(doc, data) {
console.log('[pdf] font:', data);
var font;
switch (data) {
case 'A':
font = 'Times-Roman';
break;
case 'B':
font = 'Courier';
break;
default:
font = 'Times-Roman';
break;
}
formats.font = font;
}
function renderBold(doc, data) {
console.log('[pdf] bold:', data);
formats.bold = data;
}
function renderImage(doc, data) {
return PromiseA.fromCallback(function (cb) {
Jimp.read(data.toBuffer(), cb);
}).then(function (image) {
return PromiseA.fromCallback(function (cb) {
image.getBuffer(Jimp.MIME_PNG, cb);
});
}).then(function (buffer) {
doc.image(buffer);
});
}
function decode(data) {
return iconv.decode(data, 'GB18030');
}