CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-jsbarcode

JavaScript barcode generator library that creates various types of 1D barcodes with extensive customization options for both browsers and Node.js environments.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

cli.mddocs/

Command Line Interface

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.

Installation and Setup

The CLI tool is automatically available after installing JsBarcode via npm:

npm install jsbarcode

The CLI executable is registered as JsBarcode and can be used directly:

npx JsBarcode "Hello World!" --output barcode.png

For global installation:

npm install -g jsbarcode
JsBarcode "Hello World!" --output barcode.png

Capabilities

Basic Usage

# Basic syntax
JsBarcode <content> [options]

# Generate barcode with default settings
JsBarcode "Hello World!"

# Specify output filename
JsBarcode "Hello World!" --output mybarcode.png

# Output to stdout
JsBarcode "Hello World!" --stdout

Command Line Options

# Format and content
-f, --format <format>        Barcode format (CODE128, EAN13, etc.)

# Output options
-o, --output <filename>      Output filename (default: barcode.png)
-s, --stdout                 Output PNG data to stdout

# Barcode dimensions
-W, --width <width>          Width of individual bars
-H, --height <height>        Height of the barcode

# Margins and spacing  
-q, --quite <quite>          Empty space (margins) around barcode (maps to 'quite' option, not 'margin')

# Text options
-d, --displayValue           Display text under the barcode
-F, --font <font>            Font family for text
-a, --textAlign <align>      Text alignment (left, center, right)
-p, --textPadding <padding>  Padding between barcode and text
-S, --fontSize <fontsize>    Font size in pixels

# Colors
-b, --background <color>     Background color (maps to 'backgroundColor' in options)
-l, --lineColor <color>      Barcode line color

Format-Specific Generation

Generate barcodes in specific formats with appropriate validation:

# CODE128 barcodes
JsBarcode "Hello123" --format CODE128 --output code128.png

# EAN-13 barcodes (with checksum calculation)
JsBarcode "123456789012" --format EAN13 --output ean13.png

# CODE39 barcodes (alphanumeric)
JsBarcode "PRODUCT123" --format CODE39 --output code39.png

# UPC barcodes
JsBarcode "123456789012" --format UPC --output upc.png

# ITF barcodes (even-length numeric)
JsBarcode "1234567890" --format ITF --output itf.png

# MSI barcodes with checksum
JsBarcode "123456789" --format MSI10 --output msi.png

# Pharmacode
JsBarcode "12345" --format pharmacode --output pharma.png

Usage Examples

Basic Barcode Generation

# Simple barcode with default settings
JsBarcode "Hello World!"

# Custom filename
JsBarcode "Product123" --output product.png

# Specific format
JsBarcode "123456789012" --format EAN13 --output product-ean.png

Customizing Appearance

# Large barcode with custom colors
JsBarcode "LARGE123" \
  --format CODE39 \
  --width 4 \
  --height 200 \
  --background white \
  --lineColor black \
  --output large-barcode.png

# Small compact barcode
JsBarcode "SMALL" \
  --format CODE39 \
  --width 1 \
  --height 50 \
  --quite 5 \
  --output small-barcode.png

# High contrast barcode with text
JsBarcode "HC123456" \
  --format CODE128 \
  --displayValue \
  --fontSize 24 \
  --font Arial \
  --textAlign center \
  --background "#ffffff" \
  --lineColor "#000000" \
  --output high-contrast.png

Batch Processing

# Generate multiple barcodes in a script
#!/bin/bash

products=("123456789012" "234567890123" "345678901234")
for product in "${products[@]}"; do
  JsBarcode "$product" \
    --format EAN13 \
    --output "barcode-${product}.png" \
    --width 2 \
    --height 100 \
    --displayValue
done

# Generate barcodes from a file
while IFS= read -r line; do
  JsBarcode "$line" \
    --format CODE128 \
    --output "barcode-$(echo $line | tr ' ' '-').png"
done < products.txt

Pipeline Integration

# Output to stdout for further processing
JsBarcode "PIPELINE123" --stdout | convert - -resize 200% large-barcode.png

# Generate and immediately process
JsBarcode "DATA123" --format CODE39 --stdout | \
  convert - -bordercolor black -border 10 bordered-barcode.png

# Generate barcode and upload
JsBarcode "UPLOAD123" --stdout | \
  aws s3 cp - s3://mybucket/barcode.png --content-type image/png

Production Workflows

# Retail product barcode generation
JsBarcode "501234567890" \
  --format EAN13 \
  --width 2 \
  --height 100 \
  --displayValue \
  --fontSize 14 \
  --font "Arial" \
  --background white \
  --lineColor black \
  --quite 10 \
  --output retail-product.png

