简体   繁体   中英

Create receipt with escpos package and Serial port

I'm trying build an electron app and create a simple receipt to print via Serial Port.

I'm using:

  • electron: ^11.1.1
  • escpos: ^3.0.0-alpha.6
  • escpos-serialport: ^3.0.0-alpha.4

This is my code to create receipt:

const escpos = require('escpos');
escpos.SerialPort = require('escpos-serialport');

module.exports = {

  printSerial: function(port, data){

    let device = new escpos.SerialPort('COM2', { baudRate: 19200 });
    let printer = new escpos.Printer(device);

    device.open(function(err){
      printer
      .size(1, 1)
      .font('A')
      .encode('CP949')
      .align('CT')
      .style('NORMAL')
      .println('Receipt')
      .newLine()
      .align('LT')
      .style('NORMAL')
      .tableCustom([
        { text: 'Date:', width: 0.2 },
        { text: '2021-01-04 11:11', width: 0.6 }
      ])
      .style('NORMAL')
      .tableCustom([
        { text: 'Order ID:', width: 0.2 },
        { text: '050-7866-2406', width: 0.6 }
      ])
      .style('NORMAL')
      .tableCustom([
        { text: '전화번호:', width: 0.2 },
        { text: '200000000', width: 0.6, align: 'LEFT' }
      ], { encoding: 'EUC-KR' })
      .tableCustom([
        { text: '메모:', width: 0.4 },
        { text: '문앞에 두고 벨을 눌러주세요', width: 0.6 }
      ], 'CP949')
      .drawLine()
      .newLine()
      .println('기사님 이름:', 'CP949')
      .println('기사님 번호:', 'CP949')
      .drawLine()
      .newLine()
      .tableCustom([
        { text: '내부규', width: 0.33, align: 'LEFT' },
        { text: '내부', width: 0.33, align: 'CENTER' },
        { text: '율과', width: 0.33, align: 'CENTER' }
      ], 'CP949')
      .tableCustom([
        { text: '내부규율과', width: 0.33, align: 'LEFT' },
        { text: '1', width: 0.33, align: 'CENTER' },
        { text: '2,000,000 원', width: 0.33, align: 'CENTER' }
      ], 'CP949')
      .drawLine()
      .newLine()
      .tableCustom([
        { text: '합계', width: 0.6, align: 'LEFT' },
        { text: '2,000,000 원', width: 0.4, align: 'RIGHT' }
      ], 'CP949')
      .tableCustom([
        { text: '배송료(11.2km)', width: 0.6, align: 'LEFT' },
        { text: '2,000 원', width: 0.4, align: 'RIGHT' }
      ], 'CP949')
      .tableCustom([
        { text: '수수료', width: 0.6, align: 'LEFT' },
        { text: '-0 원', width: 0.4, align: 'RIGHT' }
      ], 'CP949')
      .drawLine()
      .newLine()
      .tableCustom([
        { text: '총금액', width: 0.6, align: 'LEFT' },
        { text: '2,000,000 원', width: 0.4, align: 'RIGHT' }
      ], 'CP949')
      .newLine()
      .newLine()
      .newLine()
      .cut();

      setTimeout(function(){
        printer.close();
      }, 1000);
    });
  }

};

When I call printSerial method. Receipt print success. But text of Korean is incorrect. I was trying with many encoding names but it still not work.

And how can I change font size in receipt, current text on receipt very big.

Maybe I missing something?

Thank advance so much for help.

I also had the same issue and I found it and solved it.
I share korean encoding code.

korean encoding code: cp949

and println to text
this package is too big font size default, so you need font size control to this

.size(0.01, 0.01) // default .size(1,1)

Add This Code:

const options = { encoding: 'cp949' }

Full Code:

const escpos = require('escpos');
escpos.SerialPort = require('escpos-serialport');

module.exports = {

  printSerial: function(port, data){

    let device = new escpos.SerialPort('COM2', { baudRate: 19200 });
    const options = { encoding: 'cp949' } // <- Add This option.
    let printer = new escpos.Printer(device, options);

    device.open(function(err){
      printer
      .size(0.01, 0.01) // <-- Font size small
      .font('A')
      .encode('CP949')
      .align('CT')
      .style('NORMAL')
      .text('Receipt') // <- `println` to `text`
      .newLine()
      .align('LT')
      .style('NORMAL')
      .tableCustom([
        { text: 'Date:', width: 0.2 },
        { text: '2021-01-04 11:11', width: 0.6 }
      ])
      .style('NORMAL')
      .tableCustom([
        { text: 'Order ID:', width: 0.2 },
        { text: '050-7866-2406', width: 0.6 }
      ])
      .style('NORMAL')
      .tableCustom([
        { text: '전화번호:', width: 0.2 },
        { text: '200000000', width: 0.6, align: 'LEFT' }
      ], { encoding: 'EUC-KR' })
      .tableCustom([
        { text: '메모:', width: 0.4 },
        { text: '문앞에 두고 벨을 눌러주세요', width: 0.6 }
      ], 'CP949')
      .drawLine()
      .newLine()
      .tableCustom([
        { text: '기사님 이름:', width: 0.4 },
        { text: '기사이름', width: 0.6 }
       ], 'CP949')
      .tableCustom([
        { text: '기사님 번호:', width: 0.4 },
        { text: '010-1234-5678', width: 0.6 }
       ], 'CP949')
      .drawLine()
      .newLine()
      .tableCustom([
        { text: '내부규', width: 0.33, align: 'LEFT' },
        { text: '내부', width: 0.33, align: 'CENTER' },
        { text: '율과', width: 0.33, align: 'CENTER' }
      ], 'CP949')
      .tableCustom([
        { text: '내부규율과', width: 0.33, align: 'LEFT' },
        { text: '1', width: 0.33, align: 'CENTER' },
        { text: '2,000,000 원', width: 0.33, align: 'CENTER' }
      ], 'CP949')
      .drawLine()
      .newLine()
      .tableCustom([
        { text: '합계', width: 0.6, align: 'LEFT' },
        { text: '2,000,000 원', width: 0.4, align: 'RIGHT' }
      ], 'CP949')
      .tableCustom([
        { text: '배송료(11.2km)', width: 0.6, align: 'LEFT' },
        { text: '2,000 원', width: 0.4, align: 'RIGHT' }
      ], 'CP949')
      .tableCustom([
        { text: '수수료', width: 0.6, align: 'LEFT' },
        { text: '-0 원', width: 0.4, align: 'RIGHT' }
      ], 'CP949')
      .drawLine()
      .newLine()
      .tableCustom([
        { text: '총금액', width: 0.6, align: 'LEFT' },
        { text: '2,000,000 원', width: 0.4, align: 'RIGHT' }
      ], 'CP949')
      .newLine()
      .newLine()
      .newLine()
      .cut();

      setTimeout(function(){
        printer.close();
      }, 1000);
    });
  }

};

I hope it helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM