RollCast/User Guide/React Native

React Native

Print from a React Native app with Linking and react-native-share.

Open a rollcast://print link with Linking. Use the buffer package for base64 and react-native-share for files.

Shell
npm install buffer react-native-share

Add the <queries> block from the Flutter page to android/app/src/main/AndroidManifest.xml so Linking.canOpenURL can see RollCast on Android 11+.

Print text

JavaScript
import { Linking } from 'react-native';
import { Buffer } from 'buffer';

const STORE_URL = 'market://details?id=com.rollcast.app';

async function send(type, base64, printerId) {
  let url = `rollcast://print?type=${type}&data=${encodeURIComponent(base64)}`;
  if (printerId) url += `&printer=${encodeURIComponent(printerId)}`;
  if (await Linking.canOpenURL(url)) {
    await Linking.openURL(url);
  } else {
    await Linking.openURL(STORE_URL);
  }
}

export function printText(text, printerId) {
  return send('text', Buffer.from(text, 'utf8').toString('base64'), printerId);
}

export function printRaw(bytes, printerId) {
  return send('raw', Buffer.from(bytes).toString('base64'), printerId);
}

Raw ESC/POS bytes

JavaScript
export function printReceipt() {
  const ascii = (s) => Array.from(Buffer.from(s, 'latin1'));
  const bytes = [
    0x1B, 0x40,          // ESC @    reset
    0x1B, 0x45, 0x01,    // ESC E 1  bold on
    ...ascii('MY SHOP\n'),
    0x1B, 0x45, 0x00,    // ESC E 0  bold off
    ...ascii('TOTAL           40.00\n'),
    0x1B, 0x64, 0x03,    // ESC d 3  feed 3 lines
    0x1D, 0x56, 0x01,    // GS V 1   cut
  ];
  return printRaw(bytes);
}

Share a PDF or image

JavaScript
import Share from 'react-native-share';

export async function sharePdf(path) {
  // path: an absolute file path, e.g. from react-native-fs or a PDF generator.
  await Share.open({
    url: 'file://' + path,
    type: 'application/pdf',
    failOnCancel: false,
  });
}

The user picks RollCast in the share sheet. The file prints on the default printer.