Skip to content

Commit 5a28096

Browse files
committed
replace write function. Now working in browser
1 parent 94690a1 commit 5a28096

File tree

3 files changed

+36
-33
lines changed

3 files changed

+36
-33
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# barcode-2-svg
22

3-
Create svg file on server side without canvas
3+
Create svg file on server/browser side without canvas
4+
Work well in all modern browser
45
it's modificated version of [JQUERY PLUGIN : BARCODE](http://barcode-coder.com/en/barcode-jquery-plugin-201.html)
56
Generate 1D barcodes
67

@@ -14,6 +15,7 @@ Generate 1D barcodes
1415
## Requirements
1516

1617
- node >= 0.10.0
18+
- browser != IE
1719

1820
## Installing
1921

@@ -26,11 +28,10 @@ required ones.
2628

2729
```javascript
2830
var barcode = require('barcode-2-svg');
29-
//to file
30-
var code39 = barcode("9234567890128", "ean13",
31-
{width:'50', barWidth:1, barHeight:50});
31+
//to file work only on server side
32+
var code39 = barcode("9234567890128", "code39", {width:50, barWidth:1, barHeight:50, toFile:true});
3233
//return barcode like text
33-
var code13Text = barcode("9234567890128", "ean13",{width:'50', barWidth:1, barHeight:50, toFile:false})
34+
var code13Text = barcode("9234567890128", "ean13", {width:50, barWidth:1, barHeight:50});
3435
console.log(code13Text);
3536
```
3637
type (string)
@@ -46,7 +47,7 @@ type (string)
4647
- int25 (interleaved 2 of 5)
4748

4849
settings (object):
49-
- toFile (bool) -write to file (default: true);
50+
- toFile (bool) -write to file (default: false);
5051
- barHeight (int) -height of svg (default: 30);
5152
- width (int) -width of svg (default: 100);
5253
- bgColor (text) -background color css like (default: 'transparent');

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "barcode-2-svg",
3-
"version": "0.3.3",
3+
"version": "0.4.0",
44
"description": "Create svg file on server side without canvas",
55
"main": "src/index.js",
66
"scripts": {
@@ -13,6 +13,7 @@
1313
"keywords": [
1414
"barcode",
1515
"svg",
16+
"browser",
1617
"node",
1718
"node.js",
1819
"server"

src/index.js

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
* dual licence : http://www.cecill.info/licences/Licence_CeCILL_V2-fr.html
1818
* http://www.gnu.org/licenses/gpl.html
1919
*/
20-
var fs = require('fs');
20+
2121
var barcode = {
2222
settings: {
2323
path: "barcode",
24-
toFile:true,
24+
toFile:false,
2525
width:100,
2626
barWidth: 1,
2727
barHeight: 50,
@@ -38,12 +38,12 @@ var barcode = {
3838
},
3939
intval: function (val) {
4040
var type = typeof( val );
41-
if (type == 'string') {
41+
if (type === 'string') {
4242
val = val.replace(/[^0-9-.]/g, "");
4343
val = parseInt(val * 1, 10);
4444
return isNaN(val) || !isFinite(val) ? 0 : val;
4545
}
46-
return type == 'number' && isFinite(val) ? Math.floor(val) : 0;
46+
return type === 'number' && isFinite(val) ? Math.floor(val) : 0;
4747
},
4848
i25: { // std25 int25
4949
encoding: ["NNWWN", "WNNNW", "NWNNW", "WWNNN", "NNWNW", "WNWNN", "NWWNN", "NNNWW", "WNNWN", "NWNWN"],
@@ -992,11 +992,8 @@ var barcode = {
992992
var fontSize = barcode.intval(settings.fontSize);
993993
height += barcode.intval(settings.marginHRI) + fontSize;
994994
}
995-
var svg ='<g>';
996-
if(settings.toFile){
997-
// svg header
998-
svg = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 ' + width + ' ' + height + '" >';
999-
}
995+
var svg ='<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="' + width +'px" height="' + height +'px" viewBox="0 0 ' + width + ' ' + height + '" >';
996+
1000997
if(settings.bgColor !== 'transparent'){
1001998
// background
1002999
svg += '<rect width="' + width + '" height="' + height + '" x="0" y="0" fill="' + settings.bgColor + '" />';
@@ -1035,12 +1032,11 @@ var barcode = {
10351032
svg += '<text y="' + (height - Math.floor(fontSize / 2)) + '" text-anchor="middle" style="font-family: Arial; font-size: ' + fontSize + 'px;" fill="' + settings.color + '">' + hri + '</text>';
10361033
svg += '</g>';
10371034
}
1035+
svg += '</svg>';
10381036
// svg footer
10391037
if(settings.toFile) {
1040-
svg += '</svg>';
10411038
return this.write(settings, svg, 'svg', callback);
10421039
}else{
1043-
svg += '</g>';
10441040
return svg;
10451041
}
10461042
},
@@ -1056,20 +1052,25 @@ var barcode = {
10561052
return this.digitToSvgRenderer(settings, digit, hri, callback, s, s);
10571053
},
10581054
write: function (settings, data, type, callback) {
1059-
fs.writeFile(settings.path + '.' + type, data, null, function (err) {
1060-
var result = true;
1061-
if (err) {
1062-
console.log(err);
1063-
result = false;
1064-
return result;
1065-
}
1066-
console.log('===svg barcode file create==');
1067-
if (typeof callback === 'function') {
1068-
callback(result);
1069-
}else{
1070-
return true;
1071-
}
1072-
});
1055+
try {
1056+
var fs = require('fs');
1057+
fs.writeFile(settings.path + '.' + type, data, null, function (err) {
1058+
var result = true;
1059+
if (err) {
1060+
console.log(err);
1061+
result = false;
1062+
return result;
1063+
}
1064+
console.log('===svg barcode file create==');
1065+
if (typeof callback === 'function') {
1066+
callback(result);
1067+
}else{
1068+
return true;
1069+
}
1070+
});
1071+
} catch (err) {
1072+
console.error(err);
1073+
}
10731074
}
10741075

10751076
};
@@ -1141,7 +1142,7 @@ module.exports = function (datas, type, settings, callback) {
11411142
b2d = true;
11421143
break;
11431144
}
1144-
if (digit.length == 0) return false;
1145+
if (digit.length === 0) return false;
11451146

11461147
// Quiet Zone
11471148
if (!b2d && settings.addQuietZone) digit = "0000000000" + digit + "0000000000";

0 commit comments

Comments
 (0)