279 lines
7.3 KiB
Vue
279 lines
7.3 KiB
Vue
<template>
|
||
<Dialog
|
||
title="打印预览"
|
||
v-model="dialogVisible"
|
||
width="1000"
|
||
top="0%"
|
||
:before-close="(doClose) => beforeDialogClose(doClose)"
|
||
>
|
||
<el-button @click="outopen" style="float: right;margin-right: 0.5%;">取消</el-button>
|
||
<el-button @click="onPrint" type="primary" style="float: right;margin-right: 1%;">打印</el-button>
|
||
|
||
<!-- 打印预览 -->
|
||
<div class="print-wrap1 page" ref="print">
|
||
<div id="qrCodeContainer1"></div>
|
||
</div>
|
||
<template #footer>
|
||
<el-button @click="onPrint" type="primary">打印</el-button>
|
||
<el-button @click="outopen">取消</el-button>
|
||
</template>
|
||
</Dialog>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import QRCode from 'qrcode'
|
||
import {DICT_TYPE, getIntDictOptions} from "@/utils/dict";
|
||
|
||
const dialogVisible = ref(false)
|
||
let clauseId = null;
|
||
const startPageNum = ref(0);
|
||
const endPageNum = ref(0);
|
||
|
||
const onPrint = () => {
|
||
const printNode = document.querySelector('.print-wrap1')
|
||
if (!printNode) return
|
||
|
||
const newIframe: any = document.createElement('iframe')
|
||
newIframe.setAttribute(
|
||
'style',
|
||
'width:0px;height:0px;position:absolute;left:-9999px;top:-9999px;'
|
||
)
|
||
document.body.appendChild(newIframe)
|
||
|
||
const doc = newIframe.contentWindow.document
|
||
// 添加基础样式
|
||
doc.write(`
|
||
<!DOCTYPE html>
|
||
<html>
|
||
<head>
|
||
<style>
|
||
@page {
|
||
size: 60mm 40mm;
|
||
margin: 0;
|
||
}
|
||
body {
|
||
|
||
margin: 0;
|
||
padding: 0;
|
||
background-color: white;
|
||
color: black;
|
||
-webkit-print-color-adjust: exact;
|
||
print-color-adjust: exact;
|
||
}
|
||
.qr-page {
|
||
width: 60mm;
|
||
height: 40mm;
|
||
padding: 2mm;
|
||
box-sizing: border-box;
|
||
font-family: monospace;
|
||
position: relative;
|
||
background-color: white;
|
||
color: black;
|
||
}
|
||
.code-line {
|
||
font-size: 14px;
|
||
margin-bottom: 1.4mm;
|
||
margin-left: 0.4mm;
|
||
}
|
||
.code-line span {
|
||
display: inline-block;
|
||
width: 100%;
|
||
|
||
|
||
border-bottom: 1px solid black;
|
||
padding-bottom: 0.2mm;
|
||
font-weight: bold !important; /* 仅此处加粗 item.code */
|
||
color: black;
|
||
}
|
||
.name-line {
|
||
font-size: 14px;
|
||
margin-bottom: 1mm;
|
||
display: flex;
|
||
align-items: center;
|
||
}
|
||
.name-label {
|
||
white-space: nowrap;
|
||
margin-right: 1mm;
|
||
}
|
||
.name-underline {
|
||
flex-grow: 1;
|
||
border-bottom: 1px solid black;
|
||
height: 1em;
|
||
margin-left: -1mm;
|
||
}
|
||
.bottom-section {
|
||
display: flex;
|
||
position: absolute;
|
||
bottom: 2mm;
|
||
left: 2mm;
|
||
right: 2mm;
|
||
height: 22mm;
|
||
}
|
||
.qr-container {
|
||
width: 22mm;
|
||
height: 22mm;
|
||
margin-left: auto;
|
||
display: flex;
|
||
align-items: flex-end;
|
||
justify-content: flex-end;
|
||
}
|
||
.qr-code {
|
||
width: 100%;
|
||
height: 100%;
|
||
}
|
||
.quantity-container {
|
||
display: flex;
|
||
flex-direction: column;
|
||
justify-content: center;
|
||
align-items: flex-start;
|
||
width: 13mm;
|
||
margin-right: 1mm;
|
||
}
|
||
.spec {
|
||
min-height: calc(14px * 1.2 * 2);
|
||
display: -webkit-box;
|
||
-webkit-line-clamp: 2;
|
||
-webkit-box-orient: vertical;
|
||
overflow: hidden;
|
||
}
|
||
.rg, .pg {
|
||
white-space: nowrap;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
}
|
||
.quantity {
|
||
width: 22mm;
|
||
white-space: pre-wrap;
|
||
word-break: break-all;
|
||
overflow-wrap: anywhere;
|
||
text-indent: -13mm;
|
||
padding-left: 13mm;
|
||
font-size: 14px;
|
||
line-height: 1.2;
|
||
}
|
||
.timestamp {
|
||
font-size: 12px;
|
||
line-height: 1;
|
||
margin-left: 0.5mm;
|
||
margin-top: auto;
|
||
white-space: nowrap;
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
${printNode.innerHTML}
|
||
</body>
|
||
</html>
|
||
`)
|
||
doc.close()
|
||
dialogVisible.value = false
|
||
setTimeout(() => {
|
||
newIframe.contentWindow.focus()
|
||
newIframe.contentWindow.print()
|
||
document.body.removeChild(newIframe)
|
||
dialogVisible.value = false
|
||
}, 100)
|
||
}
|
||
|
||
function beforeDialogClose(doClose: (shouldClose: boolean) => void): void {
|
||
outopen()
|
||
}
|
||
|
||
const outopen = () => {
|
||
dialogVisible.value = false
|
||
}
|
||
|
||
const printCodeName = ref([])
|
||
const datavals = ref([])
|
||
const specarr = ref([])
|
||
const cnenList = ref([])
|
||
|
||
const open = async (vals) => {
|
||
cnenList.value = vals;
|
||
datavals.value = []
|
||
printCodeName.value = []
|
||
|
||
vals.forEach((item) => {
|
||
console.log(item)
|
||
const row1 = {
|
||
code: item.matCode,
|
||
name: item.matName == null ? '' : item.matName,
|
||
spec: item.matSpec == null ? '' : item.matSpec,
|
||
rg: item.rgName == null ? '' : item.rgName,
|
||
pg: item.pnName == null ? '' : item.pnName,
|
||
//amount: item.amount == null ? '' : item.amount
|
||
|
||
}
|
||
printCodeName.value.push(row1)
|
||
})
|
||
dialogVisible.value = true
|
||
|
||
await Promise.all(
|
||
printCodeName.value.map(async (item) => {
|
||
const formattedDate = `物料编码:${item.code},规格:${item.spec},简称:${item.name},库区:${item.rg},库位:${item.pg}`;
|
||
const qrCodeData = await QRCode.toDataURL(formattedDate ,{
|
||
errorCorrectionLevel: 'H'
|
||
})
|
||
const qrCodeElement = document.getElementById('qrCodeContainer1')
|
||
if (qrCodeElement) {
|
||
(qrCodeElement.innerHTML +=
|
||
`<div class="page qr-page">
|
||
<div class="code-line">
|
||
<span>物料编码: ${item.code}</span>
|
||
</div>
|
||
<div class="name-line">
|
||
<span class="name-label">简 称: </span>
|
||
<span class="name-underline">${item.name}</span>
|
||
</div>
|
||
<div class="bottom-section">
|
||
<div class="quantity-container">
|
||
<span class="quantity spec">规 格: ${item.spec}</span>
|
||
<span class="quantity rg">库 区: ${item.rg}</span>
|
||
<span class="quantity pg">库 位: ${item.pg}</span>
|
||
</div>
|
||
<div class="qr-container">
|
||
<img class="qr-code" src="${qrCodeData}" alt="QR Code">
|
||
</div>
|
||
</div>
|
||
</div>`)
|
||
}
|
||
})
|
||
).then(res => {
|
||
onPrint()
|
||
})
|
||
}
|
||
|
||
defineExpose({ open })
|
||
</script>
|
||
|
||
<style scoped lang="scss">
|
||
/* 预览样式 - 仅用于屏幕显示 */
|
||
.page {
|
||
width: 60mm;
|
||
min-height: 40mm;
|
||
margin: auto;
|
||
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
|
||
background-color: black;
|
||
color: white;
|
||
padding: 2mm;
|
||
box-sizing: border-box;
|
||
|
||
/* 临时隐藏下划线,打印时会显示 */
|
||
.code-line span, .name-underline {
|
||
display: none;
|
||
}
|
||
}
|
||
|
||
/* 确保打印样式优先 */
|
||
@media print {
|
||
.page {
|
||
background-color: black !important;
|
||
color: white !important;
|
||
|
||
.code-line span, .name-underline {
|
||
display: inline-block;
|
||
}
|
||
}
|
||
}
|
||
</style>
|