januslo / react-native-bluetooth-escpos-printer

React-Native plugin for the bluetooth ESC/POS & TSC printers.
MIT License
368 stars 428 forks source link

Can't print Arabic ! #77

Open hm-harshit opened 5 years ago

hm-harshit commented 5 years ago

I tried to print Arabic but it doesn't work

I tried the function

await BluetoothEscposPrinter.printText("لا يوجد مجاني");

but it doesn't work

Is there is an option to print pdf files?

mainginski commented 5 years ago

Look comments in #8

MurtazaEliyas commented 5 years ago

Any updates? Comments from that thread didnt work for me. I am using a TM-P80 Epson printer. the manual has the following code pages Page 32 (PC720: Arabic) Page 37 (PC864: Arabic) Page 50 (WPC1256: Arabic)

used all it prints gibberish. Any help would be appreciated.

jiloysss commented 4 years ago

Hello @MurtazaEliyas any updates? have you made it work? printing arabic? I also tried the thread @mainginski sent but it also didn't work

pssolutions commented 4 years ago

@jiloysss I tried a lot with different combinations of codepages and encodings. I also had to test on multiple brands of printers: Xprinter, Epson, TSC and Bixolon.. all gave same results. I used encoding:'Arabic', codepage: 22 in all my tests. It printed Arabic characters but in reverse so I reversed the strings to be printed before sending in printText method like shown below let string = ' اهلا و مرحبا مسرة\n\r'; string = string.split('').reverse().join('');

The result was not very good

WhatsApp Image 2019-12-09 at 3 08 08 PM

WhatsApp Image 2019-12-09 at 3 08 08 PM2

Finally we decided to make a bridge to native layer. And have the printers sdk (.jar file) help us in printing Arabic text. We read a little on how this library prints columns as that was something we wanted. Turns out its just looping and snipping strings to required column sizes then printing them line by line.

We have put the printer on pause for now. Will share more once its all implemented.

Hope this helps

jiloysss commented 4 years ago

Okay @pssolutions thank you very much.

byteab commented 4 years ago

using react-native-view-shot and react-native-fs help me to solve the issue. the approach is to take screenshot of the view and then read it as base64 and then send it to printer. the good thing is that you can use your custom font and custom design to print every thing that you want. the issue that I face was that image not scale the whole screen and I solve it in this comment image scale fixed

islamabujamea commented 4 years ago

hey ! Can I ask u question about this ?

byteab commented 4 years ago

@islamabujamea yeah why not

FazilMuhammed commented 4 years ago

@EhsanSarshar Hello brother... I have issue regrading Arabic text print.can you share your code to me please.how to get screenshot and read it as base64 and how you send to printer.im waiting your replay.

byteab commented 4 years ago

@FazilMuhammed here are key parts

import {
  BluetoothEscposPrinter,
  BluetoothManager,
  BluetoothTscPrinter,
  // @ts-ignore
} from 'react-native-bluetooth-escpos-printer'
import fs from 'react-native-fs'

import ViewShot from 'react-native-view-shot'

  const captureRef = useRef()

  const printIt = async () => {
    if (captureRef.current) {
      const imageUri = await captureRef.current.capture({
        result: 'tmpfile',
        width: 1000,
      })
      const base64 = await fs.readFile(imageUri, 'base64')
      await BluetoothEscposPrinter.printerInit()
      await BluetoothEscposPrinter.printerAlign(
        BluetoothEscposPrinter.ALIGN.CENTER,
      )
      await BluetoothEscposPrinter.printPic(base64, {width: 630})
    }
  }

    return (
      <View style={styles.all}>
          <ViewShot ref={captureRef} style={styles.body}>
            <View style={styles.header}>
              <Image
                resizeMode="contain"
                source={require('../someImage.png')}
                style={styles.logo}
              />
              <View>
                <Text style={styles.title}>یک متن جعلی</Text>
                <Text style={styles.address}>
                  هرات افغانستان
                </Text>
              </View>
              <Text style={styles.footerText}>تشکر از خریداری تان</Text>
            </View>
          </ViewShot>
        <TouchableOpacity onPress={printIt} style={styles.button}>
          <Text style={styles.printText}>print</Text>
        </TouchableOpacity>
      </View>
    )
