uniapp(vue3) H5页面连接打印机并打印

本文主要是介绍uniapp(vue3) H5页面连接打印机并打印,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、找到对应厂商打印机的驱动并在windows上面安装。查看是否安装完成可以在:控制面板->查看设备和打印机,找到对应打印机驱动是否安装完成

二、打印机USB连接电脑

三、运行代码调用浏览器打印,主要使用的是window.print()功能。下面使用的是基于ifream的,这样可以控制到具体打印范围,全屏打印可以考虑不用。(原理应该就是打印机打印PDF)

四、打印样式不全或者其他,可以考虑设置打印格式 或者 代码样式调整

五、demo 代码

(1) 核心代码

(2) 完整代码

<template><div class="mainContent" @click="emits('close')"><div class="printContent"><iframe style="width: 100%;height: 100%" :src="ticketUrl" ref="ticketIframe" @load="onIframeLoad"></iframe><div class="btnPrint" @click="printReceipt">打印</div></div></div>
</template><script setup>
import {forDate} from "@/utlis/uni_api";
import {ref, onMounted, getCurrentInstance, toRefs} from 'vue';let {proxy} = getCurrentInstance();
let emits = defineEmits(["print", "close"])
let props = defineProps(["data"])
const ticketIframe = ref(null);
const ticketUrl = '../static/print.html';
let {data} = toRefs(props)
let merOrderList = {}
let originalPrice = ""
let sendTxt = ""
let goodsDetails = ""// 确保iframe加载完成
onMounted(async () => {await proxy.$nextTick();ticketIframe.value.contentWindow.focus();merOrderList = data.value.merOrderListlet proList = data.value.goodListfor (let i = 0; i < proList.length; i++) {let goodsName = proList[i].goodsNamelet goodsNum = proList[i].goodsNumlet realPrice = proList[i].realPricelet spec = proList[i].goodsSpecification//商品信息goodsDetails += goodsName + "[" + spec + "]" + "&nbsp;X&nbsp;" + goodsNum + "&nbsp;&nbsp;&nbsp;&nbsp;¥" + realPrice + "</br>"}//原价originalPrice = merOrderList.price + merOrderList.discountsPrice//配送时间sendTxt = data.value.isIm ? "立即配送" : data.value.predictTime
});function onIframeLoad() {console.log("data = ", data.value)let printerName = uni.getStorageSync("printerName")let iframe = ticketIframe.valuelet iframeDocument = iframe.contentWindow ? iframe.contentWindow.document : iframe.contentDocument;// 对于跨域限制不适用的情况,可以直接操作DOMif (iframeDocument) {if (printerName == "XP58C" || printerName == "POS58") {iframeDocument.body.innerHTML = ticketXP58C()}}
}function ticketXP58C() {return `<img style="width: 160px;height: 70px;margin: 0 0 30px 20px;" src="../static/image/public/print_logo.png"><div style="font-size: 20px;font-weight: bold;margin-bottom: 20px;">#${data.value.dayNum}&nbsp;&nbsp;&nbsp;*外卖狮配送*</div><div style="font-size: 10px;margin-bottom: 4px;">-----------------------------------------------</div><div style="display: flex;justify-content: space-between;font-size: 11px;margin-bottom: 4px;"><div>送达时间:</div><div>${sendTxt}</div></div><div style="display: flex;justify-content: space-between;font-size: 11px;margin-bottom: 4px;"><span>下单时间:</span><span>${forDate(merOrderList.createTime)}</span></div><div style="display: flex;justify-content: space-between;font-size: 11px;margin-bottom: 4px;"><div>订单编号:</div><div>${merOrderList.orderSn}</div></div><div style="font-size: 10px;margin-bottom: 4px;">-----------------------------------------------</div><div style="display: flex;font-size: 18px;font-weight:bold;margin-bottom: 4px;"><span>备注:</span><span>${data.value.remark ? data.value.remark : "无"}</span></div><div style="font-size: 12px;margin-bottom: 4px;">*************************************</div><div style="font-size: 11px;margin-bottom: 4px;">---------------------餐品------------------</div><div style="font-size: 11px;margin-bottom: 4px;">${goodsDetails}</div><div style="font-size: 11px;margin-bottom: 4px;">---------------------其他------------------</div><div style="display: flex;justify-content: space-between;font-size: 11px;margin-bottom: 4px;"><span>打包费:</span><span>¥${merOrderList.packPrice}</span></div><div style="display: flex;justify-content: space-between;font-size: 11px;margin-bottom: 4px;"><span>配送费:</span><span>¥${merOrderList.distributionPrice}</span></div><div style="display: flex;justify-content: space-between;font-size: 11px;margin-bottom: 4px;"><span>优惠价格:</span><span>-¥${merOrderList.discountsPrice}</span></div><div style="font-size: 12px;margin-bottom: 4px;">*************************************</div><div style="display: flex;justify-content: space-between;font-size: 11px;margin-bottom: 4px;">${merOrderList.payType == "alipay" ? "支付宝支付" : "微信支付"}</div><div style="display: flex;justify-content: space-between;font-size: 11px;margin-bottom: 4px;">原价:¥${originalPrice}</div><div style="display: flex;justify-content: right;font-size: 18px;font-weight: bold;margin-bottom: 4px;">实付:¥${merOrderList.price}</div><div style="font-size: 10px;margin-bottom: 30px;">------------------------------------------------</div><div style="display: flex;font-size: 18px;font-weight: bold;margin-bottom: 20px;">${data.value.name} ${data.value.phone.substr(0, 3) + "****" + data.value.phone.substr(data.value.phone.length - 4, data.value.phone.length)}</div><div style="font-size: 12px;margin-bottom: 60px;">*************************************</div><div style="font-size: 1px;">-</div>`//地址// < div style = "display: flex;font-size: 18px;font-weight: bold;margin-bottom: 15px;" >// ${merOrderList.address}// < /div>
}function printReceipt() {emits("close")ticketIframe.value.contentWindow.print(); // 调用iframe内部的window.print()proxy.$refs.ticketIframe.contentWindow.location.reload(true);
}
</script><style scoped>
.mainContent {position: fixed;top: 0;left: 0;width: 100%;height: 100%;min-height: 100vh;background-color: rgba(0, 0, 0, 0.5);z-index: 999999;display: flex;align-items: center;justify-content: center;
}.printContent {width: 70%;height: 50vh;background-color: #FFFFFF;box-sizing: border-box;display: flex;flex-direction: column;align-items: center;border-radius: 10rpx;padding: 20rpx;
}.btnPrint {width: 50%;margin: 80rpx auto 20rpx;font-size: 26rpx;color: #FFFFFF;background-color: #FF0000;text-align: center;padding: 10rpx 0;border-radius: 10rpx;
}iframe {border: none;
}
</style>

这篇关于uniapp(vue3) H5页面连接打印机并打印的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/837567

相关文章

一文详解如何使用Java获取PDF页面信息

《一文详解如何使用Java获取PDF页面信息》了解PDF页面属性是我们在处理文档、内容提取、打印设置或页面重组等任务时不可或缺的一环,下面我们就来看看如何使用Java语言获取这些信息吧... 目录引言一、安装和引入PDF处理库引入依赖二、获取 PDF 页数三、获取页面尺寸(宽高)四、获取页面旋转角度五、判断

C#连接SQL server数据库命令的基本步骤

《C#连接SQLserver数据库命令的基本步骤》文章讲解了连接SQLServer数据库的步骤,包括引入命名空间、构建连接字符串、使用SqlConnection和SqlCommand执行SQL操作,... 目录建议配合使用:如何下载和安装SQL server数据库-CSDN博客1. 引入必要的命名空间2.

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

MySQL中的表连接原理分析

《MySQL中的表连接原理分析》:本文主要介绍MySQL中的表连接原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、环境3、表连接原理【1】驱动表和被驱动表【2】内连接【3】外连接【4编程】嵌套循环连接【5】join buffer4、总结1、背景

前端如何通过nginx访问本地端口

《前端如何通过nginx访问本地端口》:本文主要介绍前端如何通过nginx访问本地端口的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、nginx安装1、下载(1)下载地址(2)系统选择(3)版本选择2、安装部署(1)解压(2)配置文件修改(3)启动(4)

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动

HTML input 标签示例详解

《HTMLinput标签示例详解》input标签主要用于接收用户的输入,随type属性值的不同,变换其具体功能,本文通过实例图文并茂的形式给大家介绍HTMLinput标签,感兴趣的朋友一... 目录通用属性输入框单行文本输入框 text密码输入框 password数字输入框 number电子邮件输入编程框

HTML img标签和超链接标签详细介绍

《HTMLimg标签和超链接标签详细介绍》:本文主要介绍了HTML中img标签的使用,包括src属性(指定图片路径)、相对/绝对路径区别、alt替代文本、title提示、宽高控制及边框设置等,详细内容请阅读本文,希望能对你有所帮助... 目录img 标签src 属性alt 属性title 属性width/h

SpringBoot连接Redis集群教程

《SpringBoot连接Redis集群教程》:本文主要介绍SpringBoot连接Redis集群教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 依赖2. 修改配置文件3. 创建RedisClusterConfig4. 测试总结1. 依赖 <de