javascript ESC POS打印机通过节点控制

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript ESC POS打印机通过节点控制相关的知识,希望对你有一定的参考价值。

let options = {
  encoding: "EUC-KR"
}
// const printerPort = process.env.COM
const printerPort = process.env.COM
const escpos = require('escpos')
let nexonLogo = '_data/NexonFoundation_en.png'

class Printer {
  constructor() {
    this.isPrinting = false
  }
  printUserData(userData, callback) {
    let device, printer
    let options = {
      encoding: "EUC-KR"
    }
    // console.log('on printer',userData);
    device = new escpos.Serial(printerPort)
    printer = new escpos.Printer(device, options)
    console.log('printer is working');
    escpos.Image.load(nexonLogo, (image) => {
      device.open(() => {
        printer
          .align('lt')
          .text('\n')
          .text('\n')
          .size(2, 2)
          .style('b')
          .spacing(0)
          // Exhibition Logo
          .lineSpace(24)
          .align('ct')
          .text('Game    ')
          .text('a     ')
          .text(' me    ')
          .text('/i nvi te')
          .text(' yo u_  ')
          .align('lt')
          .text('\n')
          .text('\n')
          .text('\n')
          //ExhibitionAchivement
          .size(2, 2)
          .align('lt')
          .style('b')
          .lineSpace(28)
          .lineSpace(35)
          .text('EXHIBITION ARCHIVE')
          //ExhibitionData
          .size(1, 1)
          .style('normal')
          .align('lt')
          .lineSpace(35)
          .text('ID:' + userData.username)
          .text('DATE:' + userData.today)
          .text('PLAYTIME:' + userData.playtime)
          .lineSpace(42)
          .text('QUEST:' + userData.completedQuest.length + '/' + userData.questCount)
          .lineSpace(35)
          // Quest Accomplishment
          // Completed Quest
          .size(1, 1)
          .align('lt')
          .style('b')
          .lineSpace(34)
          .style('normal')
        this.userCompletedQuestPrint(userData, printer)
        printer
          // Failed Quest
          .style('b')
          .style('normal')
        this.userFailedQuestPrint(userData, printer)
        // for Users has Nexon Game Data
        if(userData.isNexonAccount){
          printer
            .text('\n')
            // Nexon Archive
            // Nexon Data Header
            .size(2, 2)
            .align('lt')
            .lineSpace(35)
            .style('b')
            .text('NEXON ARCHIVE')
            .size(1, 1)
            .style('normal')
            .align('lt')
            .text('ID:' + userData.username)
            .text('SINCE ' + userData.registerdDate)
            .text('FAVORITE(2019): ' + userData.favoriteGame)
            .size(1, 1)
            .lineSpace(34)
          this.userGamePrint(userData, printer)
        }
        printer
          .text('')
          .text('\n')
          .align('ct')
          .size(1, 1)
          .style('b')
          .text('THANKS FOR PLAYING GAME!')
          .size(1, 1)
          .lineSpace(28)
          .text('\n')
        for(let i = 0; i < 5; i++){
          printer
            .lineSpace(28)
            .size(1,1)
            .align('ct')
            // .style('b')
            .text(userData.couponGame[i])
            .style('normal')
            .align('ct')
            .size(1,1)
            .lineSpace(50)
            .text(userData.couponCode[i])
        }
        printer
          .lineSpace(50)
          .align('ct')
          .text('\n')
          .size(1, 1)
          .raster(image)
          .text('\n')
          .cut()
          .close(() => {
            callback()
          })
      })
    })
  }
  userCompletedQuestPrint(userData, printer) {
    let calcDotsBetweenText = this.calcDotsBetweenText.bind(this)
    for (let i = 0; i < userData.completedQuest.length; i++) {
      printer
        .text('[' + userData.completedQuest[i] + ']' + calcDotsBetweenText(42, '[' + userData.completedQuest[i] + ']', 'Completed') + 'Completed')
    }
  }
  userFailedQuestPrint(userData, printer) {
    let calcDotsBetweenText = this.calcDotsBetweenText.bind(this)
    for (let i = 0; i < userData.failedQuest.length; i++) {
      printer
        .text('[' + userData.failedQuest[i] + ']' + calcDotsBetweenText(42, '[' + userData.failedQuest[i] + ']', 'Failed') + 'Failed')
    }
  }
  userGamePrint(userData, printer) {
    for (const [i, game] of userData.gameData.entries()) {
      printer
        .text('{')
        .text('  "gameName": ' + game.gameName + ',')
        .text('  "mainCharaterId": ' + game.mainCharaterId + ',')
        .text('  "mainCharacterNickname": ' + game.mainCharacterNickname + ',')
        .text('  "mainCharacterTribe": ' + game.mainCharacterTribe + ',')
        .text('  "mainCharacterClass": ' + game.mainCharacterClass + ',')
        .text('  "mainCharacterLevel": ' + game.mainCharacterLevel + ',')
        .text('  "numCharacter": ' + game.numCharacter + ',')
        .text('  "PlayTimeInMinutes": ' + game.PlayTimeInMinutes + ',')
        .text('  "mainCharacterCreatedAt": ')
        .text('  ' + game.mainCharacterCreatedAt)
      if ((i + 1) == (userData.gameData.length))
        printer
        .text('}')
      else
        printer
        .text('},')
    }
  }
  calcDotsBetweenText(rowSpacesCount, leftText, rightText) {
    let dots = ''
    let remainingSpace = rowSpacesCount - Number(leftText.length) - Number(rightText.length)
    for (var i = 0; i < remainingSpace; i++) {
      dots += '.'
    }
    return dots
  }
  endOfPrinting(){
    console.log('Printing ends')
    isPrinting = false;
  }

  printTest(callback) {
    let device, printer
    let options = {
      encoding: "EUC-KR"
    }
    device = new escpos.Serial(printerPort)
    printer = new escpos.Printer(device, options)
    console.log('printer is working');

    device.open(function() {
      printer
        .size(1, 1)
        .align('lt')
        .text('\n')
        .text('\n')
        .style('b')
        .text('printer testing')
        .text('printer testing')
        .text('printer testing')
        .text('printer testing')
        .text('printer testing')
        .text('\n')
        .text('\n')
        .text('\n')
        .cut()
        .close(() => {
          callback()
        })
    })
  }
}
module.exports = Printer

以上是关于javascript ESC POS打印机通过节点控制的主要内容,如果未能解决你的问题,请参考以下文章

在 ESC/POS 命令中选择字符代码表

使用 Flutter 打印到 ESC/POS 时如何防止套接字超时错误

C# ESC/POS 打印越南语

如何在 Linux 中向热敏打印机发送 ESC/POS 命令

小票打印ESC/POS命令集

ESC/POS 热敏打印机 UTF-8 字符集设置