vue-plugin-hiprint简单使用打印html

该文章介绍了一个Vue项目中使用vue-plugin-hiprint插件进行无预览直接打印HTML的方法,特别是针对快递单模板的打印。开发者首先安装了必要的依赖,然后在main.js中配置,并在特定路由中初始化打印对象,获取打印机列表,最后调用打印方法。文章强调了正确设置打印机客户端连接的重要性以及处理可能出现的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文实现了无预览 指定打印机直接打印html,附带一个快递单模板

开发阶段安装了三个依赖

npm install vue-plugin-hiprint
npm i jquery --save-d
npm install socket.io

在main.js中

import {hiPrintPlugin} from 'vue-plugin-hiprint'
Vue.use(hiPrintPlugin, '$pluginName')


import $ from "jquery";
window.jquery = window.$ = $;
Vue.prototype.$ = jquery

在项目/public/index.html中

   <link rel="stylesheet" type="text/css" media="print" href="https://cdn.jsdelivr.net/npm/vue-plugin-hiprint@latest/dist/print-lock.css">

最后就到你需要打印的路由了


mounted() {
    this.init()
  },
   methods: {
     init() {
      // 初始化 打印对象
      const hiprintTemplate_ = new this.$pluginName.PrintTemplate()
      this.hiprintTemplate = hiprintTemplate_
    },
      // 获取打印机列表
    getPrinterList() {
      console.log(window.hiwebSocket)
      if (window.hiwebSocket.opened === false) {
        this.$notify.error('打印机客户端未连接')
      }
      // 模板对象获取
      const printerList_ = this.hiprintTemplate.getPrinterList()
      // console.info(printerList_)
    },
     confirmPrintPrint() {

      // 如果在 main.js 中设置了取消自动连接客户端 是获取不到打印机列表的!!!
      if (window.hiwebSocket.opened === false) {
        this.$notify.error('打印机客户端未连接')
      }

      // 这一句代码 如果打印出来有问题 可以尝试加进去,没有出现 则不用加
      // 初始化 provider
      this.$pluginName.init({
        providers: [defaultElementTypeProvider()]
      })

      // 这一句代码 如果打印出来有问题 可以尝试加进去,没有出现 则不用加
      this.$pluginName.PrintElementTypeManager.buildByHtml(this.$('.ep-draggable-item'))

      // 不要使用 this.hiprintTemplate 打印,会出现重复打印,如果要用,请每次打印 都清空内容
      const hiprintTemplate_ = new this.$pluginName.PrintTemplate()
      //打印方法
	hiprintTemplate_.printByHtml($("#printBill"))//预览打印

	 //hiprintTemplate_.printByHtml2($("#printIframeEMS"), {//跳过预览直接打印
            //    printer: '邮政', // 指定打印机 打印机 名称
              //  title: '邮政打印任务',
             //   color: false, // 是否打印颜色 默认 true
              //  copies: 1, // 打印份数 默认 1
             // });
      }

   },

官网文档中还有一种通过拖拽生成模板的方式,再通过print2方法向其中添加数据的方式我没使用
// 直接打印 带参数
// hiprintTemplate_.print2(null, {
// printer: ‘webSockets’, // 指定打印机 打印机 名称
// title: ‘打印任务名称’,
// color: false, // 是否打印颜色 默认 true
// copies: 1, // 打印份数 默认 1
// });

}

