前端VUE项目实现静默打印,无需用户手动确认

一、基础环境配置

  1. ‌安装核心依赖‌

    lua 复制代码
    npm install vue-plugin-hiprint socket.io-client jquery --save

    需同时安装socket.io-client实现静默打印通信,jquery用于DOM操作。

  2. ‌全局引入插件 ‌

    在main.js中配置:

    javascript 复制代码
    javascriptCopy 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"}
  3. ‌样式文件配置 ‌

    复制node_modules/vue-plugin-hiprint/dist/print-lock.css到项目public目录,并在index.html添加:

    ruby 复制代码
    htmlCopy Code
    <link rel="stylesheet" href="<%= BASE_URL %>print-lock.css" media="print">:ml-citation{ref="3,5" data="citationList"}

二、静默打印实现

  1. ‌客户端安装 ‌

    下载electron-hiprint客户端并以管理员身份安装,配置默认打印机和TOKEN(如TEST_PRINT)。

  2. ‌打印模板初始化‌

    php 复制代码
    javascriptCopy 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"}
      }
    })
  3. ‌静默打印触发‌

    php 复制代码
    javascriptCopy 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"}
    })

三、企业级优化方案

  1. ‌批量打印处理‌

    ini 复制代码
    javascriptCopy 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"}
  2. ‌PDF静默打印‌

    php 复制代码
    javascriptCopy Code
    this.$hiprint.hiwebSocket.send({
      type: 'url_pdf',
      pdf_path: 'http://example.com/file.pdf',
      copies: 2 // 打印份数:ml-citation{ref="6,8" data="citationList"}
    })

四、常见问题解决

  1. ‌跨域问题 ‌
    需确保客户端和前端项目同域名或配置HTTPS。
  2. ‌Node版本兼容 ‌
    推荐使用Node 16.x版本,高版本可能出现兼容性问题11。
  3. ‌样式冲突 ‌
    如遇UI框架样式干扰,需重写print-lock.css中的关键样式。
相关推荐
叼烟扛炮1 小时前
[LangChain] 学习笔记(03):打破失忆症——对话机制与上下文管理
java·前端·javascript·笔记·学习·langchain
IT_陈寒3 小时前
SpringBoot实战技巧总结
前端·人工智能·后端
TeamDev3 小时前
使用 JxBrowser 在桌面应用中集成 Spring
java·前端·后端·spring·混合应用·jxbrowser·浏览器控件
陈随易4 小时前
Rolldown 把插件改动回传提速 26 倍
前端·后端·程序员
笃行35012 小时前
集中式与分布式,KingbaseES 的四种形态共用一套内核
前端
陈随易13 小时前
Vue 编译器换掉 source map,快了三四成
前端·后端·程序员
ZhengEnCi14 小时前
12 种高级 UI 交互效果及对应 AI 提示词:从按压回弹到底部抽屉的物理感教学
前端·css·交互设计
IT_陈寒17 小时前
Redis大key删除为什么会导致服务卡顿?我的线上事故复盘
前端·人工智能·后端
用户27050777252418 小时前
MCP 最小 Server 实测:3 个 JSON-RPC 报错与修法
前端
用户4993350250718 小时前
一人VIP账号多人共用:Web 代理里的会话共享怎么实现
前端