isahaq-barcode 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +397 -297
- package/package.json +6 -1
- package/src/builders/QrCodeBuilder.js +14 -1
package/README.md
CHANGED
|
@@ -1,81 +1,112 @@
|
|
|
1
1
|
# Isahaq Barcode Generator
|
|
2
2
|
|
|
3
|
-
A
|
|
3
|
+
> A comprehensive Node.js barcode generation library supporting 32+ barcode types with Express.js integration and CLI tools
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
[](https://www.npmjs.com/package/isahaq-barcode)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
- **Multiple Output Formats**: PNG, SVG, HTML, JPG, PDF
|
|
9
|
-
- **Express.js Integration**: Middleware and route helpers
|
|
10
|
-
- **CLI Tool**: Command-line interface for quick barcode generation
|
|
11
|
-
- **QR Code Builder**: Fluent API for advanced QR code generation
|
|
12
|
-
- **Validation**: Built-in data validation for different barcode types
|
|
13
|
-
- **Customization**: Size, colors, margins, and more options
|
|
14
|
-
- **Batch Generation**: Generate multiple barcodes at once
|
|
15
|
-
- **Watermarking**: Add logos and watermarks to QR codes
|
|
8
|
+
---
|
|
16
9
|
|
|
17
|
-
##
|
|
10
|
+
## Table of Contents
|
|
11
|
+
|
|
12
|
+
- [Features](#-features)
|
|
13
|
+
- [Installation](#-installation)
|
|
14
|
+
- [Quick Start](#-quick-start)
|
|
15
|
+
- [Supported Barcode Types](#-supported-barcode-types)
|
|
16
|
+
- [Usage Examples](#-usage-examples)
|
|
17
|
+
- [Express.js Integration](#-expressjs-integration)
|
|
18
|
+
- [CLI Usage](#-cli-usage)
|
|
19
|
+
- [API Reference](#-api-reference)
|
|
20
|
+
- [Contributing](#-contributing)
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## ✨ Features
|
|
18
25
|
|
|
19
|
-
|
|
26
|
+
| Feature | Description |
|
|
27
|
+
| --------------------- | -------------------------------------------------------- |
|
|
28
|
+
| **32+ Barcode Types** | Linear, 2D, postal, stacked, and auto-detection variants |
|
|
29
|
+
| **Multiple Formats** | PNG, SVG, HTML, JPG, PDF output support |
|
|
30
|
+
| **Framework Ready** | Express.js middleware and route helpers included |
|
|
31
|
+
| **CLI Tool** | Generate barcodes directly from terminal |
|
|
32
|
+
| **Advanced QR Codes** | Logos, watermarks, labels, and customization |
|
|
33
|
+
| **Validation** | Built-in data validation for all barcode types |
|
|
34
|
+
| **Batch Processing** | Generate multiple barcodes simultaneously |
|
|
35
|
+
| **High Performance** | Optimized for enterprise-scale generation |
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## 📦 Installation
|
|
20
40
|
|
|
21
41
|
```bash
|
|
22
42
|
npm install isahaq-barcode
|
|
23
43
|
```
|
|
24
44
|
|
|
25
|
-
|
|
45
|
+
**Requirements:** Node.js 18.0 or higher
|
|
26
46
|
|
|
27
|
-
|
|
28
|
-
- Canvas library (automatically installed)
|
|
47
|
+
## 🌐 Browser Compatibility
|
|
29
48
|
|
|
30
|
-
|
|
49
|
+
> **Important:** This package is designed for **Node.js server-side environments** and is not compatible with browser environments.
|
|
31
50
|
|
|
32
|
-
|
|
33
|
-
2. **Multiple Formats**: Generate barcodes in PNG, SVG, HTML, JPG, and PDF
|
|
34
|
-
3. **Express.js Ready**: Seamless integration with Express.js framework
|
|
35
|
-
4. **CLI Support**: Generate barcodes from command line
|
|
36
|
-
5. **Advanced QR Codes**: Customizable QR codes with logos and labels
|
|
37
|
-
6. **Validation**: Built-in data validation for each barcode type
|
|
38
|
-
7. **Performance**: Optimized for high-volume generation
|
|
39
|
-
8. **Extensible**: Easy to add new barcode types and renderers
|
|
51
|
+
### Why Not Browser Compatible?
|
|
40
52
|
|
|
41
|
-
|
|
53
|
+
- **Canvas Dependency**: Uses Node.js `canvas` library for image generation
|
|
54
|
+
- **File System Access**: Requires Node.js `fs` module for file operations
|
|
55
|
+
- **Server-Side Rendering**: Designed for server-side barcode generation
|
|
42
56
|
|
|
43
|
-
###
|
|
57
|
+
### Recommended Usage Patterns
|
|
44
58
|
|
|
45
|
-
-
|
|
46
|
-
- **Code39** (Standard, Checksum, Extended, Auto)
|
|
47
|
-
- **Code93**
|
|
48
|
-
- **Code25** (Standard, Auto)
|
|
49
|
-
- **Code32** (Italian Pharmacode)
|
|
50
|
-
- **Standard25** (Standard, Checksum)
|
|
51
|
-
- **Interleaved25** (Standard, Checksum, Auto)
|
|
52
|
-
- **MSI** (Standard, Checksum, Auto)
|
|
59
|
+
**✅ Server-Side (Recommended):**
|
|
53
60
|
|
|
54
|
-
|
|
61
|
+
```javascript
|
|
62
|
+
// Node.js/Express.js server
|
|
63
|
+
const BarcodeGenerator = require('isahaq-barcode');
|
|
55
64
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
65
|
+
// Generate barcode on server
|
|
66
|
+
app.get('/barcode/:data', (req, res) => {
|
|
67
|
+
const barcode = BarcodeGenerator.png(req.params.data, 'code128');
|
|
68
|
+
res.set('Content-Type', 'image/png');
|
|
69
|
+
res.send(barcode);
|
|
70
|
+
});
|
|
71
|
+
```
|
|
59
72
|
|
|
60
|
-
|
|
73
|
+
**✅ Next.js API Routes:**
|
|
61
74
|
|
|
62
|
-
|
|
75
|
+
```javascript
|
|
76
|
+
// pages/api/barcode.js or app/api/barcode/route.js
|
|
77
|
+
import BarcodeGenerator from 'isahaq-barcode';
|
|
63
78
|
|
|
64
|
-
|
|
79
|
+
export default function handler(req, res) {
|
|
80
|
+
const barcode = BarcodeGenerator.png(req.query.data, 'code128');
|
|
81
|
+
res.setHeader('Content-Type', 'image/png');
|
|
82
|
+
res.send(barcode);
|
|
83
|
+
}
|
|
84
|
+
```
|
|
65
85
|
|
|
66
|
-
-
|
|
86
|
+
**❌ Client-Side (Not Supported):**
|
|
67
87
|
|
|
68
|
-
|
|
88
|
+
```javascript
|
|
89
|
+
// This will NOT work in browsers
|
|
90
|
+
import BarcodeGenerator from 'isahaq-barcode'; // ❌
|
|
91
|
+
```
|
|
69
92
|
|
|
70
|
-
|
|
93
|
+
### Alternative for Client-Side
|
|
71
94
|
|
|
72
|
-
|
|
95
|
+
For browser-based barcode generation, consider:
|
|
73
96
|
|
|
74
|
-
- **
|
|
97
|
+
- **jsbarcode** - Client-side barcode generation
|
|
98
|
+
- **qrcode** - Client-side QR code generation
|
|
99
|
+
- **Server-side API** - Use this package on your backend
|
|
75
100
|
|
|
76
|
-
|
|
101
|
+
**Global CLI Installation:**
|
|
77
102
|
|
|
78
|
-
|
|
103
|
+
```bash
|
|
104
|
+
npm install -g isahaq-barcode
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## 🚀 Quick Start
|
|
79
110
|
|
|
80
111
|
```javascript
|
|
81
112
|
const BarcodeGenerator = require('isahaq-barcode');
|
|
@@ -86,93 +117,112 @@ const pngBuffer = BarcodeGenerator.png('1234567890', 'code128');
|
|
|
86
117
|
// Generate SVG barcode
|
|
87
118
|
const svgString = BarcodeGenerator.svg('1234567890', 'ean13');
|
|
88
119
|
|
|
89
|
-
// Generate
|
|
90
|
-
const
|
|
120
|
+
// Generate QR code with logo
|
|
121
|
+
const qrCode = BarcodeGenerator.modernQr({
|
|
122
|
+
data: 'https://example.com',
|
|
123
|
+
size: 300,
|
|
124
|
+
logoPath: 'path/to/logo.png',
|
|
125
|
+
label: 'Scan me!',
|
|
126
|
+
});
|
|
91
127
|
|
|
92
|
-
|
|
93
|
-
const pdfBuffer = await BarcodeGenerator.pdf('1234567890', 'code128');
|
|
128
|
+
await qrCode.saveToFile('qr-code.png');
|
|
94
129
|
```
|
|
95
130
|
|
|
96
|
-
|
|
131
|
+
---
|
|
97
132
|
|
|
98
|
-
|
|
99
|
-
|
|
133
|
+
## 📊 Supported Barcode Types
|
|
134
|
+
|
|
135
|
+
### Linear Barcodes (1D)
|
|
136
|
+
|
|
137
|
+
| Type | Variants | Use Case |
|
|
138
|
+
| -------------- | ---------------------------------- | ----------------------------- |
|
|
139
|
+
| **Code 128** | A, B, C, Auto | General purpose, high density |
|
|
140
|
+
| **Code 39** | Standard, Extended, Checksum, Auto | Alphanumeric, automotive |
|
|
141
|
+
| **Code 93** | - | Compact alphanumeric |
|
|
142
|
+
| **EAN Family** | EAN-13, EAN-8, EAN-2, EAN-5 | Retail products (Europe) |
|
|
143
|
+
| **UPC Family** | UPC-A, UPC-E | Retail products (USA) |
|
|
144
|
+
| **Code 25** | Standard, Interleaved, Auto | Industrial, logistics |
|
|
145
|
+
| **ITF-14** | - | Shipping containers |
|
|
146
|
+
| **MSI** | Standard, Checksum, Auto | Inventory management |
|
|
147
|
+
| **Codabar** | - | Libraries, blood banks |
|
|
148
|
+
| **Code 11** | - | Telecommunications |
|
|
149
|
+
|
|
150
|
+
### 2D Barcodes
|
|
151
|
+
|
|
152
|
+
| Type | Data Capacity | Use Case |
|
|
153
|
+
| --------------- | ----------------- | ----------------------- |
|
|
154
|
+
| **QR Code** | Up to 4,296 chars | URLs, payments, general |
|
|
155
|
+
| **Data Matrix** | Up to 2,335 chars | Small item marking |
|
|
156
|
+
| **Aztec** | Up to 3,832 chars | Transport tickets |
|
|
157
|
+
| **PDF417** | Up to 1,850 chars | IDs, boarding passes |
|
|
158
|
+
| **Micro QR** | Up to 35 chars | Compact applications |
|
|
159
|
+
| **MaxiCode** | Up to 93 chars | Package tracking |
|
|
160
|
+
|
|
161
|
+
### Postal Barcodes
|
|
100
162
|
|
|
101
|
-
|
|
102
|
-
const barcodeBuffer = BarcodeGenerator.png('1234567890', 'code128');
|
|
163
|
+
**POSTNET** • **PLANET** • **RMS4CC** • **KIX** • **IMB**
|
|
103
164
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
165
|
+
### Specialized
|
|
166
|
+
|
|
167
|
+
**Code 32** (Italian Pharmacode) • **PharmaCode** • **PharmaCodeTwoTracks** • **Code 16K** • **Code 49**
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
## 💻 Usage Examples
|
|
172
|
+
|
|
173
|
+
### Basic Generation
|
|
174
|
+
|
|
175
|
+
```javascript
|
|
176
|
+
const BarcodeGenerator = require('isahaq-barcode');
|
|
109
177
|
|
|
110
|
-
//
|
|
111
|
-
const
|
|
178
|
+
// PNG with custom options
|
|
179
|
+
const barcode = BarcodeGenerator.png('1234567890', 'code128', {
|
|
112
180
|
width: 3,
|
|
113
181
|
height: 150,
|
|
114
|
-
displayValue:
|
|
115
|
-
foregroundColor: '#
|
|
182
|
+
displayValue: true,
|
|
183
|
+
foregroundColor: '#000000',
|
|
116
184
|
backgroundColor: '#FFFFFF',
|
|
185
|
+
margin: 10,
|
|
117
186
|
});
|
|
187
|
+
|
|
188
|
+
// Convert to Base64 for web display
|
|
189
|
+
const base64Image = barcode.toString('base64');
|
|
190
|
+
console.log(`<img src="data:image/png;base64,${base64Image}" alt="Barcode" />`);
|
|
118
191
|
```
|
|
119
192
|
|
|
120
|
-
### QR Code with Logo
|
|
193
|
+
### Advanced QR Code with Logo
|
|
121
194
|
|
|
122
195
|
```javascript
|
|
123
|
-
const BarcodeGenerator = require('isahaq-barcode');
|
|
124
|
-
|
|
125
196
|
const qrCode = BarcodeGenerator.modernQr({
|
|
126
197
|
data: 'https://example.com',
|
|
127
198
|
size: 300,
|
|
128
199
|
margin: 10,
|
|
200
|
+
|
|
201
|
+
// Logo configuration
|
|
202
|
+
logoPath: 'path/to/logo.png',
|
|
203
|
+
logoSize: 60, // Percentage
|
|
204
|
+
|
|
205
|
+
// Text elements
|
|
206
|
+
label: 'Scan me!',
|
|
207
|
+
watermark: 'Company Name',
|
|
208
|
+
watermarkPosition: 'bottom-center',
|
|
209
|
+
|
|
210
|
+
// Colors
|
|
129
211
|
foregroundColor: [0, 0, 0],
|
|
130
212
|
backgroundColor: [255, 255, 255],
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
watermark: 'Watermark Text', // Watermark
|
|
135
|
-
watermarkPosition: 'center',
|
|
136
|
-
errorCorrectionLevel: 'H', // Use H for better logo support
|
|
213
|
+
|
|
214
|
+
// Error correction (H recommended for logos)
|
|
215
|
+
errorCorrectionLevel: 'H',
|
|
137
216
|
});
|
|
138
217
|
|
|
139
218
|
// Save to file
|
|
140
|
-
await qrCode.saveToFile('qr-code
|
|
219
|
+
await qrCode.saveToFile('qr-code.png');
|
|
141
220
|
|
|
142
221
|
// Get as data URI
|
|
143
222
|
const dataUri = await qrCode.getDataUri();
|
|
144
|
-
console.log(`<img src="${dataUri}" alt="QR Code with Logo" />`);
|
|
145
223
|
```
|
|
146
224
|
|
|
147
|
-
###
|
|
148
|
-
|
|
149
|
-
```javascript
|
|
150
|
-
const { BarcodeService } = require('isahaq-barcode');
|
|
151
|
-
|
|
152
|
-
const barcodeService = new BarcodeService();
|
|
153
|
-
|
|
154
|
-
// Generate different formats
|
|
155
|
-
const pngData = barcodeService.png('1234567890', 'code128');
|
|
156
|
-
const svgData = barcodeService.svg('1234567890', 'ean13');
|
|
157
|
-
const htmlData = barcodeService.html('1234567890', 'code39');
|
|
158
|
-
|
|
159
|
-
// Generate with custom options
|
|
160
|
-
const options = {
|
|
161
|
-
width: 300,
|
|
162
|
-
height: 100,
|
|
163
|
-
displayValue: true,
|
|
164
|
-
foregroundColor: '#000000',
|
|
165
|
-
backgroundColor: '#FFFFFF',
|
|
166
|
-
};
|
|
167
|
-
const customBarcode = barcodeService.generate(
|
|
168
|
-
'1234567890',
|
|
169
|
-
'code128',
|
|
170
|
-
'png',
|
|
171
|
-
options
|
|
172
|
-
);
|
|
173
|
-
```
|
|
174
|
-
|
|
175
|
-
### QR Code Builder (Advanced)
|
|
225
|
+
### QR Code Builder Pattern
|
|
176
226
|
|
|
177
227
|
```javascript
|
|
178
228
|
const { QrCodeBuilder } = require('isahaq-barcode');
|
|
@@ -190,83 +240,115 @@ const qrCode = QrCodeBuilder.create()
|
|
|
190
240
|
.format('png')
|
|
191
241
|
.build();
|
|
192
242
|
|
|
193
|
-
// Save to file
|
|
194
243
|
await qrCode.saveToFile('qr-code.png');
|
|
244
|
+
```
|
|
195
245
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
246
|
+
### Batch Generation
|
|
247
|
+
|
|
248
|
+
```javascript
|
|
249
|
+
const items = [
|
|
250
|
+
{ data: '1234567890', type: 'code128', format: 'png' },
|
|
251
|
+
{ data: '9876543210', type: 'code39', format: 'svg' },
|
|
252
|
+
{ data: 'https://example.com', type: 'qrcode', format: 'png' },
|
|
253
|
+
];
|
|
254
|
+
|
|
255
|
+
const results = BarcodeGenerator.batch(items);
|
|
256
|
+
|
|
257
|
+
results.forEach((result, index) => {
|
|
258
|
+
if (result.success) {
|
|
259
|
+
console.log(`✓ Barcode ${index} generated`);
|
|
260
|
+
// result.data contains the generated barcode
|
|
261
|
+
} else {
|
|
262
|
+
console.error(`✗ Barcode ${index} failed: ${result.error}`);
|
|
263
|
+
}
|
|
264
|
+
});
|
|
199
265
|
```
|
|
200
266
|
|
|
201
|
-
|
|
267
|
+
### Data Validation
|
|
202
268
|
|
|
203
|
-
|
|
269
|
+
```javascript
|
|
270
|
+
const validation = BarcodeGenerator.validate('1234567890', 'code128');
|
|
271
|
+
|
|
272
|
+
if (validation.valid) {
|
|
273
|
+
console.log('✓ Valid data');
|
|
274
|
+
console.log(` Length: ${validation.length}`);
|
|
275
|
+
console.log(` Charset: ${validation.charset}`);
|
|
276
|
+
} else {
|
|
277
|
+
console.error(`✗ Invalid: ${validation.error}`);
|
|
278
|
+
}
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
---
|
|
282
|
+
|
|
283
|
+
## 🌐 Express.js Integration
|
|
284
|
+
|
|
285
|
+
### Basic Route Implementation
|
|
204
286
|
|
|
205
287
|
```javascript
|
|
206
|
-
const express = require('express')
|
|
207
|
-
const BarcodeGenerator = require('isahaq-barcode')
|
|
288
|
+
const express = require('express');
|
|
289
|
+
const BarcodeGenerator = require('isahaq-barcode');
|
|
208
290
|
|
|
209
|
-
const app = express()
|
|
291
|
+
const app = express();
|
|
210
292
|
|
|
211
|
-
//
|
|
293
|
+
// Barcode endpoint
|
|
212
294
|
app.get('/barcode/:data', (req, res) => {
|
|
213
|
-
const { data } = req.params
|
|
214
|
-
const { type = 'code128', format = 'png' } = req.query
|
|
295
|
+
const { data } = req.params;
|
|
296
|
+
const { type = 'code128', format = 'png' } = req.query;
|
|
215
297
|
|
|
216
298
|
try {
|
|
217
|
-
let result
|
|
299
|
+
let result, contentType;
|
|
300
|
+
|
|
218
301
|
switch (format) {
|
|
219
302
|
case 'png':
|
|
220
|
-
result = BarcodeGenerator.png(data, type)
|
|
221
|
-
|
|
222
|
-
break
|
|
303
|
+
result = BarcodeGenerator.png(data, type);
|
|
304
|
+
contentType = 'image/png';
|
|
305
|
+
break;
|
|
223
306
|
case 'svg':
|
|
224
|
-
result = BarcodeGenerator.svg(data, type)
|
|
225
|
-
|
|
226
|
-
break
|
|
307
|
+
result = BarcodeGenerator.svg(data, type);
|
|
308
|
+
contentType = 'image/svg+xml';
|
|
309
|
+
break;
|
|
227
310
|
case 'html':
|
|
228
|
-
result = BarcodeGenerator.html(data, type)
|
|
229
|
-
|
|
230
|
-
break
|
|
311
|
+
result = BarcodeGenerator.html(data, type);
|
|
312
|
+
contentType = 'text/html';
|
|
313
|
+
break;
|
|
231
314
|
default:
|
|
232
|
-
|
|
315
|
+
return res.status(400).json({ error: 'Invalid format' });
|
|
233
316
|
}
|
|
234
317
|
|
|
235
|
-
res.
|
|
318
|
+
res.set('Content-Type', contentType);
|
|
319
|
+
res.send(result);
|
|
236
320
|
} catch (error) {
|
|
237
|
-
res.status(400).json({ error: error.message })
|
|
321
|
+
res.status(400).json({ error: error.message });
|
|
238
322
|
}
|
|
239
|
-
})
|
|
323
|
+
});
|
|
240
324
|
|
|
241
|
-
//
|
|
242
|
-
app.get('/qr/:data', (req, res) => {
|
|
243
|
-
const { data } = req.params
|
|
244
|
-
const { size = 300, logo, label } = req.query
|
|
325
|
+
// QR code endpoint
|
|
326
|
+
app.get('/qr/:data', async (req, res) => {
|
|
327
|
+
const { data } = req.params;
|
|
328
|
+
const { size = 300, logo, label } = req.query;
|
|
245
329
|
|
|
246
330
|
try {
|
|
247
|
-
const
|
|
331
|
+
const qrCode = BarcodeGenerator.modernQr({
|
|
248
332
|
data: decodeURIComponent(data),
|
|
249
333
|
size: parseInt(size),
|
|
250
334
|
logoPath: logo,
|
|
251
|
-
label: label
|
|
252
|
-
}
|
|
335
|
+
label: label,
|
|
336
|
+
});
|
|
253
337
|
|
|
254
|
-
const
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
res.set('Content-Type', 'image/png')
|
|
258
|
-
res.send(result)
|
|
338
|
+
const result = await qrCode.generate();
|
|
339
|
+
res.set('Content-Type', 'image/png');
|
|
340
|
+
res.send(result);
|
|
259
341
|
} catch (error) {
|
|
260
|
-
res.status(400).json({ error: error.message })
|
|
342
|
+
res.status(400).json({ error: error.message });
|
|
261
343
|
}
|
|
262
|
-
})
|
|
344
|
+
});
|
|
263
345
|
|
|
264
346
|
app.listen(3000, () => {
|
|
265
|
-
console.log('
|
|
266
|
-
})
|
|
347
|
+
console.log('🚀 Barcode server running on port 3000');
|
|
348
|
+
});
|
|
267
349
|
```
|
|
268
350
|
|
|
269
|
-
###
|
|
351
|
+
### Middleware Pattern
|
|
270
352
|
|
|
271
353
|
```javascript
|
|
272
354
|
const express = require('express');
|
|
@@ -294,16 +376,16 @@ app.use('/api/barcode', (req, res, next) => {
|
|
|
294
376
|
throw new Error(`Unsupported format: ${format}`);
|
|
295
377
|
}
|
|
296
378
|
} catch (error) {
|
|
297
|
-
throw new Error(`
|
|
379
|
+
throw new Error(`Generation failed: ${error.message}`);
|
|
298
380
|
}
|
|
299
381
|
};
|
|
300
382
|
next();
|
|
301
383
|
});
|
|
302
384
|
|
|
303
|
-
//
|
|
385
|
+
// Route using middleware
|
|
304
386
|
app.get('/api/barcode/:data', (req, res) => {
|
|
305
387
|
const { data } = req.params;
|
|
306
|
-
const { type, format, width, height } = req.query;
|
|
388
|
+
const { type, format = 'png', width, height } = req.query;
|
|
307
389
|
|
|
308
390
|
const options = {};
|
|
309
391
|
if (width) options.width = parseInt(width);
|
|
@@ -311,240 +393,258 @@ app.get('/api/barcode/:data', (req, res) => {
|
|
|
311
393
|
|
|
312
394
|
try {
|
|
313
395
|
const result = req.generateBarcode(data, type, format, options);
|
|
314
|
-
|
|
396
|
+
const mimeTypes = {
|
|
397
|
+
png: 'image/png',
|
|
398
|
+
svg: 'image/svg+xml',
|
|
399
|
+
html: 'text/html',
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
res.set('Content-Type', mimeTypes[format] || 'image/png');
|
|
315
403
|
res.send(result);
|
|
316
404
|
} catch (error) {
|
|
317
405
|
res.status(400).json({ error: error.message });
|
|
318
406
|
}
|
|
319
407
|
});
|
|
320
|
-
|
|
321
|
-
function getMimeType(format) {
|
|
322
|
-
const mimeTypes = {
|
|
323
|
-
png: 'image/png',
|
|
324
|
-
svg: 'image/svg+xml',
|
|
325
|
-
html: 'text/html',
|
|
326
|
-
};
|
|
327
|
-
return mimeTypes[format] || 'image/png';
|
|
328
|
-
}
|
|
329
408
|
```
|
|
330
409
|
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
### Install CLI globally
|
|
410
|
+
---
|
|
334
411
|
|
|
335
|
-
|
|
336
|
-
npm install -g isahaq-barcode
|
|
337
|
-
```
|
|
412
|
+
## 🖥️ CLI Usage
|
|
338
413
|
|
|
339
|
-
### Generate
|
|
414
|
+
### Generate Barcodes
|
|
340
415
|
|
|
341
416
|
```bash
|
|
342
|
-
#
|
|
417
|
+
# Basic PNG barcode
|
|
343
418
|
barcode-generate barcode -d "1234567890" -t code128 -f png -o barcode.png
|
|
344
419
|
|
|
345
|
-
#
|
|
346
|
-
barcode-generate barcode -d "1234567890" -t ean13 -f svg -o barcode.svg
|
|
420
|
+
# SVG with custom dimensions
|
|
421
|
+
barcode-generate barcode -d "1234567890" -t ean13 -f svg -w 3 -h 150 -o barcode.svg
|
|
347
422
|
|
|
348
|
-
#
|
|
349
|
-
barcode-generate barcode -d "1234567890" -t code128
|
|
423
|
+
# Custom colors
|
|
424
|
+
barcode-generate barcode -d "1234567890" -t code128 \
|
|
425
|
+
--foreground "#FF0000" --background "#FFFFFF" -o red-barcode.png
|
|
350
426
|
```
|
|
351
427
|
|
|
352
|
-
### Generate QR
|
|
428
|
+
### Generate QR Codes
|
|
353
429
|
|
|
354
430
|
```bash
|
|
355
|
-
#
|
|
356
|
-
barcode-generate qr -d "https://example.com" -s 300 -
|
|
431
|
+
# Basic QR code
|
|
432
|
+
barcode-generate qr -d "https://example.com" -s 300 -o qr.png
|
|
357
433
|
|
|
358
|
-
#
|
|
359
|
-
barcode-generate qr -d "https://example.com" -s 300
|
|
434
|
+
# QR code with logo
|
|
435
|
+
barcode-generate qr -d "https://example.com" -s 300 \
|
|
436
|
+
--logo "path/to/logo.png" --logo-size 60 -o qr-logo.png
|
|
360
437
|
|
|
361
|
-
#
|
|
362
|
-
barcode-generate qr -d "https://example.com" -s 300
|
|
438
|
+
# QR code with watermark
|
|
439
|
+
barcode-generate qr -d "https://example.com" -s 300 \
|
|
440
|
+
--watermark "Company Name" --watermark-position bottom-center -o qr-watermark.png
|
|
363
441
|
```
|
|
364
442
|
|
|
365
|
-
### Batch
|
|
443
|
+
### Batch Operations
|
|
366
444
|
|
|
367
445
|
```bash
|
|
368
|
-
# Create batch file
|
|
369
|
-
|
|
446
|
+
# Create batch configuration file
|
|
447
|
+
cat > batch.json << EOF
|
|
448
|
+
[
|
|
370
449
|
{"data": "1234567890", "type": "code128", "format": "png"},
|
|
371
450
|
{"data": "9876543210", "type": "code39", "format": "svg"},
|
|
372
451
|
{"data": "https://example.com", "type": "qrcode", "format": "png"}
|
|
373
|
-
]
|
|
452
|
+
]
|
|
453
|
+
EOF
|
|
374
454
|
|
|
375
455
|
# Generate batch
|
|
376
456
|
barcode-generate batch -i batch.json -o ./output
|
|
377
457
|
```
|
|
378
458
|
|
|
379
|
-
###
|
|
459
|
+
### Utility Commands
|
|
380
460
|
|
|
381
461
|
```bash
|
|
382
|
-
# List barcode types
|
|
462
|
+
# List supported barcode types
|
|
383
463
|
barcode-generate types
|
|
384
464
|
|
|
385
465
|
# List output formats
|
|
386
466
|
barcode-generate formats
|
|
387
467
|
|
|
388
|
-
# Validate data
|
|
468
|
+
# Validate data for specific type
|
|
389
469
|
barcode-generate validate -d "1234567890" -t code128
|
|
390
470
|
```
|
|
391
471
|
|
|
392
|
-
|
|
472
|
+
---
|
|
393
473
|
|
|
394
|
-
|
|
474
|
+
## 📚 API Reference
|
|
475
|
+
|
|
476
|
+
### BarcodeGenerator (Static Methods)
|
|
477
|
+
|
|
478
|
+
#### Generation Methods
|
|
395
479
|
|
|
396
480
|
```javascript
|
|
397
|
-
|
|
481
|
+
BarcodeGenerator.png(data, type, options?)
|
|
482
|
+
// Returns: Buffer
|
|
398
483
|
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
{ data: '9876543210', type: 'code39', format: 'svg' },
|
|
402
|
-
{ data: 'https://example.com', type: 'qrcode', format: 'png' },
|
|
403
|
-
];
|
|
484
|
+
BarcodeGenerator.svg(data, type, options?)
|
|
485
|
+
// Returns: String
|
|
404
486
|
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
487
|
+
BarcodeGenerator.html(data, type, options?)
|
|
488
|
+
// Returns: String
|
|
489
|
+
|
|
490
|
+
BarcodeGenerator.pdf(data, type, options?)
|
|
491
|
+
// Returns: Promise<Buffer>
|
|
492
|
+
|
|
493
|
+
BarcodeGenerator.modernQr(options)
|
|
494
|
+
// Returns: QrCodeInstance
|
|
413
495
|
```
|
|
414
496
|
|
|
415
|
-
|
|
497
|
+
#### Utility Methods
|
|
416
498
|
|
|
417
499
|
```javascript
|
|
418
|
-
|
|
500
|
+
BarcodeGenerator.validate(data, type);
|
|
501
|
+
// Returns: { valid: boolean, error?: string, length?: number, charset?: string }
|
|
419
502
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
503
|
+
BarcodeGenerator.batch(items);
|
|
504
|
+
// Returns: Array<{ success: boolean, data?: Buffer|String, error?: string }>
|
|
505
|
+
|
|
506
|
+
BarcodeGenerator.getBarcodeTypes();
|
|
507
|
+
// Returns: Array<string>
|
|
508
|
+
|
|
509
|
+
BarcodeGenerator.getRenderFormats();
|
|
510
|
+
// Returns: Array<string>
|
|
511
|
+
|
|
512
|
+
BarcodeGenerator.getWatermarkPositions();
|
|
513
|
+
// Returns: Array<string>
|
|
429
514
|
```
|
|
430
515
|
|
|
431
|
-
###
|
|
516
|
+
### Options Object
|
|
432
517
|
|
|
433
518
|
```javascript
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
519
|
+
{
|
|
520
|
+
// Dimensions
|
|
521
|
+
width: 3, // Bar width
|
|
522
|
+
height: 150, // Bar height
|
|
523
|
+
|
|
524
|
+
// Display
|
|
525
|
+
displayValue: true, // Show text below barcode
|
|
526
|
+
fontSize: 20, // Text font size
|
|
527
|
+
textAlign: 'center', // 'left' | 'center' | 'right'
|
|
528
|
+
textPosition: 'bottom', // 'top' | 'bottom'
|
|
529
|
+
textMargin: 2, // Space between bars and text
|
|
530
|
+
|
|
531
|
+
// Colors
|
|
532
|
+
background: '#ffffff', // Background color
|
|
533
|
+
lineColor: '#000000', // Bar color (alias: foregroundColor)
|
|
534
|
+
|
|
535
|
+
// Margins
|
|
536
|
+
margin: 10, // All sides
|
|
537
|
+
marginTop: 10, // Individual sides
|
|
448
538
|
marginBottom: 10,
|
|
449
539
|
marginLeft: 10,
|
|
450
|
-
marginRight: 10
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
const barcode = BarcodeGenerator.png('1234567890', 'code128', options);
|
|
540
|
+
marginRight: 10
|
|
541
|
+
}
|
|
454
542
|
```
|
|
455
543
|
|
|
456
|
-
###
|
|
544
|
+
### QrCodeBuilder Methods
|
|
457
545
|
|
|
458
546
|
```javascript
|
|
459
|
-
|
|
547
|
+
QrCodeBuilder.create(options?)
|
|
548
|
+
.data(string) // Set data to encode
|
|
549
|
+
.size(number) // QR code size in pixels
|
|
550
|
+
.margin(number) // Margin size
|
|
551
|
+
.errorCorrectionLevel(level) // 'L' | 'M' | 'Q' | 'H'
|
|
552
|
+
.foregroundColor(rgb) // [R, G, B]
|
|
553
|
+
.backgroundColor(rgb) // [R, G, B]
|
|
554
|
+
.logoPath(string) // Path to logo image
|
|
555
|
+
.logoSize(number) // Logo size percentage
|
|
556
|
+
.label(string) // Label text
|
|
557
|
+
.watermark(text, position) // Watermark text and position
|
|
558
|
+
.format(format) // 'png' | 'svg'
|
|
559
|
+
.build() // Build QR code instance
|
|
560
|
+
|
|
561
|
+
// QR Code Instance Methods
|
|
562
|
+
qrCode.generate() // Returns: Promise<Buffer>
|
|
563
|
+
qrCode.saveToFile(path) // Returns: Promise<void>
|
|
564
|
+
qrCode.getDataUri() // Returns: Promise<string>
|
|
565
|
+
qrCode.getString() // Returns: Promise<string>
|
|
566
|
+
```
|
|
567
|
+
|
|
568
|
+
### Watermark Positions
|
|
460
569
|
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
570
|
+
```javascript
|
|
571
|
+
('top-left',
|
|
572
|
+
'top-center',
|
|
573
|
+
'top-right',
|
|
574
|
+
'left-center',
|
|
575
|
+
'center',
|
|
576
|
+
'right-center',
|
|
577
|
+
'bottom-left',
|
|
578
|
+
'bottom-center',
|
|
579
|
+
'bottom-right');
|
|
464
580
|
```
|
|
465
581
|
|
|
582
|
+
---
|
|
583
|
+
|
|
466
584
|
## 🧪 Testing
|
|
467
585
|
|
|
468
586
|
```bash
|
|
469
|
-
# Run tests
|
|
587
|
+
# Run all tests
|
|
470
588
|
npm test
|
|
471
589
|
|
|
472
|
-
# Run
|
|
590
|
+
# Run with coverage report
|
|
473
591
|
npm run test:coverage
|
|
474
592
|
|
|
475
|
-
#
|
|
593
|
+
# Watch mode for development
|
|
476
594
|
npm run test:watch
|
|
477
595
|
```
|
|
478
596
|
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
### BarcodeGenerator
|
|
482
|
-
|
|
483
|
-
#### Methods
|
|
484
|
-
|
|
485
|
-
- `png(data, type, options)` - Generate PNG barcode
|
|
486
|
-
- `svg(data, type, options)` - Generate SVG barcode
|
|
487
|
-
- `html(data, type, options)` - Generate HTML barcode
|
|
488
|
-
- `pdf(data, type, options)` - Generate PDF barcode
|
|
489
|
-
- `modernQr(options)` - Generate QR code with advanced features
|
|
490
|
-
- `validate(data, type)` - Validate data for barcode type
|
|
491
|
-
- `batch(items)` - Generate multiple barcodes
|
|
492
|
-
- `getBarcodeTypes()` - Get supported barcode types
|
|
493
|
-
- `getRenderFormats()` - Get supported output formats
|
|
494
|
-
- `getWatermarkPositions()` - Get watermark positions
|
|
495
|
-
|
|
496
|
-
### QrCodeBuilder
|
|
497
|
-
|
|
498
|
-
#### Methods
|
|
499
|
-
|
|
500
|
-
- `create(options)` - Create new builder instance
|
|
501
|
-
- `data(data)` - Set data to encode
|
|
502
|
-
- `size(size)` - Set QR code size
|
|
503
|
-
- `margin(margin)` - Set margin
|
|
504
|
-
- `errorCorrectionLevel(level)` - Set error correction level
|
|
505
|
-
- `foregroundColor(color)` - Set foreground color
|
|
506
|
-
- `backgroundColor(color)` - Set background color
|
|
507
|
-
- `logoPath(path)` - Set logo path
|
|
508
|
-
- `logoSize(size)` - Set logo size
|
|
509
|
-
- `label(text)` - Set label text
|
|
510
|
-
- `watermark(text, position)` - Set watermark
|
|
511
|
-
- `format(format)` - Set output format
|
|
512
|
-
- `build()` - Build QR code
|
|
513
|
-
- `generate()` - Generate QR code buffer
|
|
514
|
-
- `saveToFile(path)` - Save to file
|
|
515
|
-
- `getDataUri()` - Get data URI
|
|
516
|
-
- `getString()` - Get QR code string
|
|
597
|
+
---
|
|
517
598
|
|
|
518
599
|
## 🤝 Contributing
|
|
519
600
|
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
601
|
+
We welcome contributions! Please follow these steps:
|
|
602
|
+
|
|
603
|
+
1. **Fork** the repository
|
|
604
|
+
2. **Create** a feature branch: `git checkout -b feature/amazing-feature`
|
|
605
|
+
3. **Commit** your changes: `git commit -m 'Add amazing feature'`
|
|
606
|
+
4. **Push** to the branch: `git push origin feature/amazing-feature`
|
|
607
|
+
5. **Open** a Pull Request
|
|
608
|
+
|
|
609
|
+
Please ensure your code:
|
|
610
|
+
|
|
611
|
+
- Follows the existing code style
|
|
612
|
+
- Includes tests for new features
|
|
613
|
+
- Updates documentation as needed
|
|
614
|
+
|
|
615
|
+
---
|
|
525
616
|
|
|
526
617
|
## 📄 License
|
|
527
618
|
|
|
528
619
|
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
529
620
|
|
|
621
|
+
---
|
|
622
|
+
|
|
530
623
|
## 🙏 Acknowledgments
|
|
531
624
|
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
- [
|
|
535
|
-
- [
|
|
625
|
+
Built with these excellent libraries:
|
|
626
|
+
|
|
627
|
+
- [JsBarcode](https://github.com/lindell/JsBarcode) - Barcode generation
|
|
628
|
+
- [node-qrcode](https://github.com/soldair/node-qrcode) - QR code generation
|
|
629
|
+
- [node-canvas](https://github.com/Automattic/node-canvas) - Canvas implementation
|
|
630
|
+
- [PDFKit](https://github.com/foliojs/pdfkit) - PDF generation
|
|
631
|
+
|
|
632
|
+
---
|
|
536
633
|
|
|
537
634
|
## 📞 Support
|
|
538
635
|
|
|
539
|
-
|
|
636
|
+
Need help? Here's how to get support:
|
|
540
637
|
|
|
541
|
-
1. Check the [documentation](README.md)
|
|
542
|
-
2. Search [existing issues](https://github.com/isahaq1
|
|
543
|
-
3. Create a [new issue](https://github.com/isahaq1
|
|
638
|
+
1. 📖 Check the [documentation](README.md)
|
|
639
|
+
2. 🔍 Search [existing issues](https://github.com/isahaq1//npm-barcodegeneratop/issues)
|
|
640
|
+
3. 💬 Create a [new issue](https://github.com/isahaq1//npm-barcodegeneratop/issues/new)
|
|
544
641
|
|
|
545
642
|
---
|
|
546
643
|
|
|
644
|
+
<div align="center">
|
|
645
|
+
|
|
547
646
|
**Made with ❤️ by [Isahaq](https://github.com/isahaq1)**
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
647
|
+
|
|
648
|
+
[⭐ Star this repo](https://github.com/isahaq1//npm-barcodegeneratop) • [🐛 Report Bug](https://github.com/isahaq1//npm-barcodegeneratop/issues) • [✨ Request Feature](https://github.com/isahaq1//npm-barcodegeneratop/issues)
|
|
649
|
+
|
|
650
|
+
</div>
|
package/package.json
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "isahaq-barcode",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "A universal barcode generator package supporting multiple barcode types and output formats, with extra features like batch generation, watermarking, validation, CLI, and Express.js integration",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"browser": {
|
|
7
|
+
"fs": false,
|
|
8
|
+
"path": false,
|
|
9
|
+
"os": false
|
|
10
|
+
},
|
|
6
11
|
"bin": {
|
|
7
12
|
"barcode-generate": "./bin/generate.js"
|
|
8
13
|
},
|
|
@@ -4,7 +4,15 @@
|
|
|
4
4
|
|
|
5
5
|
const QRCode = require('qrcode');
|
|
6
6
|
const { createCanvas, loadImage } = require('canvas');
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
// Conditional fs import for Node.js environments only
|
|
9
|
+
let fs;
|
|
10
|
+
try {
|
|
11
|
+
fs = require('fs').promises;
|
|
12
|
+
} catch {
|
|
13
|
+
// fs not available in browser environments
|
|
14
|
+
fs = null;
|
|
15
|
+
}
|
|
8
16
|
|
|
9
17
|
class QrCodeBuilder {
|
|
10
18
|
constructor(options = {}) {
|
|
@@ -375,6 +383,11 @@ class QrCodeResult {
|
|
|
375
383
|
* @returns {Promise<void>}
|
|
376
384
|
*/
|
|
377
385
|
async saveToFile(filePath) {
|
|
386
|
+
if (!fs) {
|
|
387
|
+
throw new Error(
|
|
388
|
+
'File system operations are not available in browser environments. Use generate() or getDataUri() methods instead.'
|
|
389
|
+
);
|
|
390
|
+
}
|
|
378
391
|
const buffer = await this.generate();
|
|
379
392
|
await fs.writeFile(filePath, buffer);
|
|
380
393
|
}
|