js 实现自定义打印模板

news/2024/10/28 18:59:33/

1.创建一个print.js文件 

//打印拣货单
import { tableStyle } from './printStyle'
export const printToTable = (data, callBack) => {//初始化打印内容console.log(data)let printNum = 1const imgPath = 'data:image/png;base64,' + data.barCodeconst img = new Image()img.src = imgPathimg.onload = () => {console.log('=====')// 打印的表格内容const tableContent = ` <div class="container"><div class="container_left"><div class="item"><span class="label">波次号:</span><span>${data.waveNo||''}</span></div><div class="item"><span class="label">波次类型:</span><span>${data.waveType||''}</span></div><div class="item"><span class="label">库位总数:</span><span>${data.locNum||''}</span></div><div class="item"><span class="label">订单总数:</span><span>${data.orderQty||''}</span></div><div class="item"><span class="label">商品件数:</span><span>${data.skyQty||''}</span></div><div class="item"><span class="label">承运商:</span><span>${data.carrierName||''}</span></div><div class="item"><span class="label">货主:</span><span>${data.customerName||''}</span></div><div class="item"><span class="label">打印时间:</span><span>${data.pickListPrintTime||''}</span></div></div><div class="container_right"><img class="img"  src="${imgPath}" alt="条形码" /></div></div><div class="table"><table ><thead><tr><th rowspan="2" class="w-60">序号</th><th rowspan="2" class="w-120">货品名称</th><th rowspan="2" class="w-60">应拣数量</th><th rowspan="2" class="w-60">拣货库位</th><th rowspan="2" class="w-120">货品编码</th><th rowspan="2" class="w-120">货品条码</th><th colspan="2" class="w-120">分拣格</th></tr><tr><th class="w-60">格号</th><th class="w-60">数量</th></tr></thead><tbody>${data.oubWavePickSkuInfoVOList.map((item) => `<tr><td class="w-60" rowspan="${item.inventorySlotList.length}">${item.sortNo||''}</td><td class="w-120" rowspan="${item.inventorySlotList.length}">${item.skuName||''}</td><td class="w-60" rowspan="${item.inventorySlotList.length}">${item.pickedNum||''}</td><td class="w-60" rowspan="${item.inventorySlotList.length}">${item.loc||''}</td><td class="w-120" rowspan="${item.inventorySlotList.length}">${item.sku||''}</td><td class="w-120" rowspan="${item.inventorySlotList.length}">${item.barCode||''}</td><td class="w-60">${item.inventorySlotList[0].sortingNo||''}</td><td class="w-60">${item.inventorySlotList[0].skuQty||''}</td></tr>${item.inventorySlotList.splice(1).map(i => `<tr><td class="w-60">${i.sortingNo||''}</td><td class="w-60">${i.skuQty||''}</td></tr>`).join('')}`).join('')}</tbody></table></div><div class="footer">合计:${data.skyQty}箱</div>`// 创建一个新的窗口用于打印// const newWindow = window.open('', '_blank')//创建iframe形式let iframe = document.createElement('iframe');//不写也没什么影响iframe.style.height = '0'iframe.style.width = '0'iframe.style.visibility = 'hidden'iframe.style.position = 'absolute'iframe.style.top = '0'document.body.appendChild(iframe);const newWindow = iframe.contentWindow;//写入数据newWindow.document.write('<html lang="en"><head><title>拣货单</title>')// 打印的表格样式newWindow.document.write(tableStyle)newWindow.document.write('</head><body>')// 将表格内容写入新窗口中newWindow.document.write(tableContent)newWindow.document.write('</body></html>')//监听打印或者取消事件的回调=》不能区分是否点击了打印const afterPrint = () => {console.log('打印后');printNum = printNum + 1if (printNum == 2) {callBack}}if (newWindow.document.matchMedia) {let mediaQueryList = newWindow.document.matchMedia('print');mediaQueryList.addListener((mql) => {console.log(mql)if (mql.matches) {beforePrint();} else {afterPrint();}});}newWindow.document.onafterprint = afterPrint;newWindow.document.close();newWindow.focus();newWindow.print();setTimeout(() => {document.body.removeChild(iframe);}, 3000);// // 打印新窗口中的内容// newWindow.print()// // 关闭新窗口// newWindow.close()}img.onerror = () => {console.log('加载失败')}
}