FazilMuhammed commented 4 years ago

@EhsanSarshar Thanks for your reply. one more also I have the doubt. how will you connect the printer to your Mobile can share the code to me please. I working IOS I cannot print the datas couple months get stuck on this issue please help me.

ferasabufares commented 3 years ago

@FazilMuhammed here are key parts

import {
  BluetoothEscposPrinter,
  BluetoothManager,
  BluetoothTscPrinter,
  // @ts-ignore
} from 'react-native-bluetooth-escpos-printer'
import fs from 'react-native-fs'

import ViewShot from 'react-native-view-shot'

  const captureRef = useRef()

  const printIt = async () => {
    if (captureRef.current) {
      const imageUri = await captureRef.current.capture({
        result: 'tmpfile',
        width: 1000,
      })
      const base64 = await fs.readFile(imageUri, 'base64')
      await BluetoothEscposPrinter.printerInit()
      await BluetoothEscposPrinter.printerAlign(
        BluetoothEscposPrinter.ALIGN.CENTER,
      )
      await BluetoothEscposPrinter.printPic(base64, {width: 630})
    }
  }

    return (
      <View style={styles.all}>
          <ViewShot ref={captureRef} style={styles.body}>
            <View style={styles.header}>
              <Image
                resizeMode="contain"
                source={require('../someImage.png')}
                style={styles.logo}
              />
              <View>
                <Text style={styles.title}>یک متن جعلی</Text>
                <Text style={styles.address}>
                  هرات افغانستان
                </Text>
              </View>
              <Text style={styles.footerText}>تشکر از خریداری تان</Text>
            </View>
          </ViewShot>
        <TouchableOpacity onPress={printIt} style={styles.button}>
          <Text style={styles.printText}>print</Text>
        </TouchableOpacity>
      </View>
    )

can you help me it is not working i got blank paper

ferasabufares commented 3 years ago

@pssolutions did you manga to make work print in arabic

feras-merwas commented 3 years ago

@jiloysss I tried a lot with different combinations of codepages and encodings. I also had to test on multiple brands of printers: Xprinter, Epson, TSC and Bixolon.. all gave same results. I used encoding:'Arabic', codepage: 22 in all my tests. It printed Arabic characters but in reverse so I reversed the strings to be printed before sending in printText method like shown below let string = ' اهلا و مرحبا مسرة\n\r'; string = string.split('').reverse().join('');

The result was not very good

WhatsApp Image 2019-12-09 at 3 08 08 PM

WhatsApp Image 2019-12-09 at 3 08 08 PM2

Finally we decided to make a bridge to native layer. And have the printers sdk (.jar file) help us in printing Arabic text. We read a little on how this library prints columns as that was something we wanted. Turns out its just looping and snipping strings to required column sizes then printing them line by line.

We have put the printer on pause for now. Will share more once its all implemented.

Hope this helps

did you manga to make work print in arabic

pssolutions commented 3 years ago

@feras-merwas No we did not continue with the solution. The printer task never resumed. However I think things might have changed now with newer libraries. Will post here if I find anything

feras-merwas commented 3 years ago

@pssolutions thank you

byteab commented 3 years ago

@FazilMuhammed here are key parts

import {
  BluetoothEscposPrinter,
  BluetoothManager,
  BluetoothTscPrinter,
  // @ts-ignore
} from 'react-native-bluetooth-escpos-printer'
import fs from 'react-native-fs'

