Quentin
Quentin

Reputation: 386

How to send a print job to a printer through React Native

I am actually building an app that is supposed to print a receipt using a thermal printer or similar receipt printer.

I'm using Google Cloud Print for now but it requires a Google account and I want people to be able to connect the printer directly to their phone and use it.

Is there any way to do that? Any library that could help? I had a look on npm but all I can find are packages to communicate with bluetooth devices, but not about printing in particular.

Thanks for your help!

Upvotes: 9

Views: 9851

Answers (2)

Ian
Ian

Reputation: 2610

Take a look at react-native-print.

It works for iOS and Android. Follow their instructions for setup.

Here is the official example code:

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>
    );
  }
}

Upvotes: 0

Istiak Morsalin
Istiak Morsalin

Reputation: 11159

check if it helps :

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

FOR MORE DETAILS: https://www.npmjs.com/package/react-native-xprinter

Upvotes: 2

Related Questions