0
# Command Line Interface
1
2
JsBarcode provides a comprehensive command-line interface for generating barcode images from the terminal, perfect for batch processing, automation scripts, and server-side barcode generation.
3
4
## Installation and Setup
5
6
The CLI tool is automatically available after installing JsBarcode via npm:
7
8
```bash
9
npm install jsbarcode
10
```
11
12
The CLI executable is registered as `JsBarcode` and can be used directly:
13
14
```bash
15
npx JsBarcode "Hello World!" --output barcode.png
16
```
17
18
For global installation:
19
20
```bash
21
npm install -g jsbarcode
22
JsBarcode "Hello World!" --output barcode.png
23
```
24
25
## Capabilities
26
27
### Basic Usage
28
29
```bash { .api }
30
# Basic syntax
31
JsBarcode <content> [options]
32
33
# Generate barcode with default settings
34
JsBarcode "Hello World!"
35
36
# Specify output filename
37
JsBarcode "Hello World!" --output mybarcode.png
38
39
# Output to stdout
40
JsBarcode "Hello World!" --stdout
41
```
42
43
### Command Line Options
44
45
```bash { .api }
46
# Format and content
47
-f, --format <format> Barcode format (CODE128, EAN13, etc.)
48
49
# Output options
50
-o, --output <filename> Output filename (default: barcode.png)
51
-s, --stdout Output PNG data to stdout
52
53
# Barcode dimensions
54
-W, --width <width> Width of individual bars
55
-H, --height <height> Height of the barcode
56
57
# Margins and spacing
58
-q, --quite <quite> Empty space (margins) around barcode (maps to 'quite' option, not 'margin')
59
60
# Text options
61
-d, --displayValue Display text under the barcode
62
-F, --font <font> Font family for text
63
-a, --textAlign <align> Text alignment (left, center, right)
64
-p, --textPadding <padding> Padding between barcode and text
65
-S, --fontSize <fontsize> Font size in pixels
66
67
# Colors
68
-b, --background <color> Background color (maps to 'backgroundColor' in options)
69
-l, --lineColor <color> Barcode line color
70
```
71
72
### Format-Specific Generation
73
74
Generate barcodes in specific formats with appropriate validation:
75
76
```bash { .api }
77
# CODE128 barcodes
78
JsBarcode "Hello123" --format CODE128 --output code128.png
79
80
# EAN-13 barcodes (with checksum calculation)
81
JsBarcode "123456789012" --format EAN13 --output ean13.png
82
83
# CODE39 barcodes (alphanumeric)
84
JsBarcode "PRODUCT123" --format CODE39 --output code39.png
85
86
# UPC barcodes
87
JsBarcode "123456789012" --format UPC --output upc.png
88
89
# ITF barcodes (even-length numeric)
90
JsBarcode "1234567890" --format ITF --output itf.png
91
92
# MSI barcodes with checksum
93
JsBarcode "123456789" --format MSI10 --output msi.png
94
95
# Pharmacode
96
JsBarcode "12345" --format pharmacode --output pharma.png
97
```
98
99
## Usage Examples
100
101
### Basic Barcode Generation
102
103
```bash
104
# Simple barcode with default settings
105
JsBarcode "Hello World!"
106
107
# Custom filename
108
JsBarcode "Product123" --output product.png
109
110
# Specific format
111
JsBarcode "123456789012" --format EAN13 --output product-ean.png
112
```
113
114
### Customizing Appearance
115
116
```bash
117
# Large barcode with custom colors
118
JsBarcode "LARGE123" \
119
--format CODE39 \
120
--width 4 \
121
--height 200 \
122
--background white \
123
--lineColor black \
124
--output large-barcode.png
125
126
# Small compact barcode
127
JsBarcode "SMALL" \
128
--format CODE39 \
129
--width 1 \
130
--height 50 \
131
--quite 5 \
132
--output small-barcode.png
133
134
# High contrast barcode with text
135
JsBarcode "HC123456" \
136
--format CODE128 \
137
--displayValue \
138
--fontSize 24 \
139
--font Arial \
140
--textAlign center \
141
--background "#ffffff" \
142
--lineColor "#000000" \
143
--output high-contrast.png
144
```
145
146
### Batch Processing
147
148
```bash
149
# Generate multiple barcodes in a script
150
#!/bin/bash
151
152
products=("123456789012" "234567890123" "345678901234")
153
for product in "${products[@]}"; do
154
JsBarcode "$product" \
155
--format EAN13 \
156
--output "barcode-${product}.png" \
157
--width 2 \
158
--height 100 \
159
--displayValue
160
done
161
162
# Generate barcodes from a file
163
while IFS= read -r line; do
164
JsBarcode "$line" \
165
--format CODE128 \
166
--output "barcode-$(echo $line | tr ' ' '-').png"
167
done < products.txt
168
```
169
170
### Pipeline Integration
171
172
```bash
173
# Output to stdout for further processing
174
JsBarcode "PIPELINE123" --stdout | convert - -resize 200% large-barcode.png
175
176
# Generate and immediately process
177
JsBarcode "DATA123" --format CODE39 --stdout | \
178
convert - -bordercolor black -border 10 bordered-barcode.png
179
180
# Generate barcode and upload
181
JsBarcode "UPLOAD123" --stdout | \
182
aws s3 cp - s3://mybucket/barcode.png --content-type image/png
183
```
184
185
### Production Workflows
186
187
```bash
188
# Retail product barcode generation
189
JsBarcode "501234567890" \
190
--format EAN13 \
191
--width 2 \
192
--height 100 \
193
--displayValue \
194
--fontSize 14 \
195
--font "Arial" \
196
--background white \
197
--lineColor black \
198
--quite 10 \
199
--output retail-product.png
200
201
# Industrial asset tag
202
JsBarcode "ASSET-98765" \
203
--format CODE39 \
204
--width 2 \
205
--height 60 \
206
--displayValue \
207
--fontSize 10 \
208
--font monospace \
209
--textAlign center \
210
--output asset-tag.png
211
212
# Shipping label barcode
213
JsBarcode "1234567890123456" \
214
--format ITF \
215
--width 3 \
216
--height 120 \
217
--displayValue \
218
--fontSize 16 \
219
--quite 20 \
220
--output shipping-label.png
221
```
222
223
## Error Handling and Validation
224
225
### Exit Codes
226
227
```bash { .api }
228
# Success
229
echo $? # Returns 0 on successful generation
230
231
# Error cases
232
JsBarcode "invalid" --format EAN13 2>&1
233
# Outputs: "The data is not valid for the type of barcode."
234
# Exit code: 1
235
236
JsBarcode "test" --format INVALID 2>&1
237
# Outputs: "Module INVALID does not exist or is not loaded."
238
# Exit code: 1
239
```
240
241
### Input Validation
242
243
```bash
244
# Validate before processing
245
validate_and_generate() {
246
local content="$1"
247
local format="$2"
248
249
if JsBarcode "$content" --format "$format" 2>/dev/null; then
250
echo "Generated barcode for: $content"
251
else
252
echo "Failed to generate barcode for: $content" >&2
253
return 1
254
fi
255
}
256
257
# Usage
258
validate_and_generate "123456789012" "EAN13"
259
validate_and_generate "INVALID" "EAN13" # Will fail
260
```
261
262
### Robust Batch Processing
263
264
```bash
265
# Process with error handling
266
#!/bin/bash
267
268
generate_barcode_safe() {
269
local content="$1"
270
local format="$2"
271
local output="$3"
272
273
if JsBarcode "$content" --format "$format" --output "$output" 2>/dev/null; then
274
echo "✓ Generated: $output"
275
else
276
echo "✗ Failed: $content (format: $format)" >&2
277
fi
278
}
279
280
# Process list with error handling
281
while IFS=, read -r content format filename; do
282
generate_barcode_safe "$content" "$format" "$filename"
283
done < barcodes.csv
284
```
285
286
## Integration Examples
287
288
### Web Server Integration
289
290
```bash
291
# CGI script for web barcode generation
292
#!/bin/bash
293
echo "Content-Type: image/png"
294
echo ""
295
296
# Get parameters from query string
297
content="${QUERY_STRING}"
298
JsBarcode "$content" --stdout
299
```
300
301
### Docker Integration
302
303
```dockerfile
304
FROM node:16-alpine
305
RUN npm install -g jsbarcode canvas
306
WORKDIR /app
307
308
# Generate barcodes in container
309
ENTRYPOINT ["JsBarcode"]
310
```
311
312
```bash
313
# Use Docker to generate barcodes
314
docker run --rm -v $(pwd):/app barcode-gen "Hello Docker!" --output /app/docker-barcode.png
315
```
316
317
### Automation Scripts
318
319
```bash
320
# Inventory update script
321
#!/bin/bash
322
323
# Generate barcodes for new inventory items
324
mysql -u user -p inventory -e "SELECT sku FROM products WHERE barcode IS NULL" | \
325
while read sku; do
326
if [ "$sku" != "sku" ]; then # Skip header
327
barcode_file="barcodes/${sku}.png"
328
JsBarcode "$sku" --format CODE128 --output "$barcode_file"
329
330
# Update database with barcode file path
331
mysql -u user -p inventory -e \
332
"UPDATE products SET barcode='$barcode_file' WHERE sku='$sku'"
333
334
echo "Generated barcode for SKU: $sku"
335
fi
336
done
337
```
338
339
### System Integration
340
341
```bash
342
# Print server integration
343
generate_and_print() {
344
local content="$1"
345
local format="$2"
346
347
# Generate barcode
348
JsBarcode "$content" --format "$format" --stdout | \
349
# Convert to appropriate print format
350
convert - -density 300 -units PixelsPerInch temp.pdf
351
352
# Send to printer
353
lp temp.pdf
354
rm temp.pdf
355
}
356
357
# Usage
358
generate_and_print "123456789012" "EAN13"
359
```
360
361
## Performance and Optimization
362
363
### Optimizing for Batch Operations
364
365
```bash
366
# Pre-compile frequently used options
367
COMMON_OPTS="--width 2 --height 100 --displayValue --background white --lineColor black"
368
369
# Batch generation with common options
370
for code in "${codes[@]}"; do
371
JsBarcode "$code" $COMMON_OPTS --output "barcode-${code}.png"
372
done
373
374
# Parallel processing for large batches
375
printf '%s\n' "${codes[@]}" | \
376
xargs -n 1 -P 4 -I {} \
377
JsBarcode {} $COMMON_OPTS --output "barcode-{}.png"
378
```
379
380
### Memory Management
381
382
```bash
383
# For very large batches, process in chunks
384
process_chunk() {
385
local -a chunk=("$@")
386
for code in "${chunk[@]}"; do
387
JsBarcode "$code" --stdout > "barcode-${code}.png"
388
done
389
}
390
391
# Split large array into chunks
392
CHUNK_SIZE=100
393
for ((i=0; i<${#codes[@]}; i+=CHUNK_SIZE)); do
394
chunk=("${codes[@]:i:CHUNK_SIZE}")
395
process_chunk "${chunk[@]}"
396
done
397
```