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

本文实现了无预览 指定打印机直接打印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>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值