vue + Lodop 实现浏览器自动打印 无需预览打印

news/2024/9/23 9:37:13/

官网地址:https://www.lodop.net/download.html
先去Lodop官网下载相应的安装包
解压安装将LodopFuncs.js放在项目中utils文件夹中加一行代码

javascript">export { getLodop }; //导出
<template><div><div class="main"><ul class="btns"><li @click="start">开始打印</li><li @click="end">停止打印</li></ul><div class="user" v-if="list.length"><div class="userList" v-for="(item, index) in list" :key="index"><div>{{ item.name }}</div><div class="status"><div v-if="item.status">已打印</div><div v-if="!item.status">未打印</div></div></div></div><div v-if="!list.length" class="img"><img src="../../assets/errs.png" alt="" /></div></div></div>
</template><script>javascript">
import { getUnprintedList } from "@/utils/http.js";
import { getLodop } from "@/utils/LodopFuncs"; 
export default {data() {return {timer: null,list: [],beforeList: [],id: 2,};},created() {this.$store.commit("setPath", "/activityList");this.getListBefore();},methods: {async getListBefore() {const { data: res } = await getUnprintedList({activityId: this.$route.query.id,});if (!res.content) {return this.$message.warning("没有需要打印的信息了");}res.content.forEach((item) => {if (item.remark) {if (item.remark.length > 21) {item.firstPart = item.remark.substring(0, 21);item.secondPart = item.remark.substring(21);}}});this.list = res.content;this.id = 1;},start() {this.timer = setInterval(() => {this.getList();}, 7000); // 每7秒请求一次接口},end() {if (this.timer) {clearInterval(this.timer);}},async getList() {if (this.id == 1) {this.list.forEach((item) => {item.status = true;});this.beforeList = this.list;this.id = 2;this.forList();return;}this.list.forEach((item) => {item.status = true;});const { data: res } = await getUnprintedList({activityId: this.$route.query.id,});this.id = 2;if (!res.content) {if (this.timer) {clearInterval(this.timer);}return this.$message.warning("没有需要打印的信息了");}res.content.forEach((item) => {if (item.remark) {if (item.remark.length > 21) {item.firstPart = item.remark.substring(0, 21);item.secondPart = item.remark.substring(21);}}});this.beforeList = res.content;res.content.forEach((item) => {this.list.unshift(item);});console.log(this.list);//   return;this.forList();},forList() {this.beforeList.forEach((item) => {this.btnClickPrint(item);});},// 打印快递单btnClickPrint(data) {let tops = 30;if (data.position.length > 9) {tops = 50;}const LODOP = getLodop(); // 调用getLodop获取LODOP对象LODOP.PRINT_INIT("");LODOP.SET_PRINT_PAGESIZE(1, "55mm", "60mm", ""); // 设置纸张大小LODOP.ADD_PRINT_TEXT(0, 0, 170, 20, `姓名:${data.name}`);LODOP.ADD_PRINT_TEXT(15, 0, 160, 20, `职位:${data.position}`);LODOP.ADD_PRINT_TEXT(tops, 0, 160, 20, `公司:${data.companyName}`);if (data.firstPart) {LODOP.ADD_PRINT_TEXT(120, 0, 160, 20, `备注:${data.firstPart}`);LODOP.ADD_PRINT_TEXT(155, 0, 160, 20, `${data.secondPart}`);} else {LODOP.ADD_PRINT_TEXT(120, 0, 160, 20, `备注:${data.remark}`);}//   LODOP.ADD_PRINT_TEXT(130, 10, 150, 20, " ");// LODOP.SET_PRINT_STYLEA(0, "UseStyle", 1);//   LODOP.PREVIEW(); // 预览并打印、LODOP.PRINT();},beforeDestroy() {// 清除定时器if (this.timer) {clearInterval(this.timer);}},},
};
</script><style scoped lang='scss'>
.main {background: #fff;min-height: 400px;padding-top: 1px;margin-top: 20px;border-radius: 10px;
}
.btns {margin: 24px;display: flex;cursor: pointer;
}.btns > li:nth-of-type(1) {width: 146px;height: 44px;background: linear-gradient(214deg, #059ff4 0%, #0266e6 100%);box-shadow: 0px 2px 10px 0px rgba(3, 107, 231, 0.6);border-radius: 4px;font-size: 18px;font-family: SourceHanSansCN, SourceHanSansCN;font-weight: 500;color: #ffffff;line-height: 27px;text-shadow: 0px 2px 10px rgba(3, 107, 231, 0.6);text-align: center;line-height: 44px;margin-right: 40px;
}.btns > li:nth-of-type(2) {width: 146px;height: 42px;border-radius: 4px;border: 2px solid #026ce7;font-size: 18px;font-family: SourceHanSansCN, SourceHanSansCN;font-weight: 500;color: #026ce7;text-align: center;line-height: 42px;margin-right: 40px;
}
.user {display: flex;box-sizing: border-box;padding: 20px;flex-wrap: wrap;
}
.userList {background: #e5e4ff;border-radius: 4px;color: #000;margin-bottom: 6px;width: 80px;height: 70px;line-height: 70px;text-align: center;margin-right: 10px;font-size: 14px;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;position: relative;cursor: pointer;
}
.status {position: absolute;bottom: 2px;right: 0px;background: #999999;line-height: 20px;font-size: 12px;color: #fff;width: 50px;border-radius: 4px;opacity: 0.4;
}
.img {text-align: center;padding-bottom: 170px;
}
.img > img {width: 500px;
}
</style>

已经试验过可以实现,有问题可以互相交流


http://www.ppmy.cn/news/1476011.html

相关文章

【Flask从入门到精通:第十课:数据基本查询、关联查询】

基本查询 SQLAlchemy常用的查询过滤器 过滤器说明filter()把过滤器添加到原查询上&#xff0c;返回一个新查询filter_by()把等值过滤器添加到原查询上&#xff0c;返回一个新查询limit()使用指定的值限定原查询返回的结果数量offset()设置结果范围的开始位置&#xff0c;偏移…

算法篇 滑动窗口 leetCode 无重复字符的最长子串

无重复字符的最长子串 1.题目描述2.图解文字分析3.代码演示 1.题目描述 2.图解文字分析 3.代码演示

elasticsearch源码分析-05分片分配

分片分配 上面的集群状态和索引级状态已经恢复完成&#xff0c;开始分配索引分片&#xff0c;恢复分片级元数据&#xff0c;构建routingTable路由表 allocationService的创建在ClusterModule的构造函数中 public ClusterModule(Settings settings, ClusterService clusterSer…

ssh作用及原理

目录 什么是SSH ssh作用 ssh配置 Port UseDNS yes SyslogFacility AUTHPRIV LoginGraceTime 2m PermitRootLogin yes PasswordAuthentication yes PermitEmptyPasswords no PrintLastLog yes MaxAuthTries 6 什么是SSH 那天看到一篇关于SSH的博客,我想到,其实关…

QT--控件篇四

一、对话框 在软件开发中&#xff0c;对话框&#xff08;Dialog&#xff09;是一种常见的用户界面元素&#xff0c;用于与用户进行交互和获取信息。它通常以模态或非模态的形式出现&#xff0c;模态对话框会阻止用户与应用程序的其他部分交互&#xff0c;直到对话框关闭为止&a…

C++ :内联函数inline|nullptr

欢迎来到HarperLee的学习笔记&#xff01; 博主主页传送门&#xff1a;HarperLee博客主页&#xff01; 欢迎交流学习&#xff01; 一、inline关键字 1.1 什么是内联函数&#xff1f; 内联函数&#xff1a;用** inline 修饰的函数叫做内联函数&#xff0c;编译时C编译器会在调用…

[Linux+git+Gitee+Jenkins]持续集成实验安装配置详细

首先理解持续集成原理&#xff0c;看懂并理解图 1。 图 1 持续集成原理结构 图 1 中&#xff0c;版本控制服务器指远程代码仓库&#xff0c;本实验使用 GitEE 作为远程代码仓库&#xff1b;Jenkins 自动化部署服务器为虚拟机&#xff0c;操作系统为 Linux &#xff1b…

数仓工具—Hive基础之临时表及示例

Hive基础之临时表及示例 临时表是应用程序自动管理在大型或复杂查询执行期间生成的中间数据的一种便捷方式。Hive 0.14 及更高版本支持临时表。可以在用户会话中像使用普通表一样多次使用它们。在本文中,我们将介绍 Apache Hive 临时表,以及如何创建和使用限制的示例。 Hiv…