npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

escpos-tspl.nodejs

v1.0.2

Published

electron escpos tspl print

Downloads

7

Readme

Nodejs-Escpos-Tspl-Print

It currently supports versions of nodejs >= 12.x.x and windows system.

Usage

支持Escpos/Tspl指令打印的Nodejs打印库,可用于Nodejs、Electron等环境。只支持windows系统。

Installation

$ npm install escpos-tspl.nodejs
$ yarn add escpos-tspl.nodejs

Example code (Bitmap)

import { queryUsbDevicePathFn, ipcTsplBitmap, ipcEscPosBitmap } from 'escpos-tspl.nodejs'

const base64Data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAAXNSR0IArs4c6QAAAF1JREFUKFNjZCASMDIwMIAwJwMDw3eoHhj7P7IZIEUgEMDAwHAPyhZkYGA4iG4RTCHIlCwGBoaXDAwMKxgYGP7gUggSN4RafwObs2EmguQMoApvElKI1//IJtJZIQDzWQwLlBenDAAAAABJRU5ErkJggg=='

/**
 * ESC/POS指令打印机
 */
const escposPrinterName = 'XP-80C'
queryUsbDevicePathFn(escposPrinterName, usbDevicePath => {
    if (!usbDevicePath) {
        cosnosole.log('没有找到打印机')
        return
    }
    // 获取到打印机usb设备路径后,调用ipcEscPosBitmap方法
    ipcEscPosBitmap(usbDevicePath, base64Data)
})

/**
 * TSPL指令打印机
 */
const tsplPrinterName = 'HPRT N41'
queryUsbDevicePathFn(tsplPrinterName, usbDevicePath => {
    if (!usbDevicePath) {
        cosnosole.log('没有找到打印机')
        return
    }
    // 获取到打印机usb设备路径后,调用ipcTsplBitmap方法
    ipcTsplBitmap(usbDevicePath, base64Data)
})

指令打印

// 代码示例:/example/command.js
import { queryUsbDevicePathFn, ipcTsplCommand, ipcEscPosCommand } from 'escpos-tspl.nodejs'

/**
 * ESC/POS指令打印机
 */
const escposPrinterName = 'XP-80C'
queryUsbDevicePathFn(escposPrinterName, usbDevicePath => {
    if (!usbDevicePath) {
        cosnosole.log('没有找到打印机')
        return
    }
    const commands = [
        0x1B, 0x40, // 初始化打印机  
        0x1B, 0x56, 0x30, // 设置文本大小(这里假设是0,表示默认大小)  
        0x1B, 0x21, 0x30, // 选择标准字符集(美国ASCII)  
        0x1B, 0x61, 0x30, // 设置文本对齐(0表示左对齐)  
        0x68, 0x65, 0x6C, 0x6C, 0x6F, // hello 文本(ASCII编码)  
        0x0A, // LF换行符(在某些打印机上可能需要,但ESC/POS通常使用GS E进行换行)  
        0x1B, 0x45, 0x32  // GS E 2 回车换行(更常用的ESC/POS换行方式)  
    ]
    // 获取到打印机usb设备路径后,调用指令打印方法
    ipcEscPosCommand(usbDevicePath, commands)
})

/**
 * TSPL指令打印机
 */
const tsplPrinterName = 'HPRT N41'
queryUsbDevicePathFn(tsplPrinterName, usbDevicePath => {
    if (!usbDevicePath) {
        cosnosole.log('没有找到打印机')
        return
    }
    const command = `
        ! 0 200 200 210 1\r\n
        TEXT 4 0 30 40 hello\r\n
        PRINT\r\n
    `
    // 获取到打印机usb设备路径后,调用指令打印方法
    ipcTsplCommand(usbDevicePath, command)
})

提示

推荐使用Bitmap打印,因为Bitmap打印支持的打印机型号更多,指令打印会有很多打印机不支持
并且Bitmap的打印样式更丰富。如果不知道要如何生成base64格式的图片,可以参考这个:https://github.com/zyhahaha/PrintJson2CanvasBase64/blob/master/example/index.html
通过canvans生成base64格式的图片,然后通过canvas.toDataURL('image/png')方法获取到图片的base64编码
或者可以使用html转canvans的第三方库,比如html2canvas,把html转成canvas,然后通过canvas.toDataURL('image/png')方法获取到图片的base64编码

关于异常状态

此库打印时进程是阻塞的,所以如果在Electron中使用时为了防止页面卡死,建议打印前在渲染进程开启Lodding,或者使用child_process方法在子进程中打印。
在打印机卡纸、缺纸时也是阻塞的,这时可以给使用者提示相应异常(如:缺纸请放入纸张),使用者解决打印机异常即可继续打印。
当打印机关闭电源时,打印任务会自动结束。