isahaq-barcode 1.0.0

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/.eslintrc.js ADDED
@@ -0,0 +1,29 @@
1
+ module.exports = {
2
+ env: {
3
+ node: true,
4
+ es2021: true,
5
+ jest: true,
6
+ },
7
+ extends: ['standard'],
8
+ parserOptions: {
9
+ ecmaVersion: 12,
10
+ sourceType: 'module',
11
+ },
12
+ rules: {
13
+ 'no-console': 'warn',
14
+ 'no-unused-vars': 'error',
15
+ 'prefer-const': 'error',
16
+ 'no-var': 'error',
17
+ 'object-shorthand': 'error',
18
+ 'prefer-template': 'error',
19
+ },
20
+ globals: {
21
+ Buffer: 'readonly',
22
+ process: 'readonly',
23
+ __dirname: 'readonly',
24
+ __filename: 'readonly',
25
+ module: 'readonly',
26
+ require: 'readonly',
27
+ exports: 'readonly',
28
+ },
29
+ };
package/README.md ADDED
@@ -0,0 +1,549 @@
1
+ # Isahaq Barcode Generator
2
+
3
+ A universal barcode generator package supporting **32+ barcode types** (linear, 2D, postal, stacked, and auto-detection variants), multiple output formats, CLI, and Express.js integration.
4
+
5
+ ## ๐Ÿš€ Features
6
+
7
+ - **32+ Barcode Types**: Linear, 2D, postal, stacked, and auto-detection variants
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
16
+
17
+ ## ๐Ÿ“ฆ Installation
18
+
19
+ ### Via npm
20
+
21
+ ```bash
22
+ npm install isahaq-barcode
23
+ ```
24
+
25
+ ### Requirements
26
+
27
+ - Node.js 14.0 or higher
28
+ - Canvas library (automatically installed)
29
+
30
+ ## ๐Ÿ”ง Why Use This Package?
31
+
32
+ 1. **Comprehensive Support**: 32+ barcode types including industry standards and auto-detection
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
40
+
41
+ ## ๐Ÿ“‹ Supported Barcode Types (32+ Types)
42
+
43
+ ### Linear Barcodes
44
+
45
+ - **Code128** (A, B, C, Auto)
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)
53
+
54
+ ### EAN/UPC Family
55
+
56
+ - **EAN13**, **EAN8**, **EAN2**, **EAN5**
57
+ - **UPC-A**, **UPC-E**
58
+ - **ITF14**
59
+
60
+ ### Postal Barcodes
61
+
62
+ - **POSTNET**, **PLANET**, **RMS4CC**, **KIX**, **IMB**
63
+
64
+ ### Specialized Barcodes
65
+
66
+ - **Codabar**, **Code11**, **PharmaCode**, **PharmaCodeTwoTracks**
67
+
68
+ ### 2D Matrix Codes
69
+
70
+ - **QRCode**, **DataMatrix**, **Aztec**, **PDF417**, **MicroQR**, **Maxicode**
71
+
72
+ ### Stacked Linear Codes
73
+
74
+ - **Code16K**, **Code49**
75
+
76
+ ## ๐Ÿ› ๏ธ Usage
77
+
78
+ ### Basic Node.js Usage
79
+
80
+ ```javascript
81
+ const BarcodeGenerator = require("isahaq-barcode");
82
+
83
+ // Generate PNG barcode
84
+ const pngBuffer = BarcodeGenerator.png("1234567890", "code128");
85
+
86
+ // Generate SVG barcode
87
+ const svgString = BarcodeGenerator.svg("1234567890", "ean13");
88
+
89
+ // Generate HTML barcode
90
+ const htmlString = BarcodeGenerator.html("1234567890", "code39");
91
+
92
+ // Generate PDF barcode
93
+ const pdfBuffer = await BarcodeGenerator.pdf("1234567890", "code128");
94
+ ```
95
+
96
+ ### Display Barcode as Base64 Image (PNG)
97
+
98
+ ```javascript
99
+ const BarcodeGenerator = require("isahaq-barcode");
100
+
101
+ // Generate barcode
102
+ const barcodeBuffer = BarcodeGenerator.png("1234567890", "code128");
103
+
104
+ // Convert to base64
105
+ const barcodeImage = barcodeBuffer.toString("base64");
106
+ console.log(
107
+ `<img src="data:image/png;base64,${barcodeImage}" alt="Barcode" />`
108
+ );
109
+
110
+ // With custom options
111
+ const customBarcode = BarcodeGenerator.png("1234567890", "code128", {
112
+ width: 3,
113
+ height: 150,
114
+ displayValue: false,
115
+ foregroundColor: "#FF0000",
116
+ backgroundColor: "#FFFFFF",
117
+ });
118
+ ```
119
+
120
+ ### QR Code with Logo and Watermark
121
+
122
+ ```javascript
123
+ const BarcodeGenerator = require("isahaq-barcode");
124
+
125
+ const qrCode = BarcodeGenerator.modernQr({
126
+ data: "https://example.com",
127
+ size: 300,
128
+ margin: 10,
129
+ foregroundColor: [0, 0, 0],
130
+ backgroundColor: [255, 255, 255],
131
+ logoPath: "path/to/logo.png", // Logo in the center
132
+ logoSize: 60, // Logo size as percentage
133
+ label: "Scan me!", // Label below QR code
134
+ watermark: "Watermark Text", // Watermark
135
+ watermarkPosition: "center",
136
+ errorCorrectionLevel: "H", // Use H for better logo support
137
+ });
138
+
139
+ // Save to file
140
+ await qrCode.saveToFile("qr-code-with-logo.png");
141
+
142
+ // Get as data URI
143
+ const dataUri = await qrCode.getDataUri();
144
+ console.log(`<img src="${dataUri}" alt="QR Code with Logo" />`);
145
+ ```
146
+
147
+ ### Using the Service Class
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)
176
+
177
+ ```javascript
178
+ const { QrCodeBuilder } = require("isahaq-barcode");
179
+
180
+ const qrCode = QrCodeBuilder.create()
181
+ .data("https://example.com")
182
+ .size(300)
183
+ .margin(10)
184
+ .foregroundColor([0, 0, 0])
185
+ .backgroundColor([255, 255, 255])
186
+ .logoPath("path/to/logo.png")
187
+ .logoSize(60)
188
+ .label("Scan me!")
189
+ .watermark("Watermark Text", "center")
190
+ .format("png")
191
+ .build();
192
+
193
+ // Save to file
194
+ await qrCode.saveToFile("qr-code.png");
195
+
196
+ // Get data URI
197
+ const dataUri = await qrCode.getDataUri();
198
+ console.log(`<img src="${dataUri}" alt="QR Code">`);
199
+ ```
200
+
201
+ ## ๐ŸŽฏ Express.js Integration
202
+
203
+ ### Basic Express.js Usage
204
+
205
+ ```javascript
206
+ const express = require('express')
207
+ const BarcodeGenerator = require('isahaq-barcode')
208
+
209
+ const app = express()
210
+
211
+ // Generate barcode endpoint
212
+ app.get('/barcode/:data', (req, res) => {
213
+ const { data } = req.params
214
+ const { type = 'code128', format = 'png' } = req.query
215
+
216
+ try {
217
+ let result
218
+ switch (format) {
219
+ case 'png':
220
+ result = BarcodeGenerator.png(data, type)
221
+ res.set('Content-Type', 'image/png')
222
+ break
223
+ case 'svg':
224
+ result = BarcodeGenerator.svg(data, type)
225
+ res.set('Content-Type', 'image/svg+xml')
226
+ break
227
+ case 'html':
228
+ result = BarcodeGenerator.html(data, type)
229
+ res.set('Content-Type', 'text/html')
230
+ break
231
+ default:
232
+ throw new Error(`Unsupported format: ${format}`)
233
+ }
234
+
235
+ res.send(result)
236
+ } catch (error) {
237
+ res.status(400).json({ error: error.message })
238
+ }
239
+ })
240
+
241
+ // Generate QR code endpoint
242
+ app.get('/qr/:data', (req, res) => {
243
+ const { data } = req.params
244
+ const { size = 300, logo, label } = req.query
245
+
246
+ try {
247
+ const qrOptions = {
248
+ data: decodeURIComponent(data),
249
+ size: parseInt(size),
250
+ logoPath: logo,
251
+ label: label
252
+ }
253
+
254
+ const qrCode = BarcodeGenerator.modernQr(qrOptions)
255
+ const result = await qrCode.generate()
256
+
257
+ res.set('Content-Type', 'image/png')
258
+ res.send(result)
259
+ } catch (error) {
260
+ res.status(400).json({ error: error.message })
261
+ }
262
+ })
263
+
264
+ app.listen(3000, () => {
265
+ console.log('Server running on port 3000')
266
+ })
267
+ ```
268
+
269
+ ### Express.js Middleware
270
+
271
+ ```javascript
272
+ const express = require("express");
273
+ const BarcodeGenerator = require("isahaq-barcode");
274
+
275
+ const app = express();
276
+
277
+ // Barcode middleware
278
+ app.use("/api/barcode", (req, res, next) => {
279
+ req.generateBarcode = (
280
+ data,
281
+ type = "code128",
282
+ format = "png",
283
+ options = {}
284
+ ) => {
285
+ try {
286
+ switch (format) {
287
+ case "png":
288
+ return BarcodeGenerator.png(data, type, options);
289
+ case "svg":
290
+ return BarcodeGenerator.svg(data, type, options);
291
+ case "html":
292
+ return BarcodeGenerator.html(data, type, options);
293
+ default:
294
+ throw new Error(`Unsupported format: ${format}`);
295
+ }
296
+ } catch (error) {
297
+ throw new Error(`Barcode generation failed: ${error.message}`);
298
+ }
299
+ };
300
+ next();
301
+ });
302
+
303
+ // Use the middleware
304
+ app.get("/api/barcode/:data", (req, res) => {
305
+ const { data } = req.params;
306
+ const { type, format, width, height } = req.query;
307
+
308
+ const options = {};
309
+ if (width) options.width = parseInt(width);
310
+ if (height) options.height = parseInt(height);
311
+
312
+ try {
313
+ const result = req.generateBarcode(data, type, format, options);
314
+ res.set("Content-Type", getMimeType(format));
315
+ res.send(result);
316
+ } catch (error) {
317
+ res.status(400).json({ error: error.message });
318
+ }
319
+ });
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
+ ```
330
+
331
+ ## ๐Ÿ–ฅ๏ธ CLI Usage
332
+
333
+ ### Install CLI globally
334
+
335
+ ```bash
336
+ npm install -g isahaq-barcode
337
+ ```
338
+
339
+ ### Generate Barcode
340
+
341
+ ```bash
342
+ # Generate PNG barcode
343
+ barcode-generate barcode -d "1234567890" -t code128 -f png -o barcode.png
344
+
345
+ # Generate SVG barcode
346
+ barcode-generate barcode -d "1234567890" -t ean13 -f svg -o barcode.svg
347
+
348
+ # Generate with custom options
349
+ barcode-generate barcode -d "1234567890" -t code128 -f png -w 3 -h 150 --foreground "#FF0000" --background "#FFFFFF"
350
+ ```
351
+
352
+ ### Generate QR Code
353
+
354
+ ```bash
355
+ # Generate QR code
356
+ barcode-generate qr -d "https://example.com" -s 300 -f png -o qr.png
357
+
358
+ # Generate QR code with logo
359
+ barcode-generate qr -d "https://example.com" -s 300 --logo "path/to/logo.png" --logo-size 60 -o qr-with-logo.png
360
+
361
+ # Generate QR code with watermark
362
+ barcode-generate qr -d "https://example.com" -s 300 --watermark "Watermark Text" --watermark-position center -o qr-with-watermark.png
363
+ ```
364
+
365
+ ### Batch Generation
366
+
367
+ ```bash
368
+ # Create batch file (batch.json)
369
+ echo '[
370
+ {"data": "1234567890", "type": "code128", "format": "png"},
371
+ {"data": "9876543210", "type": "code39", "format": "svg"},
372
+ {"data": "https://example.com", "type": "qrcode", "format": "png"}
373
+ ]' > batch.json
374
+
375
+ # Generate batch
376
+ barcode-generate batch -i batch.json -o ./output
377
+ ```
378
+
379
+ ### List Supported Types and Formats
380
+
381
+ ```bash
382
+ # List barcode types
383
+ barcode-generate types
384
+
385
+ # List output formats
386
+ barcode-generate formats
387
+
388
+ # Validate data
389
+ barcode-generate validate -d "1234567890" -t code128
390
+ ```
391
+
392
+ ## ๐Ÿ“Š Advanced Features
393
+
394
+ ### Batch Generation
395
+
396
+ ```javascript
397
+ const BarcodeGenerator = require("isahaq-barcode");
398
+
399
+ const items = [
400
+ { data: "1234567890", type: "code128", format: "png" },
401
+ { data: "9876543210", type: "code39", format: "svg" },
402
+ { data: "https://example.com", type: "qrcode", format: "png" },
403
+ ];
404
+
405
+ const results = BarcodeGenerator.batch(items);
406
+ results.forEach((result, index) => {
407
+ if (result.success) {
408
+ console.log(`Barcode ${index} generated successfully`);
409
+ } else {
410
+ console.error(`Barcode ${index} failed: ${result.error}`);
411
+ }
412
+ });
413
+ ```
414
+
415
+ ### Validation
416
+
417
+ ```javascript
418
+ const BarcodeGenerator = require("isahaq-barcode");
419
+
420
+ // Validate data for specific barcode type
421
+ const validation = BarcodeGenerator.validate("1234567890", "code128");
422
+ if (validation.valid) {
423
+ console.log("Data is valid");
424
+ console.log(`Length: ${validation.length}`);
425
+ console.log(`Charset: ${validation.charset}`);
426
+ } else {
427
+ console.error(`Invalid data: ${validation.error}`);
428
+ }
429
+ ```
430
+
431
+ ### Custom Renderer Options
432
+
433
+ ```javascript
434
+ const BarcodeGenerator = require("isahaq-barcode");
435
+
436
+ const options = {
437
+ width: 3,
438
+ height: 150,
439
+ displayValue: true,
440
+ fontSize: 20,
441
+ textAlign: "center",
442
+ textPosition: "bottom",
443
+ textMargin: 2,
444
+ background: "#ffffff",
445
+ lineColor: "#000000",
446
+ margin: 10,
447
+ marginTop: 10,
448
+ marginBottom: 10,
449
+ marginLeft: 10,
450
+ marginRight: 10,
451
+ };
452
+
453
+ const barcode = BarcodeGenerator.png("1234567890", "code128", options);
454
+ ```
455
+
456
+ ### Available Watermark Positions
457
+
458
+ ```javascript
459
+ const BarcodeGenerator = require("isahaq-barcode");
460
+
461
+ const positions = BarcodeGenerator.getWatermarkPositions();
462
+ // Returns: ['top-left', 'top-right', 'bottom-left', 'bottom-right', 'center',
463
+ // 'top-center', 'bottom-center', 'left-center', 'right-center']
464
+ ```
465
+
466
+ ## ๐Ÿงช Testing
467
+
468
+ ```bash
469
+ # Run tests
470
+ npm test
471
+
472
+ # Run tests with coverage
473
+ npm run test:coverage
474
+
475
+ # Run tests in watch mode
476
+ npm run test:watch
477
+ ```
478
+
479
+ ## ๐Ÿ“ API Reference
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
517
+
518
+ ## ๐Ÿค Contributing
519
+
520
+ 1. Fork the repository
521
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
522
+ 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
523
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
524
+ 5. Open a Pull Request
525
+
526
+ ## ๐Ÿ“„ License
527
+
528
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
529
+
530
+ ## ๐Ÿ™ Acknowledgments
531
+
532
+ - [JsBarcode](https://github.com/lindell/JsBarcode) - Barcode generation library
533
+ - [QRCode](https://github.com/soldair/node-qrcode) - QR code generation library
534
+ - [Canvas](https://github.com/Automattic/node-canvas) - Canvas implementation for Node.js
535
+ - [PDFKit](https://github.com/foliojs/pdfkit) - PDF generation library
536
+
537
+ ## ๐Ÿ“ž Support
538
+
539
+ If you have any questions or need help, please:
540
+
541
+ 1. Check the [documentation](README.md)
542
+ 2. Search [existing issues](https://github.com/isahaq1/barcode-generator-npm/issues)
543
+ 3. Create a [new issue](https://github.com/isahaq1/barcode-generator-npm/issues/new)
544
+
545
+ ---
546
+
547
+ **Made with โค๏ธ by [Isahaq](https://github.com/isahaq1)**
548
+ # npm-barcodegeneratop
549
+