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

相关推荐
不想有bug的小菜鸟几秒前
vue3使用iframe全屏展示pdf效果
前端·pdf
m0_748238631 分钟前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
u0100559602 分钟前
前端代理,解决跨域问题讲解
前端
quitv7 分钟前
react脚手架配置别名
前端·javascript·react.js
m0_5287238116 分钟前
前端如何进行性能优化
前端·性能优化
化作繁星17 分钟前
在 Vue 3 中,如何缓存和复用动态组件
前端·vue.js·缓存
一粒沙-37 分钟前
iOS 将GIF图分享至微信
前端·ios
graywen41 分钟前
从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
前端
Gazer_S2 小时前
【现代前端框架中本地图片资源的处理方案】
前端·javascript·chrome·缓存·前端框架
林的快手4 小时前
CSS默认样式
前端·css·vue.js·chrome·css3