安装两个插件:一个是生成二维码的,一个是打印的插件
npm install qrcodejs2 --save;
npm install vue-print-nb;
Vue全局引用打印插件,方便使用。:
import Print from 'vue-print-nb'
Vue.use(Print)
vue页面需要引入生成二维码插件:
import QRCode from 'qrcodejs2'
Vue页面的template代码:
<div>
<div id="printTest">
<div id="SetQRCode" style="page-break-after:always"></div>
<div id="SetQRCode2" style="page-break-after:always"></div>
<div id="SetQRCode3" style="page-break-after:always"></div>
</div> <button v-print="'#printTest'">打印</button>
</div>
js代码:
import QRCode from 'qrcodejs2'
/* vue生命周期函数*/
mounted() {
this.$nextTick(function () {
this.testQRCode();
})
},
methods: {
/* documentName绑定的是template中div的id,size是二维码的大小,qrText是扫描出来的文字,obj是需要传递的对象 */
canvasEWM(documentName, size, qrText, obj) {
// let qrcodeDiv = document.getElementById(documentName)
let qrcode = new QRCode(document.getElementById(documentName), {
text: qrText,
width: size,
height: size,
colorDark: "#000",
//容错率,L/M/H L最高
correctLevel: QRCode.CorrectLevel.L
});
var canvas = qrcode._el.firstChild;
var ctx = canvas.getContext("2d");
// 字体 粗体 格式
const fontWeight = 'bold'
// 字体大小
const fontSize = 14
// 底部文字上下间距
const tb = 5
/* 右侧扩容宽度 */
const addWidth = 200;
// 文字行高
const wenzilieheight = fontSize + 2 * tb;
//实际高度 二维码的高度 200 +标签高度+ERP码的高度
const realHeight = size + (fontSize + 2 * tb) * 2
const realWidth = size + fontSize + 2 * tb + addWidth //实际宽度
/* =============================设置二维码数据 =============================*/
// 获取二维码数据
const data = ctx.getImageData(0, 0, size, size);
ctx.fillStyle = "#fff"
canvas.setAttribute('height', realHeight); // 重设画布像素高度
canvas.style.setProperty('height', realHeight + 'px'); // 重设画布实际高度
canvas.setAttribute('width', realWidth); // 重设画布像素宽度
canvas.style.setProperty('width', realWidth + 'px'); // 重设画布实际宽度
ctx.fillRect(wenzilieheight, wenzilieheight, size, size)
ctx.putImageData(data, wenzilieheight, wenzilieheight)// 填充二维码数据
ctx.font = `${fontWeight} ${fontSize}px Arial`;
//文字真实宽度
const textWidth = ctx.measureText("山东冶金科学院物料标签").width;
/* =============================设置标签 =============================*/
// 设置底部背景色
ctx.fillStyle = "#fff";
/* 设置底部填充位置 */
ctx.fillRect(0, 0, realWidth, wenzilieheight);
ctx.textBaseline = "top"; //设置绘制文本时的文本基线。
ctx.fillStyle = "#333";// 字体颜色
ctx.fillText(qrText, (size - textWidth) / 2, 0)
/* =============================流水码 =============================*/
// 根据字体大小计算文字left
const fleft = (size - textWidth) / 2;
const ftop = size + wenzilieheight + tb; //文字距顶部位置
const textPadding = fontSize / 2; //字体边距为字体大小的一半可以自己设置
// 设置底部背景色
ctx.fillStyle = "#fff";
/* 设置底部填充位置 */
ctx.fillRect(wenzilieheight, size + wenzilieheight, size, realHeight - 2 * tb);
// 设置字体填充位置
ctx.fillRect(
fleft,
ftop,
textWidth + textPadding,
fontSize + textPadding
);
ctx.textBaseline = "top"; //设置绘制文本时的文本基线。
ctx.fillStyle = "#333";// 字体颜色
ctx.fillText("ERP流水码", fleft, ftop)
/* =============================设置右侧数据============================= */
// 设置底部背景色
ctx.fillStyle = "#fff";
/* 设置右侧填充位置 */
let rightX = size + wenzilieheight * 2;
let rightY = 40;
/*========名称小件======= */
ctx.fillRect(rightX, rightY, size, realHeight - 2 * tb);
// 设置字体填充位置
ctx.fillRect(
rightX,
rightY,
textWidth + textPadding,
fontSize + textPadding
);
ctx.textBaseline = "top"; //设置绘制文本时的文本基线。
ctx.fillStyle = "#333";// 字体颜色
ctx.fillText("名称:" + obj.materialName, rightX, rightY)
/*========规格小件 绘制小件的时候每次都向下移动40个======= */
// 设置底部背景色
ctx.fillStyle = "#fff";
rightY += 40
ctx.fillRect(rightX, rightY, size, realHeight - 2 * tb);
// 设置字体填充位置
ctx.fillRect(
rightX,
rightY,
textWidth + textPadding,
fontSize + textPadding
);
ctx.textBaseline = "top"; //设置绘制文本时的文本基线。
ctx.fillStyle = "#333";// 字体颜色
ctx.fillText("规格:" + obj.specification, rightX, rightY)
/*========成分小件======= */
// 设置底部背景色
ctx.fillStyle = "#fff";
rightY += 40
ctx.fillRect(rightX, rightY, size, realHeight - 2 * tb);
// 设置字体填充位置
ctx.fillRect(
rightX,
rightY,
textWidth + textPadding,
fontSize + textPadding
);
ctx.textBaseline = "top"; //设置绘制文本时的文本基线。
ctx.fillStyle = "#333";// 字体颜色
ctx.fillText("成分:" + obj.composition, rightX, rightY)
/*========国别小件======= */
// 设置底部背景色
ctx.fillStyle = "#fff";
rightY += 40
ctx.fillRect(rightX, rightY, size, realHeight - 2 * tb);
// 设置字体填充位置
ctx.fillRect(
rightX,
rightY,
textWidth + textPadding,
fontSize + textPadding
);
ctx.textBaseline = "top"; //设置绘制文本时的文本基线。
ctx.fillStyle = "#333";// 字体颜色
ctx.fillText("国别:" + obj.country, rightX, rightY)
/*========状态小件======= */
// 设置底部背景色
ctx.fillStyle = "#fff";
rightY += 40
ctx.fillRect(rightX, rightY, size, realHeight - 2 * tb);
// 设置字体填充位置
ctx.fillRect(
rightX,
rightY,
textWidth + textPadding,
fontSize + textPadding
);
ctx.textBaseline = "top"; //设置绘制文本时的文本基线。
ctx.fillStyle = "#333";// 字体颜色
ctx.fillText("状态:" + obj.status, rightX, rightY)
},
testQRCode() {
this.canvasEWM("SetQRCode", 200, "物料标签1", { composition: "123", materialName: "123", specification: "15*12*13", country: "中国", "status": "正常" });
this.canvasEWM("SetQRCode2", 200, "物料标签12", { composition: "456", materialName: "456", specification: "15*12*13", country: "美国", "status": "正常" });
this.canvasEWM("SetQRCode3", 200, "物料标签3", { composition: "789", materialName: "789", specification: "15*12*13", country: "澳大利亚", "status": "不正常" });
},
}