Files
klp-oa/klp-ui/src/views/wms/print/pages/tt/LabelRender/index.vue

247 lines
6.8 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<div class="label-render-container">
<!-- 操作按钮 -->
<div class="action-buttons">
<el-button type="primary" @click="downloadLabelAsImage">下载标签图片</el-button>
<el-button type="primary" @click="printLabel" style="margin-left: 10px;">打印标签</el-button>
</div>
<!-- 标签预览容器 -->
<div class="preview-container" id="label-preview-container" ref="labelRef">
<ProductionTagPreview v-if="labelType === '2'" :content="content" />
<OuterTagPreview v-if="labelType === '3'" :content="content" />
<SampleTagPreview v-if="labelType === '4'" :content="content" />
<ForgeTagPreview v-if="labelType === '5'" :content="content" />
<SaltSprayTagPreview v-if="labelType === '6'" :content="content" />
</div>
</div>
</template>
<script>
import domToImage from 'dom-to-image';
import printJS from 'print-js';
import { Message } from 'element-ui'; // 若使用Element UI引入消息提示
import ProductionTagPreview from './ProductionTagPreview.vue';
import OuterTagPreview from './OuterTagPreview.vue';
import SampleTagPreview from './SampleTagPreview.vue';
import ForgeTagPreview from './ForgeTagPreview.vue';
import SaltSprayTagPreview from './SaltSprayTagPreview.vue';
export default {
name: 'LabelRender',
components: {
ProductionTagPreview,
OuterTagPreview,
SampleTagPreview,
ForgeTagPreview,
SaltSprayTagPreview,
},
props: {
labelType: {
type: String,
required: true,
},
content: {
type: Object,
required: true,
},
},
methods: {
// -------- 图片下载方法 --------
async downloadLabelAsImage() {
// 1. 获取要转换的DOM容器
const labelContainer = document.getElementById('label-preview-container');
if (!labelContainer) {
Message.error('未找到标签容器,无法下载');
return;
}
try {
// 2. 用dom-to-image生成PNG图片的DataURL
const dataUrl = await domToImage.toPng(labelContainer);
// 3. 创建临时a标签触发下载
const downloadLink = document.createElement('a');
downloadLink.href = dataUrl;
downloadLink.download = `标签_${new Date().getTime()}.png`; // 自定义文件名(带时间戳防重复)
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink); // 下载后移除临时标签
} catch (error) {
console.error('标签图片下载失败:', error);
Message.error('标签图片下载失败,请重试');
}
},
// 打印方法
printLabel() {
// 1. 获取标签容器DOM
const labelContainer = document.getElementById('label-preview-container');
if (!labelContainer) {
Message.error('未找到标签容器,无法打印');
return;
}
// 2. 获取标签原始宽高(用于保持尺寸)
const labelWidth = labelContainer.offsetWidth;
const labelHeight = labelContainer.offsetHeight;
// 3. 定义打印样式(确保与预览一致)
// const printStyles = `
// /* 基础容器样式 */
// #label-preview-container {
// width: ${labelWidth}px !important;
// height: ${labelHeight}px !important;
// padding: 1.5rem !important;
// box-sizing: border-box !important;
// border: 1px solid #333 !important;
// background-color: #fff !important;
// }
// .company-header {
// display: flex;
// align-items: center;
// margin-bottom: 0.3em;
// }
// .company-logo {
// width: 2.8em;
// height: 2.8em;
// margin-right: 0.5em;
// }
// .company-name {
// font-size: 0.95em;
// line-height: 1.1;
// }
// .english-name {
// font-size: 0.6em;
// opacity: 0.8;
// }
// .product-title {
// font-size: 1.4em;
// font-weight: bold;
// text-align: center;
// margin-bottom: 0.4em;
// border-bottom: 1.5px solid #000;
// padding-bottom: 0.2em;
// }
// .footer-info {
// display: flex;
// justify-content: space-between;
// align-items: flex-start;
// font-size: 0.7em;
// margin-top: 0.3em;
// }
// .address {
// line-height: 1.2;
// width: 65%;
// }
// .english-address {
// opacity: 0.8;
// font-size: 0.9em;
// }
// .contact-timestamp {
// text-align: right;
// line-height: 1.4;
// }
// /* 表格与单元格样式(解决空单元格问题) */
// table {
// border-collapse: collapse !important;
// width: 100% !important;
// table-layout: fixed !important; /* 固定列宽 */
// }
// td {
// border: 1px solid #000 !important;
// padding: 6px 4px !important; /* 确保单元格大小 */
// min-height: 28px !important; /* 空单元格最小高度 */
// vertical-align: middle !important;
// text-align: center !important;
// word-break: break-all !important; /* 长文本换行 */
// font-family: "SimSun", serif !important;
// font-size: 14px !important;
// }
// /* 标签与值单元格样式 */
// .label-cell {
// font-weight: bold !important;
// background-color: #f5f5f5 !important;
// }
// .value-cell {
// background-color: #fff !important;
// }
// /* 列宽分配(根据实际列数调整) */
// td { width: 25% !important; } /* 4列布局 */
// td[colspan="2"] { width: 50% !important; }
// td[colspan="3"] { width: 75% !important; }
// td[colspan="4"] { width: 100% !important; }
// `;
// 4. 调用print-js打印
printJS({
printable: 'label-preview-container', // 要打印的元素ID
type: 'html', // 打印类型为HTML
header: null, // 不显示页眉
footer: null, // 不显示页脚
// style: printStyles, // 注入打印样式
scanStyles: true, // 禁用自动扫描页面样式(避免冲突)
targetStyles: ['*'], // 允许所有样式生效
printContainer: true, // 打印指定容器
onError: (error) => { // 错误处理
console.error('打印失败:', error);
Message.error('打印失败,请重试');
}
});
}
},
};
</script>
<style scoped>
.label-render-container {
width: 100%;
height: calc(100vh - 200px);
display: flex;
flex-direction: column;
/* 按钮与预览垂直排列 */
justify-content: flex-start;
align-items: center;
padding: 2rem;
box-sizing: border-box;
}
.action-buttons {
margin-bottom: 1rem;
/* 按钮与预览区的间距 */
}
.preview-container {
background-color: #fff;
color: #000;
padding: 1.5rem;
width: fit-content;
min-width: 200px;
border-radius: 6px;
box-shadow: 0 0 60px rgba(255, 255, 255, 0.2);
border: 1px solid rgba(255, 255, 255, 0.1);
overflow: hidden;
transition: all 0.3s ease;
}
.preview-container:hover {
box-shadow: 0 0 80px rgba(255, 255, 255, 0.3);
transform: translateY(-2px);
}
</style>