如何通过React Native将打印作业发送到打印机

pftdvrlh  于 2023-02-24  发布在  React
关注(0)|答案(2)|浏览(263)

我实际上正在构建一个应用程序,应该使用热敏打印机或类似的收据打印机打印收据。
我现在使用谷歌云打印,但它需要一个谷歌帐户,我希望人们能够直接连接打印机到他们的手机和使用它。
有没有办法做到这一点?有没有什么库可以帮忙?我在NPM上看了一下,但我能找到的都是与蓝牙设备通信的包,但不是特别关于打印的。
谢谢你的帮忙!

izkcnapc

izkcnapc1#

检查是否有帮助:

import RNXprinter from 'react-native-xprinter';
 RNXprinter.pickPrinter();
 await RNXprinter.printDemoPage();

了解更多详情:https://www.npmjs.com/package/react-native-xprinter

cgfeq70w

cgfeq70w2#

看一下react-native-print
它适用于iOS和Android,按照他们的说明进行设置。
下面是官方示例代码:

import RNHTMLtoPDF from 'react-native-html-to-pdf';
import RNPrint from 'react-native-print';

export default class RNPrintExample extends Component {
  state = {
    selectedPrinter: null
  }

  // @NOTE iOS Only
  selectPrinter = async () => {
    const selectedPrinter = await RNPrint.selectPrinter({ x: 100, y: 100 })
    this.setState({ selectedPrinter })
  }

  // @NOTE iOS Only
  silentPrint = async () => {
    if (!this.state.selectedPrinter) {
      alert('Must Select Printer First')
    }

    const jobName = await RNPrint.print({
      printerURL: this.state.selectedPrinter.url,
      html: '<h1>Silent Print</h1>'
    })

  }

  async printHTML() {
    await RNPrint.print({
      html: '<h1>Heading 1</h1><h2>Heading 2</h2><h3>Heading 3</h3>'
    })
  }

  async printPDF() {
    const results = await RNHTMLtoPDF.convert({
      html: '<h1>Custom converted PDF Document</h1>',
      fileName: 'test',
      base64: true,
    })

    await RNPrint.print({ filePath: results.filePath })
  }

  async printRemotePDF() {
    await RNPrint.print({ filePath: 'https://graduateland.com/api/v2/users/jesper/cv' })
  }

  customOptions = () => {
    return (
      <View>
        {this.state.selectedPrinter &&
          <View>
            <Text>{`Selected Printer Name: ${this.state.selectedPrinter.name}`}</Text>
            <Text>{`Selected Printer URI: ${this.state.selectedPrinter.url}`}</Text>
          </View>
        }
      <Button onPress={this.selectPrinter} title="Select Printer" />
      <Button onPress={this.silentPrint} title="Silent Print" />
    </View>

    )
  }

  render() {
    return (
      <View style={styles.container}>
        {Platform.OS === 'ios' && this.customOptions()}
        <Button onPress={this.printHTML} title="Print HTML" />
        <Button onPress={this.printPDF} title="Print PDF" />
        <Button onPress={this.printRemotePDF} title="Print Remote PDF" />
      </View>
    );
  }
}

相关问题