forked from lindell/JsBarcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·67 lines (60 loc) · 1.96 KB
/
cli.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
#!/usr/bin/env node
var program = require("commander");
var JsBarcode = require('./JsBarcode.js');
var Canvas = require("canvas");
var fs = require('fs');
var path = require("path");
function parseIntTen (x) {
return parseInt(x, 10)
}
var content;
program
.usage('<content> [options]')
.option('-o, --output <filename>', "Default: barcode.png")
.option('-s, --stdout', "Output the png data to stdout")
.option('-f, --format <format>', 'Barcode format')
.option('-W, --width <width>', 'Width of a line in the barcode', parseIntTen)
.option('-H, --height <height>', 'Height of the barcode', parseIntTen)
.option('-q, --quite <quite>', 'Empty space left and right of the barcode', parseIntTen)
.option('-d, --displayValue', 'Display the value as text under the barcode')
.option('-F, --font <font>', 'Set the font of the text')
.option('-a, --textAlign <align>', 'Set where the text will be displayed')
.option('-p, --textPadding <padding>', 'The padding between the barcode and the text', parseIntTen)
.option('-S, --fontSize <fontsize>', 'Set the font size', parseIntTen)
.option('-b, --background <color>', 'Set the background color')
.option('-l, --lineColor <color>', 'Set the line color')
.action(function (c) {
content = c;
});
program.parse(process.argv);
if(!content){
program.help();
}
var filename = program.output || "barcode.png";
var options = {
format: program.format,
width: program.width,
height: program.height,
quite: program.quite,
displayValue: program.displayValue,
font: program.font,
textAlign: program.textAlign,
textPadding: program.textPadding,
fontSize: program.fontSize,
backgroundColor: program.background,
lineColor: program.lineColor
};
try{
var canvas = new Canvas();
JsBarcode(canvas, content, options);
var stream = canvas.pngStream();
if(program.stdout){
stream.pipe(process.stdout);
}
else{
stream.pipe(fs.createWriteStream(filename));
}
}
catch(e){
console.error(e.message);
}