前端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群。

相关推荐
IT_陈寒2 分钟前
Vite 5震撼发布!10个新特性让你的开发效率飙升200% 🚀
前端·人工智能·后端
一路向前的月光7 分钟前
uniapp(5)滚动列表scroll-view
前端·javascript·uni-app
Hilaku31 分钟前
就因为package.json里少了个^号,我们公司赔了客户十万块
前端·javascript·npm
晴殇i38 分钟前
尤雨溪创立的 VoidZero 完成 1250 万美元 A 轮融资,加速整合前端工具链生态
前端·vue.js
一大树1 小时前
MutationObserver 完整用法指南
前端
一晌小贪欢1 小时前
【Html模板】赛博朋克风格数据分析大屏(已上线-可预览)
前端·数据分析·html·数据看板·看板·电商大屏·大屏看板
墨寒博客栈1 小时前
Linux基础常用命令
java·linux·运维·服务器·前端
野生龟1 小时前
designable和formily实现简单的低代码平台学习
前端
路多辛1 小时前
为什么我要做一个开发者工具箱?聊聊 Kairoa 的诞生
前端·后端
jerryinwuhan1 小时前
理论及算法_时间抽取论文
前端·算法·easyui