Have ideas to improve npm?Join in the discussion! »

    @tillpos/rn-receipt-printer-utils
    TypeScript icon, indicating that this package has built-in type declarations

    0.0.17 • Public • Published

    @tillpos/rn-receipt-printer-utils

    Originally forked from react-native-thermal-receipt-printer

    A React Native Library to connect to thermal printer over network and to send the buffer (data to be printed) to thermal printer.

    Node.js Package

    Installation

    yarn add @tillpos/rn-receipt-printer-utils
    

    Troubleshoot

    • when install in react-native version >= 0.60, xcode show this error
    duplicate symbols for architecture x86_64
    

    that because the .a library uses CocoaAsyncSocket library and Flipper uses it too

    Podfile

    ...
      use_native_modules!
    
      # Enables Flipper.
      #
      # Note that if you have use_frameworks! enabled, Flipper will not work and
      # you should disable these next few lines.
      # add_flipper_pods!
      # post_install do |installer|
      #   flipper_post_install(installer)
      # end
    ...

    and comment out code related to Flipper in ios/AppDelegate.m ios/AppDelegate.m

    Support

    Printer Android IOS
    USBPrinter
    BLEPrinter
    NetPrinter ✔️ ✔️

    Tested on following platforms (manual)

    Platform Framework / lib Tested
    Android React Native ✔️
    iOS React Native ✔️

    Development workflow

    To get started with the project, run yarn bootstrap in the root directory to install the required dependencies for each package:

    yarn bootstrap

    While developing, you can run the example app to test your changes.

    To start the packager:

    yarn example start

    To run the example app on Android:

    yarn example android

    To run the example app on iOS:

    yarn example ios

    To make build

    yarn prepare

    Usage

    import {
      USBPrinter,
      NetPrinter,
      BLEPrinter,
    } from "@tillpos/rn-receipt-printer-utils";
    
    USBPrinter.printText("sample text");
    USBPrinter.printBill("sample bill");

    Example

    NetPrinter

    interface INetPrinter {
      device_name: string;
      host: string;
      port: number;
    }

    Note: get list device for net printers is support scanning in local ip but not recommended

      componentDidMount = () => {
        NetPrinter.init().then(() => {
          this.setState(Object.assign({}, this.state, {printers: [{host: '192.168.10.241', port: 9100}]}))
          })
      }
    
      _connectPrinter => (host, port) => {
        //connect printer
        NetPrinter.connectPrinter(host, port).then(
          (printer) => this.setState(Object.assign({}, this.state, {currentPrinter: printer})),
          error => console.warn(error))
    }
    
      printTextTest = () => {
        if (this.state.currentPrinter) {
          NetPrinter.printText("<C>sample text</C>\n");
        }
      }
    
      printBillTest = () => {
        if(this.state.currentPrinter) {
          NetPrinter.printBill("<C>sample bill</C>");
        }
      }
    
      ...
    
      render() {
        return (
          <View style={styles.container}>
            {
              this.state.printers.map(printer => (
                <TouchableOpacity key={printer.device_id} onPress={(printer) => this._connectPrinter(printer.host, printer.port)}>
                  {`device_name: ${printer.device_name}, host: ${printer.host}, port: ${printer.port}`}
                </TouchableOpacity>
                ))
            }
            <TouchableOpacity onPress={() => this.printTextTest()}>
              <Text> Print Text </Text>
            </TouchableOpacity>
            <TouchableOpacity onPress={() => this.printBillTest()}>
              <Text> Print Bill Text </Text>
            </TouchableOpacity>
          </View>
        )
      }
    
      ...

    TODO

    • [ ] Update documentation
    • [ ] Maintain Multiple connections
    • [ ] Use release-it to publish builds
    • [ ] Bluetooth support
    • [ ] USB support

    Install

    npm i @tillpos/rn-receipt-printer-utils

    DownloadsWeekly Downloads

    606

    Version

    0.0.17

    License

    Apache-2.0

    Unpacked Size

    6.21 MB

    Total Files

    48

    Last publish

    Collaborators

    • avatar
    • avatar
    • avatar