|
| 1 | +# react-native-printer |
| 2 | +Fork of react-native-printer and add implement for auto connect printer with usb |
| 3 | +A React Native Library to support USB/BLE/Net printer for Android platform |
1 | 4 |
|
2 |
| -# react-native-thermal-receipt-printer |
| 5 | +## Installation |
3 | 6 |
|
4 |
| -## Getting started |
| 7 | +``` |
| 8 | +npm install react-native-printer --save |
5 | 9 |
|
6 |
| -`$ npm install react-native-thermal-receipt-printer --save` |
| 10 | +``` |
7 | 11 |
|
8 |
| -### Mostly automatic installation |
| 12 | +## Integrate module |
9 | 13 |
|
10 |
| -`$ react-native link react-native-thermal-receipt-printer` |
| 14 | +To integrate `react-native-printer` with the rest of your react app just execute: |
11 | 15 |
|
12 |
| -### Manual installation |
| 16 | +``` |
| 17 | +react-native link react-native-printer |
13 | 18 |
|
| 19 | +``` |
14 | 20 |
|
15 |
| -#### iOS |
| 21 | +## Usage |
16 | 22 |
|
17 |
| -1. In XCode, in the project navigator, right click `Libraries` ➜ `Add Files to [your project's name]` |
18 |
| -2. Go to `node_modules` ➜ `react-native-thermal-receipt-printer` and add `RNThermalReceiptPrinter.xcodeproj` |
19 |
| -3. In XCode, in the project navigator, select your project. Add `libRNThermalReceiptPrinter.a` to your project's `Build Phases` ➜ `Link Binary With Libraries` |
20 |
| -4. Run your project (`Cmd+R`)< |
| 23 | +```javascript |
| 24 | +import { USBPrinter, NetPrinter, BLEPrinter } from 'react-native-printer'; |
21 | 25 |
|
22 |
| -#### Android |
| 26 | +USBPrinter.printText('<C>这是一个测试打印</C>') |
| 27 | +USBPrinter.printBill("<C>这是一段打印测试文字</C>") |
23 | 28 |
|
24 |
| -1. Open up `android/app/src/main/java/[...]/MainActivity.java` |
25 |
| - - Add `import com.reactlibrary.RNThermalReceiptPrinterPackage;` to the imports at the top of the file |
26 |
| - - Add `new RNThermalReceiptPrinterPackage()` to the list returned by the `getPackages()` method |
27 |
| -2. Append the following lines to `android/settings.gradle`: |
28 |
| - ``` |
29 |
| - include ':react-native-thermal-receipt-printer' |
30 |
| - project(':react-native-thermal-receipt-printer').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-thermal-receipt-printer/android') |
31 |
| - ``` |
32 |
| -3. Insert the following lines inside the dependencies block in `android/app/build.gradle`: |
33 |
| - ``` |
34 |
| - compile project(':react-native-thermal-receipt-printer') |
35 |
| - ``` |
| 29 | +``` |
36 | 30 |
|
37 |
| -#### Windows |
38 |
| -[Read it! :D](https://github.com/ReactWindows/react-native) |
| 31 | +## Example |
39 | 32 |
|
40 |
| -1. In Visual Studio add the `RNThermalReceiptPrinter.sln` in `node_modules/react-native-thermal-receipt-printer/windows/RNThermalReceiptPrinter.sln` folder to their solution, reference from their app. |
41 |
| -2. Open up your `MainPage.cs` app |
42 |
| - - Add `using Thermal.Receipt.Printer.RNThermalReceiptPrinter;` to the usings at the top of the file |
43 |
| - - Add `new RNThermalReceiptPrinterPackage()` to the `List<IReactPackage>` returned by the `Packages` method |
| 33 | +### USBPrinter |
| 34 | + |
| 35 | +Printer structure: |
| 36 | +``` |
| 37 | +{ |
| 38 | + device_name: '/usb/lp1', |
| 39 | + vendor_id: 1155, |
| 40 | + product_id: 22304, |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | + |
| 45 | +```javascript |
| 46 | + |
| 47 | + componentDidMount = () => { |
| 48 | + if(Platform.OS == 'android'){ |
| 49 | + USBPrinter.init().then(()=> { |
| 50 | + //list printers |
| 51 | + USBPrinter.getDeviceList() |
| 52 | + .then(printers => { |
| 53 | + this.setState(Object.assign({}, this.state, {printers: printers})) |
| 54 | + }); |
| 55 | + |
| 56 | + //connect printer |
| 57 | + vendorID = 1155 |
| 58 | + productId = 22304 |
| 59 | + USBPrinter.connectPrinter(vendorID, productId).then( |
| 60 | + (printer) => this.setState(Object.assign({}, this.state, {currentPrinter: printer})), |
| 61 | + error => console.warn(error)) |
| 62 | + }) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + printTextTest = () => { |
| 67 | + if(this.state.currentPrinter) { |
| 68 | + USBPrinter.printText("<C>这是一段打印测试文字</C>\n"); |
| 69 | + }else{ |
| 70 | + console.log("没有设置打印机") |
| 71 | + } |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + printBillTest = () => { |
| 76 | + if(this.state.currentPrinter) { |
| 77 | + USBPrinter.printBill("<C>这是一段打印测试文字</C>"); |
| 78 | + }else{ |
| 79 | + console.log("没有设置打印机") |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + ... |
| 84 | + |
| 85 | + render() { |
| 86 | + return ( |
| 87 | + <View style={styles.container}> |
| 88 | + { |
| 89 | + this.state.printers.map(printer => ( |
| 90 | + <TouchableOpacity key={printer.device_id} onPress={(printer) => this._connectPrinter(printer.vendor_id, printer.product_id)}> |
| 91 | + {`device_name: ${printer.device_name}, device_id: ${printer.device_id}, vendor_id: ${printer.vendor_id}, product_id: ${printer.product_id}`} |
| 92 | + </TouchableOpacity> |
| 93 | + )) |
| 94 | + } |
| 95 | + <TouchableOpacity onPress={() => this.printTextTest()}> |
| 96 | + <Text> Print Text </Text> |
| 97 | + </TouchableOpacity> |
| 98 | + <TouchableOpacity onPress={() => this.printBillTest()}> |
| 99 | + <Text> Print Bill Text </Text> |
| 100 | + </TouchableOpacity> |
| 101 | + </View> |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + ... |
| 106 | + |
| 107 | +``` |
| 108 | + |
| 109 | +### BLEPrinter |
| 110 | + |
| 111 | +Printer structure: |
| 112 | +``` |
| 113 | +{ |
| 114 | + device_name: '内部打印机', |
| 115 | + inner_mac_address: 'XXXXX-XXXXXXXX', |
| 116 | +} |
| 117 | +``` |
44 | 118 |
|
45 | 119 |
|
46 |
| -## Usage |
47 | 120 | ```javascript
|
48 |
| -import RNThermalReceiptPrinter from 'react-native-thermal-receipt-printer'; |
49 | 121 |
|
50 |
| -// TODO: What to do with the module? |
51 |
| -RNThermalReceiptPrinter; |
| 122 | + componentDidMount = () => { |
| 123 | + if(Platform.OS == 'android'){ |
| 124 | + USBPrinter.init().then(()=> { |
| 125 | + //list printers |
| 126 | + USBPrinter.getDeviceList() |
| 127 | + .then(printers => { |
| 128 | + this.setState(Object.assign({}, this.state, {printers: printers})) |
| 129 | + }); |
| 130 | + |
| 131 | + |
| 132 | + }) |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + _connectPrinter => (inner_mac_address) => { |
| 137 | + if(Platform.OS == 'android'){ |
| 138 | + //connect printer |
| 139 | + USBPrinter.connectPrinter(inner_mac_address).then( |
| 140 | + (printer) => this.setState(Object.assign({}, this.state, {currentPrinter: printer})), |
| 141 | + error => console.warn(error)) |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + printTextTest = () => { |
| 146 | + if(this.state.currentPrinter) { |
| 147 | + USBPrinter.printText("<C>这是一段打印测试文字</C>\n"); |
| 148 | + }else{ |
| 149 | + console.log("没有设置打印机") |
| 150 | + } |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + printBillTest = () => { |
| 155 | + if(this.state.currentPrinter) { |
| 156 | + USBPrinter.printBill("<C>这是一段打印测试文字</C>"); |
| 157 | + }else{ |
| 158 | + console.log("没有设置打印机") |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + ... |
| 163 | + |
| 164 | + render() { |
| 165 | + return ( |
| 166 | + <View style={styles.container}> |
| 167 | + { |
| 168 | + this.state.printers.map(printer => ( |
| 169 | + <TouchableOpacity key={printer.inner_mac_address} onPress={(printer) => this._connectPrinter(printer.inner_mac_address)}> |
| 170 | + {`device_name: ${printer.device_name}, inner_mac_address: ${printer.inner_mac_address}`} |
| 171 | + </TouchableOpacity> |
| 172 | + )) |
| 173 | + } |
| 174 | + <TouchableOpacity onPress={() => this.printTextTest()}> |
| 175 | + <Text> Print Text </Text> |
| 176 | + </TouchableOpacity> |
| 177 | + <TouchableOpacity onPress={() => this.printBillTest()}> |
| 178 | + <Text> Print Bill Text </Text> |
| 179 | + </TouchableOpacity> |
| 180 | + </View> |
| 181 | + ) |
| 182 | + } |
| 183 | + |
| 184 | + ... |
| 185 | + |
| 186 | +``` |
| 187 | + |
| 188 | +### NetPrinter |
| 189 | + |
| 190 | +Printer structure: |
52 | 191 | ```
|
53 |
| - |
| 192 | +{ |
| 193 | + device_name: "192.168.10.241:9100", |
| 194 | + host: '192.168.10.241', |
| 195 | + port: 9100 |
| 196 | +} |
| 197 | +``` |
| 198 | + |
| 199 | + |
| 200 | +```javascript |
| 201 | + |
| 202 | + componentDidMount = () => { |
| 203 | + if(Platform.OS == 'android'){ |
| 204 | + NetPrinter.init().then(() => { |
| 205 | + this.setState(Object.assign({}, this.state, {printers: [{host: '192.168.10.241', port: 9100}]})) |
| 206 | + }) |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + _connectPrinter => (host, port) => { |
| 211 | + if(Platform.OS == 'android'){ |
| 212 | + //connect printer |
| 213 | + NetPrinter.connectPrinter(host, port).then( |
| 214 | + (printer) => this.setState(Object.assign({}, this.state, {currentPrinter: printer})), |
| 215 | + error => console.warn(error)) |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + printTextTest = () => { |
| 220 | + if(this.state.currentPrinter) { |
| 221 | + NetPrinter.printText("<C>这是一段打印测试文字</C>\n"); |
| 222 | + }else{ |
| 223 | + console.log("没有设置打印机") |
| 224 | + } |
| 225 | + |
| 226 | + } |
| 227 | + |
| 228 | + printBillTest = () => { |
| 229 | + if(this.state.currentPrinter) { |
| 230 | + NetPrinter.printBill("<C>这是一段打印测试文字</C>"); |
| 231 | + }else{ |
| 232 | + console.log("没有设置打印机") |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + ... |
| 237 | + |
| 238 | + render() { |
| 239 | + return ( |
| 240 | + <View style={styles.container}> |
| 241 | + { |
| 242 | + this.state.printers.map(printer => ( |
| 243 | + <TouchableOpacity key={printer.device_id} onPress={(printer) => this._connectPrinter(printer.host, printer.port)}> |
| 244 | + {`device_name: ${printer.device_name}, host: ${printer.host}, port: ${printer.port}`} |
| 245 | + </TouchableOpacity> |
| 246 | + )) |
| 247 | + } |
| 248 | + <TouchableOpacity onPress={() => this.printTextTest()}> |
| 249 | + <Text> Print Text </Text> |
| 250 | + </TouchableOpacity> |
| 251 | + <TouchableOpacity onPress={() => this.printBillTest()}> |
| 252 | + <Text> Print Bill Text </Text> |
| 253 | + </TouchableOpacity> |
| 254 | + </View> |
| 255 | + ) |
| 256 | + } |
| 257 | + |
| 258 | + ... |
| 259 | + |
| 260 | +``` |
0 commit comments