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

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

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

<think> 我们正在讨论Vue3项目中如何使用vue-plugin-hiprint插件实现打印功能。根据引用内容,我们可以总结出以下步骤和示例: 1. 安装插件:在Vue3项目中安装vue-plugin-hiprint插件。 2. 引入插件:在需要使用的文件中引入插件。 3. 使用插件:创建打印模板,设计打印内容,并调用打印方法。 下面我们详细说明: ### 1. 安装插件 在项目根目录下运行以下命令安装插件: ```bash npm install vue-plugin-hiprint ``` ### 2. 引入插件 在Vue3组件中,我们可以按需引入插件: ```javascript import { hiprint } from 'vue-plugin-hiprint'; import 'vue-plugin-hiprint/dist/hiPrint.css'; ``` ### 3. 使用插件 以下是一个简单的示例,展示如何创建一个打印模板并打印。 #### 3.1 创建模板 首先,我们需要创建一个打印模板。可以通过代码创建,也可以使用可视化设计器(如果需要可视化设计,则需要额外引入设计器组件)。 ```javascript // 在组件的setup函数中 const createTemplate = () => { // 初始化模板 let template = new hiprint.PrintTemplate(); // 添加一个文本元素 template.addPrintElement({ type: 'text', title: '文本', options: { text: '这是一个文本示例', width: 100, height: 20, fontSize: 14, textAlign: 'center' } }); // 添加一个条形码 template.addPrintElement({ type: 'text', title: '条形码', options: { text: '1234567890', textType: 'barcode', width: 100, height: 40 } }); return template; }; ``` #### 3.2 打印模板 创建模板后,我们可以调用打印方法进行打印。 ```javascript // 在需要打印的地方调用 const print = () => { const template = createTemplate(); // 假设传入的数据为空对象,因为我们的示例文本是固定的 template.print({}); }; ``` ### 4. 直接打印(静默打印) 引用[1]和[3]提到了直接打印(静默打印)功能。这通常需要安装一个客户端程序,该程序会监听一个端口,然后通过插件调用该端口进行打印。 #### 4.1 安装客户端 根据引用[1],需要下载并安装一个名为`hiprint-Plugin`的客户端程序。安装后,该程序会在本地运行,监听一个端口(默认为17521)。 #### 4.2 配置直接打印 在代码中,我们需要设置直接打印的地址和端口: ```javascript hiprint.setConfig({ url: '127.0.0.1', // 客户端IP port: 17521, // 客户端端口 token: 'test' // 令牌,根据客户端设置 }); ``` #### 4.3 调用直接打印 使用创建的模板调用直接打印方法: ```javascript template.directPrint({}); ``` ### 5. 注意事项 - 条形码和二维码:使用`addPrintText`方法,设置`textType`为`'barcode'`或`'qrcode'`(引用[4])。 - 打印HTML使用`addPrintHtml`方法(引用[4])。 - 更多元素设置:参考官方文档 http://hiprint.io/docs/text(引用[4])。 ### 完整示例代码 下面是一个Vue3组件的示例代码: ```vue <template> <div> <button @click="print">打印</button> <button @click="directPrint">直接打印</button> </div> </template> <script> import { ref } from 'vue'; import { hiprint } from 'vue-plugin-hiprint'; import 'vue-plugin-hiprint/dist/hiPrint.css'; export default { setup() { let template = ref(null); // 创建模板 const createTemplate = () => { let tpl = new hiprint.PrintTemplate(); tpl.addPrintElement({ type: 'text', title: '文本', options: { text: '示例文本', width: 100, height: 20, fontSize: 14, textAlign: 'center' } }); tpl.addPrintElement({ type: 'text', title: '条形码', options: { text: '1234567890', textType: 'barcode', width: 100, height: 40 } }); return tpl; }; // 初始化模板 template.value = createTemplate(); // 普通打印(弹出预览) const print = () => { template.value.print({}); }; // 直接打印(静默打印) const directPrint = () => { // 确保已安装客户端并设置正确 hiprint.setConfig({ url: '127.0.0.1', port: 17521, token: 'test' }); template.value.directPrint({}); }; return { print, directPrint }; } }; </script> ``` ### 参考资料 - vue-plugin-hiprint项目地址:https://gitee.com/CcSimple/vue-plugin-hiprint - 使用入门篇:https://mp.weixin.qq.com/s?__biz=Mzg2NTA1MjcyNw==&mid=2247483846&idx=1&sn=1b6c20c8c2fdb805a8c3a14c0f0de5d1 - hiprint官方文档:http://hiprint.io/docs/start §§相关问题§§ 1. 如何解决vue-plugin-hiprintVue3中引入样式的问题? 2. 如何设计一个包含表格和图片的打印模板? 3. 如何实现动态数据打印(比如从API获取数据填充到打印模板)? 4. 直接打印时客户端安装后无法连接怎么办? </think> ### Vue3中使用vue-plugin-hiprint插件实现打印的方法和示例 #### 一、安装与配置 1. **安装插件**: ```bash npm install vue-plugin-hiprint ``` 2. **全局引入**(在main.js中): ```javascript import { createApp } from 'vue' import App from './App.vue' import hiprint from 'vue-plugin-hiprint' import 'vue-plugin-hiprint/dist/hiPrint.css' // 必须引入样式 const app = createApp(App) app.use(hiprint) // 全局注册 app.mount('#app') ``` #### 二、基本使用示例(无预览静默打印) ```vue <template> <div> <button @click="handlePrint">静默打印</button> <div id="hiprint-printTemplate"></div> </div> </template> <script setup> import { ref, onMounted } from 'vue' import { hiprint } from 'vue-plugin-hiprint' const template = ref(null) // 初始化模板 onMounted(() => { template.value = new hiprint.PrintTemplate({ template: { panels: [{ width: 100, height: 150, printElements: [ { type: 'text', title: '标题文本', options: { text: '订单详情', fontSize: 18, textAlign: 'center' } }, { type: 'text', options: { text: '产品名称: {{productName}}', width: 200, fontSize: 12 } }, { type: 'barcode', // 条形码 options: { text: '{{barcodeData}}', height: 40, width: 150 } } ] }] } }) }) // 静默打印 const handlePrint = () => { const printData = { productName: 'Vue3开发指南', barcodeData: 'ISBN-978755123456' } template.value.print(printData, { printer: '', // 默认打印机 title: '订单打印', // 打印任务名称 silent: true // 启用静默打印(无预览) }) } </script> ``` #### 三、关键功能实现 1. **条形码/二维码打印**: ```javascript template.addPrintElement({ type: 'text', options: { textType: 'barcode', // 或 'qrcode' text: '{{barcodeValue}}', width: 100 } }) ``` 2. **HTML内容打印**: ```javascript template.addPrintElement({ type: 'html', options: { content: '<div class="custom-html">自定义内容</div>', width: 200 } }) ``` 3. **直接打印配置**(需安装客户端): ```javascript // 设置打印机连接 hiprint.setConfig({ url: '127.0.0.1', // 客户端IP port: 17521, // 默认端口 token: 'your-token' }) // 调用直接打印 template.directPrint(data) ``` #### 四、注意事项 1. **静默打印要求**: - 必须安装打印客户端(引用[1]) - 客户端下载地址通常在插件文档中提供 - 首次使用需授权打印机访问权限 2. **数据绑定**: - 使用`{{variable}}`语法绑定动态数据 - 支持嵌套对象:`{{user.address.city}}` 3. **设计器使用**: ```javascript // 启用可视化设计器 const designer = new hiprint.PrintDesigner('#designer-container', template) ``` #### 五、官方资源 - [GitHub项目地址](https://gitee.com/CcSimple/vue-plugin-hiprint)(引用[2]) - [官方文档](http://hiprint.io/docs/start)(引用[2]) - [示例教程](https://mp.weixin.qq.com/s/1b6c20c8c2fdb805a8c3a14c0f0de5d1)(引用[3]) > 提示:最新版本(>=0.0.50)已支持Vue3全局引入和多语言(i18n)配置(引用[3])
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值