快递模板

 <!-- 打印面单EMS-->
      <div id="printIframeEMS" style="padding: 0 ; margin: 0 ;">
        <div style="margin:0px auto 0px;page-break-after:always" v-for="item, index in dataEMS" :key="index">
          <div style="margin-left:5px ;padding: 0 50px;">
            <div
              style="height: 70px;border: 1px dotted #000;font-size: 12px !important;overflow: hidden;box-sizing:border-box;">
              <div style="width: 100%;height: 100%;float: left;text-align: center;box-sizing:border-box;">
                <div style="float: right;">
                  <div style="font-size: 16px;margin-top:1px;text-align: right;">国际快递</div>
                </div>
                <div class="three" style="font-size: 14px;margin:1px 0;margin-top: 40px; text-align: center !important;">
                  {{ item.bigPen }}</div>
              </div>
            </div>
            <div
              style="height: 95px;border: 1px dotted #000;font-size: 12px !important;overflow: hidden;border-top:none;box-sizing:border-box;">
              <div
                style="width: 10%;height: 100%;float: left;border-right: 1px dotted #000;padding:5px 0 0 5px;box-sizing:border-box;">
                <div>
                  收件:
                </div>
              </div>
              <div
                style="font-size:12px;text-align:center;display:block;padding-top: 0px;box-sizing: border-box;float: left; width: 88%;">
                <div style="padding-left:5px;box-sizing:border-box;text-align: left;">
                  收件地址:<span>{{ item.consigneeAddress }}</span></div>
                <div style="padding-left:5px;box-sizing:border-box;text-align: left;">收件电话:<span
                    style="margin-right:3px">{{ item.consigneePhone }}</span><br>收件人:<span>{{ item.consigneeName }}</span>
                </div>

              </div>
            </div>
            <div
              style="height: 65px;border: 1px dotted #000;font-size: 12px !important;overflow: hidden;border-top:none;box-sizing:border-box;">
              <div
                style="width: 10%;height: 100%;float: left;border-right: 1px dotted #000;padding:5px 0 0 5px;box-sizing:border-box;">
                <div>
                  寄件:<span></span>
                </div>
              </div>
              <div
                style="font-size:12px;text-align:center;display:block;padding-top: 0px;box-sizing: border-box;float: left; width: 88%;">
                <div style="padding-left:5px;box-sizing:border-box;text-align: left;">寄件始发地:<span>{{ item.senderAddress
                }}</span> </div>
                <div style="padding-left:5px;box-sizing:border-box;text-align: left;">寄件人电话:<span>{{ item.senderTelephone
                }}</span></div>
              </div>
            </div>
            <div
              style="height: 80px;border: 1px dotted #000;font-size: 12px !important;overflow: hidden;box-sizing:border-box;">
              <div style="width: 100%;height: 100%;float: left;text-align: center;box-sizing:border-box;">
                <div style="font-size: 22px;margin-top:1px;"></div>
                <div style="font-size: 15px;">
                  <div><img :src="item.logisticsImg" alt="" style="max-width: 90%;height: 50px;"></div>
                  <div style="letter-spacing:5px">{{ item.logisticsNo }}</div>
                </div>
              </div>
            </div>
            <div
              style="height: 100px;border: 1px dotted #000;border-top:none;font-size: 12px !important;overflow: hidden;box-sizing:border-box;">
              <div
                style="width: 55%;height: 100%;float: left;border-right: 1px dotted #000;padding-right: 0;margin-left: 0;padding:0;box-sizing:border-box;">

                <div style="line-height:13px; margin-bottom: 0; width: 100%;font-size:10px">
                  快件送达收件人地址,经收件人或收件人(寄件人)允许的代收人签字,视为送达,您的签字代表您已签收此包裹,并已确认商品信息无误,包装完好,没有划痕,破损等方面质量问题</div>
                <div class="time" style="font-size: 12px;">{{ item.createTime }}</div>
              </div>
              <div style="width: 45%;float: left;height: 100%;padding:5px 0 0 5px;box-sizing:border-box;">
                <div style="margin-bottom: 0px;font-size:10px">
                  收件人/代收人:</div>
                <div style=" margin-bottom: 0px;font-size:10px;white-space: nowrap;">
                  签收时间:&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;</div>
                <div style="margin-top: 0;font-size:10px">

                  快件送达收货人地址,经收件人或收件人允许的代收人签字视为送达</div>
              </div>
            </div>
            <div
              style="height: 60px;border: 1px dotted #000;font-size: 12px !important;overflow: hidden;box-sizing:border-box;">
              <div style="width: 100%;height: 100%;float: left;text-align: center;box-sizing:border-box;">
                <div style="margin-top:0px;text-align: left;">内件描述: <span>/Description of Contents</span></div>
                <div style="margin-top:0px;text-align: left;">
                  <span>{{ item.commodityDescription }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
        <!-- </div> -->

      </div>

### 如何在 Vue3 中使用 `vue-plugin-hiprint` 实现 JSON 数据的打印 #### 安装插件 首先,在项目中通过 npm 或 yarn 安装 `vue-plugin-hiprint` 插件。可以通过以下命令完成安装: ```bash npm install vue-plugin-hiprint --save ``` 或者使用 yarn: ```bash yarn add vue-plugin-hiprint ``` 确认安装完成后,可以在项目的 `package.json` 文件中找到该插件及其具体版本号[^1]。 --- #### 页面引入与初始化 在 Vue3 项目中,需要先引入并注册 `vue-plugin-hiprint` 插件。通常可以将其放在全局文件(如 main.js)中进行配置: ```javascript import { createApp } from 'vue'; import App from './App.vue'; import Hiprint from 'vue-plugin-hiprint'; const app = createApp(App); app.use(Hiprint); // 注册插件 app.mount('#app'); ``` 这样就完成了插件的基础配置工作[^3]。 --- #### 打印 HTML 元素 在实际开发过程中,如果需要打印某个 DOM 节点中的内容,则可以直接调用 `this.$hiprint.print()` 方法传入目标节点的选择器即可。例如: ```javascript methods: { printHtmlElement() { this.$hiprint.print('#targetElement', {}, () => { console.log('打印成功!'); }); } } ``` 上述代码会将 id 为 `targetElement` 的 DOM 节点内容渲染到打印机上[^4]。 --- #### 打印 JSON 数据 对于 JSON 数据的打印需求,`vue-plugin-hiprint` 提供了一个灵活的方式来处理自定义模板和动态数据绑定的功能。以下是具体的实现方式: ##### 自定义模板结构 创建一个用于展示 JSON 数据的 HTML 模板字符串,并将其作为参数传递给 `printJsonTemplate` 函数。假设我们有如下 JSON 数据: ```json { "name": "张三", "age": 28, "address": "北京市朝阳区" } ``` 我们可以设计对应的模板样式如下所示: ```html <div class="json-print"> <h1>个人信息</h1> <p><strong>姓名:</strong>{{ name }}</p> <p><strong>年龄:</strong>{{ age }} 岁</p> <p><strong>地址:</strong>{{ address }}</p> </div> ``` 然后利用 JavaScript 将此模板与 JSON 数据相结合形成最终可打印的内容。 ##### 绑定数据至模板 下面是一段完整的示例代码展示了如何结合 JSON 数据以及自定义模板来进行打印操作: ```javascript methods: { printJsonData(jsonData) { const templateStr = ` <div class="json-print"> <h1>个人信息</h1> <p><strong>姓名:</strong>{{ name }}</p> <p><strong>年龄:</strong>{{ age }} 岁</p> <p><strong>地址:</strong>{{ address }}</p> </div> `; let compiledContent = Mustache.render(templateStr, jsonData); this.$hiprint.print({ content: compiledContent, style: '.json-print{font-family:"Arial", sans-serif; font-size:14px;}' }, function(result){ alert("打印已完成!"); }); } } ``` 这里需要注意的是,由于原生 JavaScript 并不直接支持模板引擎语法解析,因此推荐借助第三方库比如 **Mustache** 来帮助完成变量替换的任务。 --- #### 总结 通过对以上几个部分的学习,相信读者已经掌握了如何在 Vue3 环境下运用 `vue-plugin-hiprint` 插件去满足不同场景下的打印需求——无论是静态 HTML 片段还是复杂的 JSON 结构都可以轻松应对。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值