代码之家  ›  专栏  ›  技术社区  ›  Mr Ken

使用escpos包和串行端口创建收据

  •  0
  • Mr Ken  · 技术社区  · 4 年前

    我正在尝试构建一个电子应用程序,并创建一个简单的收据,以便通过串行端口打印。

    我正在使用:

    • 电子: ^11.1.1
    • escpos: ^3.0.0-alpha.6
    • escpos串行端口: ^3.0.0-alpha.4

    这是我创建收据的代码:

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

    当我打电话的时候 printSerial 方法。收据打印成功。但韩语的文本是不正确的。我尝试了许多编码名称,但仍然不起作用。

    我如何更改收据上的字体大小,收据上的当前文本很大。

    也许我错过了什么?

    非常感谢进步的帮助。

    0 回复  |  直到 4 年前
        1
  •  0
  •   jkim    2 年前

    我也遇到了同样的问题,我找到了并解决了它。
    我共享韩语编码代码。

    朝鲜语编码代码: cp949

    println text
    这个包默认字体太大, 所以你需要控制字体大小

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

    添加此代码:

    const options = { encoding: 'cp949' }
    

    完整代码:

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

    我希望这对你有帮助。