You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import React, { NativeModules } from 'react-native';
  2. import EPToolkit from 'escpos-printer-toolkit';
  3. var RNUSBPrinter = NativeModules.RNUSBPrinter;
  4. var RNBLEPrinter = NativeModules.RNBLEPrinter;
  5. var RNNetPrinter = NativeModules.RNNetPrinter;
  6. var textTo64Buffer = (text) => {
  7. let options = {
  8. beep: false,
  9. cut: false,
  10. tailingLine: false,
  11. encoding: 'UTF8'
  12. }
  13. const buffer = EPToolkit.exchange_text(text, options)
  14. return buffer.toString('base64');
  15. }
  16. var billTo64Buffer = (text) => {
  17. let options = {
  18. beep: true,
  19. cut: true,
  20. encoding: 'UTF8',
  21. tailingLine: true
  22. }
  23. const buffer = EPToolkit.exchange_text(text, options)
  24. return buffer.toString("base64");
  25. }
  26. export const USBPrinter = {
  27. init: () =>
  28. new Promise((resolve, reject) =>
  29. RNUSBPrinter.init(() => resolve(), (error) => reject(error))),
  30. getDeviceList: () =>
  31. new Promise((resolve, reject) =>
  32. RNUSBPrinter.getDeviceList((printers) => resolve(printers), (error) => reject(error))),
  33. connectPrinter: (vendorId, productId) =>
  34. new Promise((resolve, reject) =>
  35. RNUSBPrinter.connectPrinter(vendorId, productId, (printer) => resolve(printer), (error) => reject(error))),
  36. closeConn: () => new Promise((resolve, reject) => {
  37. RNUSBPrinter.closeConn();
  38. resolve();
  39. }),
  40. printText: (text) => RNUSBPrinter.printRawData(textTo64Buffer(text), (error) => console.warn(error)),
  41. printImage: (base64Img) => RNUSBPrinter.printRawData(base64Img, (error) => console.warn(error)),
  42. printBill: (text) => RNUSBPrinter.printRawData(billTo64Buffer(text), (error) => console.warn(error)),
  43. }
  44. export const BLEPrinter = {
  45. init: () =>
  46. new Promise((resolve, reject) =>
  47. RNBLEPrinter.init(() => resolve(), (error) => reject(error))),
  48. getDeviceList: () =>
  49. new Promise((resolve, reject) =>
  50. RNBLEPrinter.getDeviceList((printers) => resolve(printers), (error) => reject(error))),
  51. connectPrinter: (inner_mac_address) =>
  52. new Promise((resolve, reject) =>
  53. RNBLEPrinter.connectPrinter(inner_mac_address, (printer) => resolve(printer), (error) => reject(error))),
  54. closeConn: () => new Promise((resolve, reject) => {
  55. RNBLEPrinter.closeConn();
  56. resolve();
  57. }),
  58. printText: (text) => RNBLEPrinter.printRawData(textTo64Buffer(text), (error) => console.warn(error)),
  59. printBill: (text) => RNBLEPrinter.printRawData(billTo64Buffer(text), (error) => console.warn(error)),
  60. }
  61. export const NetPrinter = {
  62. init: () =>
  63. new Promise((resolve, reject) =>
  64. RNNetPrinter.init(() => resolve(), (error) => reject(error))),
  65. connectPrinter: (host, port) =>
  66. new Promise((resolve, reject) =>
  67. RNNetPrinter.connectPrinter(host, port, (printer) => resolve(printer), (error) => reject(error))),
  68. closeConn: () => new Promise((resolve, reject) => {
  69. RNNetPrinter.closeConn();
  70. resolve();
  71. }),
  72. printText: (text) => RNNetPrinter.printRawData(textTo64Buffer(text), (error) => console.warn(error)),
  73. // printText: (text) => console.log(textTo64Buffer(text)),
  74. printBill: (text) => RNNetPrinter.printRawData(billTo64Buffer(text), (error) => console.warn(error)),
  75. }