本文介绍对vue-plugin-hiprint部分重要代码的解析,这是一个很好的开源插件,能够自己自定义打印模板,通过后端传来的数据进行渲染打印,官方也提供了许多的api供开发者使用。界面采用了antdesign。实现了免预览的直接打印。
😄 生命不息,写作不止
🔥 继续踏上学习之路,学之分享笔记
👊 总有一天我也能像各位大佬一样
🏆 一个有梦有戏的人 @怒放吧德德
🌝分享学习心得,欢迎指正,大家一起学习成长!
生命不息,写作不止,养成良好的学习精神!
本文介绍对vue-plugin-hiprint部分重要代码的解析,这是一个很好的开源插件,能够自己自定义打印模板,通过后端传来的数据进行渲染打印,官方也提供了许多的api供开发者使用。界面采用了antdesign。实现了免预览的直接打印。
github:https://github.com/CcSimple/vue-plugin-hiprint
print.io官网:http://hiprint.io/demo

jsbarcode:
socket.io:
jspdf:
分别是:拖拽组件、画布、属性栏
在挂载中调用初始化
其中初始化方法:
设置纸张大小
通过生命周期activated来解决切换模板的时候还能拖拽,并且不会被清除
封装的预览vue界面
将模板和数据用HTML的方法转化赋值 $('#preview_content_custom').html(hiprintTemplate.getHtml(printData))
直接打印需要安装桌面插件,window.hiwebSocket.opened是为了判断socketIo是否打开,hiprintTemplate中的print2是直接打印,print是会显示预览的打印。直接打印在printIo底层会自动去连接客户端,以及传输数据。
批量打印就是采用队列打印的方式,通过TaskRunner 任务进程管理,在通过for循环收集数据去打印。
batPrint() {
if (window.hiwebSocket.opened) {
const printerList = hiprintTemplate.getPrinterList();
console.log(printerList)
this.tasksPrint()
return
}
this.$message.error('客户端未连接,无法直接打印')
},
tasksPrint() {
const runner = new TaskRunner();
runner.setConcurrency(1);
const task = []
let that = this
const tasksKey = `open${Date.now()}`;
for (let i = 0; i < testDatas.table.length; i++) {
let key = `task${i}`;
task.push(done => {
let printData = {
testChinese: testDatas.table[i].testChinese,
testEnglish: testDatas.table[i].testEnglish
}
console.log('printData', printData)
that.realPrint(runner, done, key, i, printData, tasksKey)
})
}
runner.addMultiple(task)
this.openNotification(runner, tasksKey)
},
realPrint(runner, done, key, i, printData, tasksKey) {
let that = this
that.$notification.info({
key: key,
placement: 'topRight',
duration: 2.5,
message: `正在准备打印第 ${i} 张`,
description: '队列运行中...',
});
let template = that.$ls.get('KEY_TEMPLATES', {})
let hiprintTemplate = new hiprint.PrintTemplate({
template: template.aProviderModule,
});
hiprintTemplate.print2(printData, {printer: '', title: key});
hiprintTemplate.on('printSuccess', function () {
let info = runner.tasks.list.length > 1 ? '准备打印下一张' : '已完成打印'
that.$notification.success({
key: key,
placement: 'topRight',
message: key + ' 打印成功',
description: info,
});
done()
if (!runner.isBusy()) {
that.$notification.close(tasksKey)
}
})
hiprintTemplate.on('printError', function () {
that.$notification.close(key)
done()
that.$message.error('打印失败,已加入重试队列中')
runner.add(that.realPrint(runner, done, key, i, printData))
})
},
openNotification(runner, tasksKey) {
let that = this;
that.$notification.open({
key: tasksKey,
message: '队列运行中...',
duration: 0,
placement: 'topLeft',
description: '点击关闭所有任务',
btn: h => {
return h(
'a-button',
{
props: {
type: 'danger',
size: 'small',
},
on: {
click: () => {
that.$notification.close(tasksKey);
runner.removeAll();
that.$message.info('已移除所有任务');
},
},
},
'关闭任务',
);
},
});
}
只要调用apihiprintTemplate.getJson()
封装js中,使用addPrintElementTypes方法添加自定义的组件,可以查看print.io官方文档来配置参数。通过控制台输入window.HIPRINT_CONFIG可以查看配置参数名。
new hiprint.PrintElementTypeGroup("自定义表格1", [
{
tid: 'aProviderModule.customText1',
title: '表格标题',
customText: '自定义文本',
custom: true,
width: 120,
type: 'text',
options: {
height: 31.5,
hideTitle: true,
field: 'testEnglish',
fontSize: 20.25,
color: '#000000',
backgroundColor: '#ffffff',
textAlign: 'center',
textContentVerticalAlign: 'middle',
lineAlign: 'center',
borderLeft: 'solid',
borderTop: 'solid',
borderRight: 'solid',
borderBottom: 'solid'
}
},
{
tid: 'aProviderModule.customText2',
title: '表格内容',
customText: '自定义文本',
custom: true,
width: 120,
type: 'text',
options: {
hideTitle: true,
field: 'testChinese',
height: 31.5,
fontSize: 20.25,
color: '#000000',
backgroundColor: '#ffffff',
textAlign: 'center',
textContentVerticalAlign: 'middle',
lineAlign: 'center',
borderLeft: 'solid',
borderTop: 'solid',
borderRight: 'solid',
borderBottom: 'solid'
}
},
]),
👍创作不易,如有错误请指正,感谢观看!记得点个赞哦!👍
__EOF__
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)