<template>
|
<div>
|
<vol-box
|
v-model="showDetialBox"
|
:lazy="true"
|
width="300px"
|
:padding="15"
|
title="打印"
|
>
|
<div
|
id="printContent"
|
style="display: flex; justify-content: center; align-items: center"
|
>
|
<div
|
style="display: flex; justify-content: center; align-items: center"
|
>
|
<!-- 使用 SVG 来显示条形码 -->
|
<svg id="barcode" ref="barcode"></svg>
|
</div>
|
</div>
|
<div id="palletcode">
|
<span
|
style="display: flex; justify-content: center; align-items: center"
|
>{{ palletCode }}</span
|
>
|
</div>
|
|
<template #footer>
|
<el-button type="primary" size="small" @click="print()">打印</el-button>
|
<el-button type="danger" size="small" @click="showDetialBox = false"
|
>关闭</el-button
|
>
|
</template>
|
</vol-box>
|
</div>
|
</template>
|
|
<script>
|
import VolBox from "@/components/basic/VolBox.vue";
|
import JsBarcode from "jsbarcode";
|
|
export default {
|
components: { VolBox },
|
data() {
|
return {
|
showDetialBox: false,
|
row: null,
|
palletCode: "",
|
};
|
},
|
methods: {
|
open(row) {
|
this.row = row;
|
this.showDetialBox = true;
|
if (row && row.palletCode) {
|
this.palletCode = row.palletCode;
|
// 下次 DOM 更新后生成条形码
|
this.$nextTick(() => {
|
this.generateBarcode();
|
});
|
}
|
},
|
|
// 生成条形码
|
generateBarcode() {
|
if (this.palletCode && this.$refs.barcode) {
|
try {
|
JsBarcode(this.$refs.barcode, this.palletCode, {
|
format: "CODE128", // 条形码格式
|
width: 4, // 条宽度
|
height: 100, // 条高度
|
displayValue: true, // 是否显示数值
|
text: this.palletCode, // 显示的文本
|
fontOptions: "bold", // 字体样式
|
fontSize: 16, // 字体大小
|
background: "#ffffff", // 背景色
|
lineColor: "#000000", // 线条颜色
|
margin: 10 // 边距
|
});
|
} catch (error) {
|
console.error("生成条形码失败:", error);
|
}
|
}
|
},
|
|
print() {
|
let printContent = document.getElementById("printContent");
|
var printWindow = window.open("", "_blank");
|
|
// 精确计算内容高度,避免多余空白
|
printWindow.document.write(`
|
<!DOCTYPE html>
|
<html>
|
<head>
|
<title>打印条形码</title>
|
<style>
|
@page {
|
size: auto; /* 自动大小 */
|
margin: 5mm; /* 最小边距 */
|
}
|
@media print {
|
body {
|
margin: 0;
|
padding: 0;
|
height: 50mm; /* 精确控制高度 */
|
display: flex;
|
flex-direction: column;
|
align-items: center;
|
justify-content: center;
|
}
|
.print-container {
|
width: 100%;
|
height: 45mm; /* 略小于页面高度 */
|
display: flex;
|
flex-direction: column;
|
align-items: center;
|
justify-content: center;
|
}
|
#barcode {
|
max-height: 30mm;
|
width: auto;
|
}
|
#palletcode {
|
display: none;
|
}
|
}
|
</style>
|
</head>
|
<body>
|
<div class="print-container">
|
${printContent.innerHTML}
|
</div>
|
<script>
|
// 阻止默认的打印后行为
|
window.onafterprint = function() {
|
window.close();
|
};
|
<\/script>
|
</body>
|
</html>
|
`);
|
|
printWindow.document.close();
|
|
printWindow.onload = function() {
|
printWindow.focus();
|
printWindow.print();
|
};
|
|
this.updatePrintStatus();
|
}
|
},
|
watch: {
|
// 监听 palletCode 变化,重新生成条形码
|
palletCode(newVal) {
|
if (newVal) {
|
this.$nextTick(() => {
|
this.generateBarcode();
|
});
|
}
|
}
|
}
|
};
|
</script>
|
|
<style scoped>
|
.el-col {
|
border-radius: 4px;
|
}
|
|
.grid-content {
|
border-radius: 4px;
|
min-height: 36px;
|
}
|
|
.content-text {
|
display: flex;
|
align-items: center;
|
justify-content: center;
|
}
|
|
.right-text {
|
display: flex;
|
align-items: center;
|
justify-content: flex-end;
|
}
|
</style>
|
|
<style>
|
.el-table .warning-row {
|
background: #e6a23c;
|
}
|
|
.el-table .success-row {
|
background: #f0f9eb;
|
}
|
|
.el-table .error-row {
|
background: #f56c6c;
|
}
|
|
#barcode {
|
display: block;
|
margin: auto;
|
}
|
</style>
|