# Industrial asset tag
JsBarcode "ASSET-98765" \
  --format CODE39 \
  --width 2 \
  --height 60 \
  --displayValue \
  --fontSize 10 \
  --font monospace \
  --textAlign center \
  --output asset-tag.png

# Shipping label barcode
JsBarcode "1234567890123456" \
  --format ITF \
  --width 3 \
  --height 120 \
  --displayValue \
  --fontSize 16 \
  --quite 20 \
  --output shipping-label.png

Error Handling and Validation

Exit Codes

# Success
echo $?  # Returns 0 on successful generation

# Error cases
JsBarcode "invalid" --format EAN13 2>&1
# Outputs: "The data is not valid for the type of barcode."
# Exit code: 1

JsBarcode "test" --format INVALID 2>&1  
# Outputs: "Module INVALID does not exist or is not loaded."
# Exit code: 1

Input Validation

# Validate before processing
validate_and_generate() {
  local content="$1"
  local format="$2"
  
  if JsBarcode "$content" --format "$format" 2>/dev/null; then
    echo "Generated barcode for: $content"
  else
    echo "Failed to generate barcode for: $content" >&2
    return 1
  fi
}

# Usage
validate_and_generate "123456789012" "EAN13"
validate_and_generate "INVALID" "EAN13"  # Will fail

Robust Batch Processing

# Process with error handling
#!/bin/bash

generate_barcode_safe() {
  local content="$1"
  local format="$2"
  local output="$3"
  
  if JsBarcode "$content" --format "$format" --output "$output" 2>/dev/null; then
    echo "✓ Generated: $output"
  else
    echo "✗ Failed: $content (format: $format)" >&2
  fi
}

# Process list with error handling
while IFS=, read -r content format filename; do
  generate_barcode_safe "$content" "$format" "$filename"
done < barcodes.csv

Integration Examples

Web Server Integration

# CGI script for web barcode generation
#!/bin/bash
echo "Content-Type: image/png"
echo ""

# Get parameters from query string
content="${QUERY_STRING}"
JsBarcode "$content" --stdout

Docker Integration

FROM node:16-alpine
RUN npm install -g jsbarcode canvas
WORKDIR /app

# Generate barcodes in container
ENTRYPOINT ["JsBarcode"]
# Use Docker to generate barcodes
docker run --rm -v $(pwd):/app barcode-gen "Hello Docker!" --output /app/docker-barcode.png

Automation Scripts

# Inventory update script
#!/bin/bash

# Generate barcodes for new inventory items
mysql -u user -p inventory -e "SELECT sku FROM products WHERE barcode IS NULL" | \
while read sku; do
  if [ "$sku" != "sku" ]; then  # Skip header
    barcode_file="barcodes/${sku}.png"
    JsBarcode "$sku" --format CODE128 --output "$barcode_file"
    
    # Update database with barcode file path
    mysql -u user -p inventory -e \
      "UPDATE products SET barcode='$barcode_file' WHERE sku='$sku'"
    
    echo "Generated barcode for SKU: $sku"
  fi
done

System Integration

# Print server integration
generate_and_print() {
  local content="$1"
  local format="$2"
  
  # Generate barcode
  JsBarcode "$content" --format "$format" --stdout | \
  # Convert to appropriate print format
  convert - -density 300 -units PixelsPerInch temp.pdf
  
  # Send to printer
  lp temp.pdf
  rm temp.pdf
}

# Usage
generate_and_print "123456789012" "EAN13"

Performance and Optimization

Optimizing for Batch Operations

# Pre-compile frequently used options
COMMON_OPTS="--width 2 --height 100 --displayValue --background white --lineColor black"

# Batch generation with common options
for code in "${codes[@]}"; do
  JsBarcode "$code" $COMMON_OPTS --output "barcode-${code}.png"
done

# Parallel processing for large batches
printf '%s\n' "${codes[@]}" | \
  xargs -n 1 -P 4 -I {} \
  JsBarcode {} $COMMON_OPTS --output "barcode-{}.png"

Memory Management

# For very large batches, process in chunks
process_chunk() {
  local -a chunk=("$@")
  for code in "${chunk[@]}"; do
    JsBarcode "$code" --stdout > "barcode-${code}.png"
  done
}

# Split large array into chunks
CHUNK_SIZE=100
for ((i=0; i<${#codes[@]}; i+=CHUNK_SIZE)); do
  chunk=("${codes[@]:i:CHUNK_SIZE}")
  process_chunk "${chunk[@]}"
done

docs

cli.md

code128.md

configuration.md

ean-upc.md

index.md

industrial-formats.md

tile.json