vue实现标签打印
实现标签打印的Vue方案
在Vue中实现标签打印功能,通常需要结合浏览器打印API和CSS打印样式控制。以下是几种常见实现方式:
使用window.print()基础打印
创建打印专用组件,通过CSS控制打印样式:

<template>
<div>
<button @click="print">打印标签</button>
<div class="print-content" ref="printContent">
<!-- 标签内容 -->
<div class="label">产品标签示例</div>
</div>
</div>
</template>
<script>
export default {
methods: {
print() {
const printWindow = window.open('', '_blank');
printWindow.document.write(this.$refs.printContent.innerHTML);
printWindow.document.close();
printWindow.focus();
printWindow.print();
printWindow.close();
}
}
}
</script>
<style>
@media print {
body * {
visibility: hidden;
}
.print-content, .print-content * {
visibility: visible;
}
.print-content {
position: absolute;
left: 0;
top: 0;
}
}
</style>
使用html2canvas+jsPDF实现PDF打印
对于需要精确控制打印布局的场景:
<template>
<button @click="exportToPDF">导出PDF</button>
<div id="labelContent">
<!-- 标签内容 -->
</div>
</template>
<script>
import html2canvas from 'html2canvas';
import jsPDF from 'jspdf';
export default {
methods: {
async exportToPDF() {
const element = document.getElementById('labelContent');
const canvas = await html2canvas(element, {
scale: 2,
logging: false,
useCORS: true
});
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({
orientation: 'portrait',
unit: 'mm'
});
const imgWidth = 50; // 标签宽度(mm)
const imgHeight = 30; // 标签高度(mm)
pdf.addImage(imgData, 'PNG', 10, 10, imgWidth, imgHeight);
pdf.save('label.pdf');
}
}
}
</script>
使用专用打印插件
对于专业标签打印需求,可考虑专用库:

npm install vue-thermal-print
<template>
<button @click="printLabel">打印标签</button>
</template>
<script>
import { ThermalPrinter } from 'vue-thermal-print';
export default {
methods: {
printLabel() {
const printer = new ThermalPrinter({
type: 'epson',
interface: 'USB',
width: 80 // 标签宽度(mm)
});
printer.align('CT');
printer.println('产品标签');
printer.cut();
printer.execute();
}
}
}
</script>
批量标签打印实现
对于需要打印多个标签的场景:
<template>
<div>
<button @click="printBatch">批量打印</button>
<div class="labels-container" ref="labels">
<div class="label" v-for="(item, index) in labels" :key="index">
{{ item.name }}
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
labels: [{name: '标签1'}, {name: '标签2'}] // 标签数据
}
},
methods: {
printBatch() {
const printContent = this.$refs.labels.innerHTML;
const printWindow = window.open('', '_blank');
printWindow.document.write(`
<!DOCTYPE html>
<html>
<head>
<style>
@page {
size: A4;
margin: 0;
}
body {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10mm;
padding: 10mm;
}
.label {
border: 1px dashed #ccc;
padding: 5mm;
height: 30mm;
}
</style>
</head>
<body>${printContent}</body>
</html>
`);
printWindow.document.close();
printWindow.print();
}
}
}
</script>
打印样式优化建议
针对标签打印的特殊需求,CSS媒体查询可以优化打印效果:
@media print {
@page {
size: 50mm 30mm; /* 自定义标签尺寸 */
margin: 0;
}
body {
margin: 0;
padding: 0;
-webkit-print-color-adjust: exact;
}
.no-print {
display: none !important;
}
.page-break {
page-break-after: always;
}
}
以上方案可根据实际需求选择或组合使用,对于工业级标签打印,建议使用专业打印插件或与后端打印服务集成。






