一、基础环境配置
-
安装核心依赖
luanpm install vue-plugin-hiprint socket.io-client jquery --save
需同时安装socket.io-client实现静默打印通信,jquery用于DOM操作。
-
全局引入插件
在
main.js
中配置:javascriptjavascriptCopy Code import { hiPrintPlugin } from 'vue-plugin-hiprint' import $ from 'jquery' Vue.use(hiPrintPlugin, '$hiprint') Vue.prototype.$ = $ hiPrintPlugin.disAutoConnect() // 禁用自动连接客户端:ml-citation{ref="4,5" data="citationList"}
-
样式文件配置
复制
node_modules/vue-plugin-hiprint/dist/print-lock.css
到项目public
目录,并在index.html
添加:rubyhtmlCopy Code <link rel="stylesheet" href="<%= BASE_URL %>print-lock.css" media="print">:ml-citation{ref="3,5" data="citationList"}
二、静默打印实现
-
客户端安装
下载electron-hiprint客户端并以管理员身份安装,配置默认打印机和TOKEN(如
TEST_PRINT
)。 -
打印模板初始化
phpjavascriptCopy Code const template = new this.$hiprint.PrintTemplate({ settingContainer: '#printSettings' }) const panel = template.addPrintPanel({ width: 210, height: 297, paperFooter: 340 }) panel.addPrintText({ options: { width: 100, height: 20, title: '产品条码', textType: 'barcode' // 支持条形码/二维码:ml-citation{ref="13,19" data="citationList"} } })
-
静默打印触发
phpjavascriptCopy Code // 连接打印客户端 const socket = io("http://localhost:17521", { transports: ["websocket"], auth: { token: "TEST_PRINT" } }) // 发送打印指令 socket.emit("news", { html: template.getHtml(), pageSize: 'A4', printer: '打印机名称' // 不填则使用默认打印机:ml-citation{ref="4,6" data="citationList"} })
三、企业级优化方案
-
批量打印处理
inijavascriptCopy Code const batchData = [{id:1}, {id:2}] batchData.forEach(item => { panel.updateElement('barcode', { title: item.id }) socket.emit("news", { html: template.getHtml() }) }):ml-citation{ref="16,19" data="citationList"}
-
PDF静默打印
phpjavascriptCopy Code this.$hiprint.hiwebSocket.send({ type: 'url_pdf', pdf_path: 'http://example.com/file.pdf', copies: 2 // 打印份数:ml-citation{ref="6,8" data="citationList"} })
四、常见问题解决
- 跨域问题
需确保客户端和前端项目同域名或配置HTTPS。 - Node版本兼容
推荐使用Node 16.x版本,高版本可能出现兼容性问题11。 - 样式冲突
如遇UI框架样式干扰,需重写print-lock.css
中的关键样式。