2.创建一个printStyle.js为打印内容填写样式

export const tableStyle = `
<style>* {padding: 0;margin: 0;}.print-timestamp {display: none;}.container {display: flex;width: 100%;margin: 10px auto;justify-content: space-between;}.container_left {display: flex;flex-wrap: wrap;width: calc(100% - 220px);}.container_right {width: 400px;}.img {width: 400px;height: 100px;display: block;}.pageLine {page-break-before: auto;page-break-after: always;}.item {min-width: 33.3%;}.footer {font-size: 18px;font-weight: bold;text-align: center;margin-top:4px;}table {width: 100%;border-collapse: collapse;}td {border: 1px solid black;text-align: center;font-size: 14px;}.w-60 {width: 80px;}th {border: 1px solid black;color: #000;}.print-title {font-size: 28px;font-weight: bold;text-align: center;margin-bottom: 20px;margin-top: 2px}
</style>`

3.使用

4.效果

 


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

相关文章

Spring Cloud --- Sentinel 授权规则

授权规则概述 在某些场景下&#xff0c;需要根据调用接口的来源判断是否允许执行本次请求。此时就可以使用 Sentinel 提供的授权规则来实现&#xff0c;Sentinel 的授权规则能够根据请求的来源判断是否允许本次请求通过。 在 Sentinel 的授权规则中&#xff0c;提供了 白名单…

3GPP协议解读_NTN系列(一)_38.811_非地面网络(NTN)的背景、应用场景和信道建模

非地面网络 1. Scope4. 非地面网络背景介绍4.1 5G中的非地面网络4.2 非地面网络在5G中的用例4.3 卫星和空中接入网的架构4.4 卫星和空中接入网终端的特点4.5 空气/星载飞行器特性4.6 NTN的覆盖模式4.7 NTN网络架构选项4.8 频谱 5. 非地面网络应用场景5.1 应用场景概览5.2 属性介…

unity开发之可视化制作动画

录制动画 1&#xff09;打开录制动画页面&#xff08;或者按快捷键ctrl6&#xff09; 2&#xff09;选中需要录制动画的对象 3&#xff09;创建动画列表&#xff0c;注意现在还没有录制动画&#xff0c;我这里创建了开门和关门动画列表 4&#xff09;选择需要录制动画的对象的相…

回忆Web编程的岁月变迁

目录 引子 记忆的片断 CGI / ISAPI 何为 CGI / ISAPI ? 一个小插曲 ASP与我的ASP Builder ASP编程技术 何为 Windows DNA &#xff1f; 什么是 COM ? ASP.NET 什么是 ActiveX ? IntraBuilder与我的InterBuilder 结尾 引子 凌晨三点醒了&#xff0c;大多的时候是…

【C】用c写贪吃蛇

1.输入正确的账号密码及其用户名&#xff0c;登录成功进入贪吃蛇游戏界面&#xff0c; 2.随机生成蛇头★、食物▲的位置(x,y)&#xff0c;并使用□打印地图 3.使用w s a d按键&#xff0c;完成蛇头的上下左右移动 4.蛇头碰撞到食物后&#xff0c;吃下食物变成蛇身的一部分●…

基于SSM+小程序民宿短租管理系统(民宿1)

&#x1f449;文末查看项目功能视频演示获取源码sql脚本视频导入教程视频 1、项目介绍 基于SSM小程序民宿短租管理系统实现了管理员、用户及房主 1、管理员可以管理民宿信息和订单信息用户管理、房主管理、房间类型管理、预定管理等。 2、房主可以管理自己的民宿和订单 3、…

Oracle 第3章:Oracle数据库体系结构

在讨论 Oracle 数据库的体系结构时&#xff0c;我们主要关注两个方面&#xff1a;实例&#xff08;Instance&#xff09;和数据库&#xff08;Database&#xff09;。这两个概念是理解 Oracle 数据库工作原理的基础。 实例与数据库文件 实例 是运行在内存中并由一组后台进程组…

一个和v-html功能一样且能防止xss攻击的插件

一个和v-html功能一样且能防止xss攻击的插件 vue-dompurify-html 牛皮plus 在页面上显示富文本内容 在vue中可以用v-html来实现 但是v-html有一个弊端&#xff1a;无法防止xss攻击 现在和大家分享一个插件&#xff1a; 既能实现v-html的功能&#xff0c;又能防止xss攻击&#…