|
1 | | -recta |
| 1 | +# Recta |
| 2 | + |
| 3 | +> Direct printing from Browser |
| 4 | +
|
| 5 | +[](https://travis-ci.org/adenvt/recta) |
| 6 | +[](https://badge.fury.io/js/recta) |
| 7 | + |
| 8 | +## Introduction |
| 9 | + |
| 10 | +### Reason |
| 11 | +I'm web developer, and get project to make some Point Of Sale (POS) software. I have trouble when i need print some receipt especially with POS printer, so i make this project. |
| 12 | + |
| 13 | +### How it work |
| 14 | +Recta need desktop software called [Recta Host][recta-host], it serve all hardware connection and bridge with Browser via Websocket |
| 15 | + |
| 16 | +## How to use |
| 17 | + |
| 18 | +### Requirement |
| 19 | +* You need to install [Recta Host][recta-host] on every host that will be used for print, checkout [here][recta-host] to more information. |
| 20 | + |
| 21 | +### Standalone |
| 22 | +Just include it like other JS library |
| 23 | +```html |
| 24 | +... |
| 25 | +<script src="https://cdn.jsdelivr.net/npm/recta/dist/recta.js"></script> |
| 26 | +... |
| 27 | +``` |
| 28 | + |
| 29 | +### Using Webpack or Browserify |
| 30 | +Recta support commonjs module, so if you using webpack for your project. you can easly using it like other module |
| 31 | + |
| 32 | +1. Install package |
| 33 | +```bash |
| 34 | +$ npm install --save recta |
| 35 | +``` |
| 36 | +2. Import in your code |
| 37 | +```js |
| 38 | +// using CommonJS |
| 39 | +var Recta = require('recta') |
| 40 | +// using ES6 style |
| 41 | +import Recta from 'recta' |
| 42 | +``` |
| 43 | + |
| 44 | +## Example |
| 45 | + |
| 46 | +```html |
| 47 | +<script type="text/javascript"> |
| 48 | + // ... |
| 49 | + var printer = new Recta('APPKEY', '1811') |
| 50 | +
|
| 51 | + function onClick () { |
| 52 | + printer.open().then(function () { |
| 53 | + printer.align('center') |
| 54 | + .text('Hello World !!') |
| 55 | + .bold(true) |
| 56 | + .text('This is bold text') |
| 57 | + .bold(false) |
| 58 | + .underline(true) |
| 59 | + .text('This is underline text') |
| 60 | + .underline(false) |
| 61 | + .barcode('CODE39', '123456789') |
| 62 | + .cut() |
| 63 | + .print() |
| 64 | + }) |
| 65 | + } |
| 66 | + // ... |
| 67 | +</script> |
| 68 | +``` |
| 69 | + |
| 70 | +## API |
| 71 | + |
| 72 | +### Class |
| 73 | + |
| 74 | +#### *new* Recta(key, port) |
| 75 | +Create a new Recta object, parameter: |
| 76 | + |
| 77 | +* Key: APPKEY used to authentication with Host, You can see on your [Recta Host][recta-host] Configuration |
| 78 | +* Port: port of host, *default*: 1811 |
| 79 | + |
| 80 | +### Methods |
| 81 | + |
| 82 | +### .open() |
| 83 | +open connection to printer host. |
| 84 | + |
| 85 | +*return* **Promise** |
| 86 | + |
| 87 | +### .text('string') |
| 88 | +print text. |
| 89 | + |
| 90 | +note: it automaticaly add linefeed *(`\n`)* use `.raw()` instead if you wouldn’t add linefeed. |
| 91 | + |
| 92 | +### .align('align') |
| 93 | +set horizontal text align, parameter value can be: |
| 94 | + |
| 95 | +* LEFT |
| 96 | +* CENTER |
| 97 | +* RIGHT |
| 98 | + |
| 99 | +### .bold(true|false) |
| 100 | +turn emphasized mode on / off, **default**: true |
| 101 | + |
| 102 | +### .underline(true|false|2) |
| 103 | +turn underline mode on / off, set `2` for set 2-dot width underline, **default**: true |
| 104 | + |
| 105 | +### .font('A'|'B') |
| 106 | +select character font `'A'` or `'B'` |
| 107 | + |
| 108 | +### .mode(font, emphasized, doubleHeight, doubleWidth, underline) |
| 109 | +select print modes, parameter: |
| 110 | + |
| 111 | +* **font**: select character font, value: `'A'` or `'B'`, *default*: `'A'` |
| 112 | +* **emphasized**: turn on / off emphasized / bold mode, value: `true` or `false`, *default*: `false` |
| 113 | +* **doubleHeight**: turn on / off double height mode, value: `true` or `false`, *default*: `false` |
| 114 | +* **doubleWidth**: turn on / off double width mode, value: `true` or `false`, *default*: `false` |
| 115 | +* **underline**: turn on / off underline mode, value: `true` or `false`, *default*: `false` |
| 116 | + |
| 117 | +### .cut(partial, linefeed) |
| 118 | +cut paper, parameter: |
| 119 | + |
| 120 | +* **partial**: if `true` execute partial cut (one point left uncut) else cut paper completely (full cut), value: `true` or `false`, *default*: `false` |
| 121 | +* **linefeed**: add linefeed, value: **integer**, *default*: 4 |
| 122 | + |
| 123 | +### .barcode(type, barcode, barcodeHeight) |
| 124 | +print barcode, parameter |
| 125 | + |
| 126 | +* **type**: type of Barcode, value: |
| 127 | + * UPC-A |
| 128 | + * UPC-E |
| 129 | + * EAN13 |
| 130 | + * EAN8 |
| 131 | + * CODE39 |
| 132 | + * ITF |
| 133 | + * CODABAR |
| 134 | + * CODE93 |
| 135 | + * CODE128 |
| 136 | +* **barcode**: content barcode, value: **string** |
| 137 | +* **barcodeHeight**: set barcode height *(optional)*, value: **integer** |
| 138 | + |
| 139 | +### .barcodeHeight(height) |
| 140 | +set barcode height, value can be range: 1 ≤ n ≤ 255 |
| 141 | + |
| 142 | +### .feed(n) |
| 143 | +add linefeed as much as n, *default*: 4 |
| 144 | + |
| 145 | +### .raw(raw) |
| 146 | +print raw text / buffer |
| 147 | + |
| 148 | +### .print() |
| 149 | +send print command to printer. ***printer wouldn’t execute until you call this*** |
| 150 | + |
| 151 | +### .reset() |
| 152 | +send reset instruction to printer |
| 153 | + |
| 154 | +### .flush() |
| 155 | +return a sliced Buffer instance and clear buffer |
| 156 | + |
| 157 | +*return* **Buffer** |
| 158 | + |
| 159 | +### .clearBuffer() |
| 160 | +clear buffer |
| 161 | + |
| 162 | +### .close() |
| 163 | +close connection to host |
| 164 | + |
| 165 | +*return* **Promise** |
| 166 | + |
| 167 | +## Events |
| 168 | + |
| 169 | +### .on('open', function() {}) |
| 170 | +Fired upon successful connection |
| 171 | + |
| 172 | +### .on('close', function() {}) |
| 173 | +Fired upon disconnected |
| 174 | + |
| 175 | +### .on('error', function(error) {}) |
| 176 | +Fired when an error occurs |
| 177 | + |
| 178 | +## Contributing |
| 179 | + |
| 180 | +* Fork this repo |
| 181 | +* Clone your repo |
| 182 | +* Install dependencies |
| 183 | +* Checkout a feature branch |
| 184 | +* Feel free to add your features |
| 185 | +* Make sure your features are fully tested |
| 186 | +* Open a pull request, and enjoy <3 |
| 187 | + |
| 188 | +## License |
| 189 | + |
| 190 | +This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details |
| 191 | + |
| 192 | +[recta-host]: http://github.com/adenvt/recta-host |
0 commit comments