import ViewShot from 'react-native-view-shot'

  const captureRef = useRef()

  const printIt = async () => {
    if (captureRef.current) {
      const imageUri = await captureRef.current.capture({
        result: 'tmpfile',
        width: 1000,
      })
      const base64 = await fs.readFile(imageUri, 'base64')
      await BluetoothEscposPrinter.printerInit()
      await BluetoothEscposPrinter.printerAlign(
        BluetoothEscposPrinter.ALIGN.CENTER,
      )
      await BluetoothEscposPrinter.printPic(base64, {width: 630})
    }
  }

    return (
      <View style={styles.all}>
          <ViewShot ref={captureRef} style={styles.body}>
            <View style={styles.header}>
              <Image
                resizeMode="contain"
                source={require('../someImage.png')}
                style={styles.logo}
              />
              <View>
                <Text style={styles.title}>یک متن جعلی</Text>
                <Text style={styles.address}>
                  هرات افغانستان
                </Text>
              </View>
              <Text style={styles.footerText}>تشکر از خریداری تان</Text>
            </View>
          </ViewShot>
        <TouchableOpacity onPress={printIt} style={styles.button}>
          <Text style={styles.printText}>print</Text>
        </TouchableOpacity>
      </View>
    )

can you help me it is not working i got blank paper

Yeah sure! send me your source code.

feras-merwas commented 3 years ago

@TheEhsanSarshar hello this is my code when print i got weird character

import { BluetoothManager, BluetoothEscposPrinter, } from 'react-native-bluetooth-escpos-printer';

version '0.0.5' 20211114_104621

  printIt = async () => {
    const imageUri = await this.refs.viewShot2.capture({
      result: 'tmpfile',
      width: 1000,
    });

    const base64 = await FileSystem.readFile(imageUri, {
      encoding: 'base64',
    });

    await BluetoothEscposPrinter.printerInit();
    await BluetoothEscposPrinter.printerAlign(
      BluetoothEscposPrinter.ALIGN.CENTER,
    );
    console.log(`base64`, base64);

    try {
      await BluetoothEscposPrinter.printPic(base64, {
        width: 100,
        left: 0,
      });
    } catch (e) {
      console.log(e || 'ERROR');
      console.log(e);
    }
  };
      return (
        <View style={styles.Maincontainer}>
          <ViewShot ref="viewShot2" style={styles.body}>
            <Image
              resizeMode="contain"
              source={require('../../../../assists/img/cloud.jpg')}
              style={{width: 100, height: 100}}
            />
          </ViewShot>
          <View
            style={{
              width: '100%',
              padding: 10,
              justifyContent: 'center',
              alignItems: 'center',
            }}>
            <TouchableOpacity onPress={this.printIt} style={styles.button}>
              <Text style={styles.printText}>print</Text>
            </TouchableOpacity>
          </View>
        </View>
      );
tanishabedi commented 2 years ago

@FazilMuhammed here are key parts

import {
  BluetoothEscposPrinter,
  BluetoothManager,
  BluetoothTscPrinter,
  // @ts-ignore
} from 'react-native-bluetooth-escpos-printer'
import fs from 'react-native-fs'

import ViewShot from 'react-native-view-shot'

  const captureRef = useRef()

  const printIt = async () => {
    if (captureRef.current) {
      const imageUri = await captureRef.current.capture({
        result: 'tmpfile',
        width: 1000,
      })
      const base64 = await fs.readFile(imageUri, 'base64')
      await BluetoothEscposPrinter.printerInit()
      await BluetoothEscposPrinter.printerAlign(
        BluetoothEscposPrinter.ALIGN.CENTER,
      )
      await BluetoothEscposPrinter.printPic(base64, {width: 630})
    }
  }

    return (
      <View style={styles.all}>
          <ViewShot ref={captureRef} style={styles.body}>
            <View style={styles.header}>
              <Image
                resizeMode="contain"
                source={require('../someImage.png')}
                style={styles.logo}
              />
              <View>
                <Text style={styles.title}>یک متن جعلی</Text>
                <Text style={styles.address}>
                  هرات افغانستان
                </Text>
              </View>
              <Text style={styles.footerText}>تشکر از خریداری تان</Text>
            </View>
          </ViewShot>
        <TouchableOpacity onPress={printIt} style={styles.button}>
          <Text style={styles.printText}>print</Text>
        </TouchableOpacity>
      </View>
    )

I am getting black print in place of text or image. Please Help.

byteab commented 2 years ago

@tanishabedi @feras-merwas share yourlogcat logs while printing. it's sometimes because of device Ram issues