前端js,uniapp使用printease连接打印机打印,打印命令生成

接下来我们将使用printease实现前端js,uniapp打印。

此外,连接打印机的方式有很多种,这里并不会涉及到,该库只是提供了一种打印机指令的生成方式,你可以将生成的指令发送到打印机,打印机会执行一次印刷过程。

这个教程是关于tspl指令的,如果你使用的其他指令,可以提出issue,我们会尽快更新。

文档地址

参考文档地址

下载

bash 复制代码
npm install printease

使用教程

使用下面命令打印一个内容hello, printease!

打印文本

js 复制代码
import printsea from 'printsea'
let tspl = new printsea.tspl.TsplLib()
tspl.init()
tspl.setSize(60, 40)
tspl.setGap(0, 0)
tspl.setCls()
tspl.setText(50,50,'3',0,1,1,'Hello, Printease!')
tspl.setPrint(1)
console.log(tspl.getBuffer())

可以看到我们使用了``tspl.getBuffer()```来获取一段缓冲区数据,需要连接到打印机,然后将缓冲区内容发送到打印机,打印机会执行一次 印刷过程。

打印条码

js 复制代码
import printsea from 'printsea'
let tspl = new printsea.tspl.TsplLib()
tspl.init()
tspl.setSize(60, 40)
tspl.setGap(0, 0)
tspl.setCls()
tspl.setBarCode(10, 10, '39', 100, 1, 0, 2, 4, '123456789')
tspl.setPrint(1)
console.log(tspl.getBuffer())

打印二维码

js 复制代码
import printsea from 'printsea'
let tspl = new printsea.tspl.TsplLib()
tspl.init()
tspl.setSize(60, 40)
tspl.setGap(0, 0)
tspl.setCls()
tspl.setQRCode(10, 10, 'L', 5, 'A', 0, '123456789')
tspl.setPrint(1)
console.log(tspl.getBuffer())

打印图片

js 复制代码
import printsea from 'printsea'
let imageData = [0, 0, 0, 0, 0, 0, 7, 255, 3, 255, 17, 255, 24, 255, 28, 127, 30, 63, 31, 31, 31, 143, 31, 199, 31, 227, 31, 231, 31, 255, 31, 255]
let tspl = new printsea.tspl.TsplLib()
tspl.init()
tspl.setSize(60, 40)
tspl.setGap(0, 0)
tspl.setCls()
tspl.setBitmap(200, 200, 16, 16, 0, imageData)
tspl.setPrint(1)
console.log(tspl.getBuffer())

如果你不想自己组织图像数据,可以使用``imagePixelInformation```进行处理,如下所示:

js 复制代码
import printsea from 'printsea'
import img from './test.png'
let ctx = new printsea.imagePixelInformation.JavaScriptImagePixelInformation(100);
ctx.getImageData(img,200,300,(res)=>{
    let tspl = new printsea.tspl.TsplLib()
    tspl.init()
    tspl.setSize(60, 40)
    tspl.setGap(0, 0)
    tspl.setCls()
    tspl.setBitmap(10, 10, res.width, res.height, 0, res.data)
    tspl.setPrint(1)
    console.log(tspl.getBuffer())
})

如果您使用uniapp,请使用UniappImagePixelInformation```的uniGetImageData方法。 例如: ctx.uniGetImageData('myCanvas','./test.png',200,300,(res)=>{})```

js 复制代码
let ctx = new printsea.imagePixelInformation.UniappImagePixelInformation(100);
ctx.uniGetImageData('myCanvas',img,200,300,()=>{
    // ...
})

需要注意的是:由于uniapp的一些限制,您需要提前创建画布。 另外,画布的宽度和高度应设置为大于要打印图形的宽度和高度

设置编码

编码默认是utf-8,如果产生中文乱码,可以设置编码为GB18030,当然,也可以设置为其他编码。

js 复制代码
import printsea from 'printsea'
let tspl = new printease.tspl.TsplLib()
tspl.init()
tspl.setEncoding('GB18030')
tspl.setSize(60, 40)
tspl.setGap(3, 0)
tspl.setCls()
tspl.setText(10, 10, 'TSS24.BF2', 0, 1, 1, '世界你好')
tspl.setPrint(1)
console.log(tspl.getBuffer())

如果使用过程中遇到问题,可以在github上提issue,或者加入q群。

相关推荐
未来之窗软件服务3 小时前
一体化系统(九)智慧社区综合报表——东方仙盟练气期
大数据·前端·仙盟创梦ide·东方仙盟·东方仙盟一体化
陈天伟教授6 小时前
人工智能训练师认证教程(2)Python os入门教程
前端·数据库·python
信看7 小时前
NMEA-GNSS-RTK 定位html小工具
前端·javascript·html
Tony Bai7 小时前
【API 设计之道】04 字段掩码模式:让前端决定后端返回什么
前端
爱吃大芒果7 小时前
Flutter 主题与深色模式:全局样式统一与动态切换
开发语言·javascript·flutter·ecmascript·gitcode
苏打水com7 小时前
第十四篇:Day40-42 前端架构设计入门——从“功能实现”到“架构思维”(对标职场“大型项目架构”需求)
前端·架构
king王一帅7 小时前
流式渲染 Incremark、ant-design-x markdown、streammarkdown-vue 全流程方案对比
前端·javascript·人工智能
苏打水com8 小时前
第十八篇:Day52-54 前端跨端开发进阶——从“多端适配”到“跨端统一”(对标职场“全栈化”需求)
前端
Bigger8 小时前
后端拒写接口?前端硬核自救:纯前端实现静态资源下载全链路解析
前端·浏览器·vite