主要用到的xmlns:th 和 “vue3-print-nb”: “^0.1.4”,
实现思路
1.前端画出需要打印表格的页面,可以用纯原生css、html
例如:
<body>
<div style="font-size: 12px;color:#74798C;">
<section style="margin-top: 10px;">
<table style="width: 100%;border: 1px solid black;border-collapse:collapse;">
<tbody>
<tr>
<td style="border:1px solid black;text-align:center;width:5%;color:black;font-weight: bold; line-height: 30px;font-size: 16px;"
colspan="8">22222222222222222222222222222</td>
</td>
</tr>
<tr>
<td style="border:1px solid black;text-align:center;width:5%;color:#74798C;writing-mode:vertical-lr;letter-spacing: 18px;"
rowspan="8">11111111</td>
<td style="border:1px solid black;width:10%;text-align: center;color:#74798C;height: 40px;">
22222</td>
<td style="border:1px solid black;width:15%;text-align: center;color:#74798C;">
<span th:text="${realname}"></span>
</td>
<td style="border:1px solid black;width:10%;text-align: center;color:#74798C;">
33333</td>
<td style="border:1px solid black;width:15%;text-align: center;color:#74798C;" colspan="6">
<span th:text="${applyDate}"></span>
</td>
</tr>
<tr style="height: 60px">
<td style="border:1px solid black;text-align: center;color:#74798C;">11111</td>
<td style="border:1px solid black;text-align: center;color:#74798C;">
<span th:text="${name}"></span>
</td>
<td style="border:1px solid black;text-align: center;color:#74798C;">22222222</td>
<td style="border:1px solid black;text-align: center;color:#74798C;" colspan="6">
<span th:text="${budget}"></span>
</td>
</tr>
<tr style="height: 60px">
<td style="border:1px solid black;text-align: center;color:#74798C;">333333</td>
<td style="border:1px solid black;text-align: left;color:#74798C; " colspan="8">
<div style="margin-left: 20px;">
<input type="checkbox" th:checked="${beckonpickStyle1}"
style="margin: 0px;line-height: 40px;">
<span style="line-height: 40px; display: inline-block;margin-right: 14px;">11111</span>
<input type="checkbox" th:checked="${beckonpickStyle2}"
style="margin: 0px;line-height: 40px;">
<span style="line-height: 40px; display: inline-block;margin-right: 14px;">11111</span>
<input type="checkbox" th:checked="${beckonpickStyle3}"
style="margin: 0px;line-height: 40px;">
<span style="line-height: 40px; display: inline-block;margin-right: 14px;">222222</span>
<input type="checkbox" th:checked="${beckonpickStyle4}"
style="margin: 0px;line-height: 40px;">
<span style="line-height: 40px; display: inline-block;margin-right: 14px;">3333333</span>
</div>
</td>
</tr>
<tr style="height: 60px">
<td style="border:1px solid black;text-align: center;color:#74798C;">111111</td>
<td style="border:1px solid black;text-align: center;color:#74798C;">
<span th:text="${deadline}"></span>
</td>
<td style="border:1px solid black;text-align: center;color:#74798C;">111111</td>
<td style="border:1px solid black;text-align: center;color:#74798C;" colspan="6">
<span th:text="${handleName}"></span>
</td>
</tr>
<tr style="height: 60px">
<td style="border:1px solid black;text-align: center;color:#74798C;">2222222</td>
<td style="border:1px solid black;text-align: left;color:#74798C;" colspan="8">
<!-- <span th:text="${idCard}"></span> -->
<div style="margin-left: 20px;">
<input type="checkbox" th:checked="${beckonpickMoney1}"
style="margin: 0px;line-height: 40px;">
<span style="line-height: 40px; display: inline-block;margin-right: 14px;">1111111</span>
<input type="checkbox" th:checked="${beckonpickMoney2}"
style="margin: 0px;line-height: 40px;">
<span style="line-height: 40px; display: inline-block;margin-right: 14px;">11111111</span>
</div>
</td>
</tr>
<tr style="height: 60px">
<td style="border:1px solid black;text-align: center;color:#74798C;">22222222</td>
<td style="border:1px solid black;text-align: left;color:#74798C;" colspan="8">
<div style="margin-left: 20px;">
<input type="checkbox" th:checked="${beckonpickBuy1}"
style="margin: 0px;line-height: 40px;">
<span style="line-height: 40px; display: inline-block;margin-right: 14px;">1111111</span>
<input type="checkbox" th:checked="${beckonpickBuy2}"
style="margin: 0px;line-height: 40px;">
<span style="line-height: 40px; display: inline-block;margin-right: 14px;">22222222</span>
<input type="checkbox" th:checked="${beckonpickBuy3}"
style="margin: 0px;line-height: 40px;">
<span style="line-height: 40px; display: inline-block;margin-right: 14px;">33333333</span>
</div>
</td>
</tr>
<tr style="height: 60px">
<td style="border:1px solid black;text-align: center;color:#74798C;">1111111</td>
<td style="border:1px solid black;text-align: left;color:#74798C;" colspan="8">
<div style="margin-left: 20px;">
<input type="checkbox" th:checked="${beckonpickBuytype1}"
style="margin: 0px;line-height: 40px;">
<span
style="line-height: 40px; display: inline-block;margin-right: 4px;margin-right: 4px;">2222222</span>
<input type="checkbox" th:checked="${beckonpickBuytype2}"
style="margin: 0px;line-height: 40px;">
<span style="line-height: 40px; display: inline-block;margin-right: 14px;">33333333</span>
<input type="checkbox" th:checked="${beckonpickBuytype3}"
style="margin: 0px;line-height: 40px;">
<span style="line-height: 40px; display: inline-block;margin-right: 14px;">11111111</span>
<input type="checkbox" th:checked="${beckonpickBuytype4}"
style="margin: 0px;line-height: 40px;">
<span style="line-height: 40px; display: inline-block;margin-right: 14px;">11111111</span>
<input type="checkbox" th:checked="${beckonpickBuytype5}"
style="margin: 0px;line-height: 40px;">
<span style="line-height: 40px; display: inline-block;margin-right: 14px;">1111111</span>
<input type="checkbox" th:checked="${beckonpickBuytype6}"
style="margin: 0px;line-height: 40px;">
<span style="line-height: 40px; display: inline-block;margin-right: 14px;">22222222</span>
<input type="checkbox" th:checked="${beckonpickBuytype7}"
style="margin: 0px;line-height: 40px;">
<span style="line-height: 40px; display: inline-block;margin-right: 14px;">333333333</span>
<input type="checkbox" th:checked="${beckonpickBuytype8}"
style="margin: 0px;line-height: 40px;">
<span style="line-height: 40px; display: inline-block;margin-right: 14px;">444444444</span>
<input type="checkbox" th:checked="${beckonpickBuytype9}"
style="margin: 0px;line-height: 40px;">
<span style="line-height: 40px; display: inline-block;margin-right: 14px;">11111111</span>
<input type="checkbox" th:checked="${beckonpickBuytype10}"
style="margin: 0px;line-height: 40px;">
<span style="line-height: 40px; display: inline-block;margin-right: 14px;">22222222</span>
<input type="checkbox" th:checked="${beckonpickBuytype11}"
style="margin: 0px;line-height: 40px;">
<span style="line-height: 40px; display: inline-block;margin-right: 14px;">3333</span>
<input type="checkbox" th:checked="${beckonpickBuytype12}"
style="margin: 0px;line-height: 40px;">
<span style="line-height: 40px; display: inline-block;margin-right: 14px;">11111</span>
</div>
</td>
</tr>
<tr style="height: 60px">
<td style="border:1px solid black;text-align: center;color:#74798C;">1111111</td>
<td style="border:1px solid black;text-align: left;color:#74798C;" colspan="8">
<div style="margin-left: 20px;">
<input type="checkbox" th:checked="${beckonpickEffect1}"
style="margin: 0px;line-height: 40px;">
<span
style="line-height: 40px; display: inline-block;margin-right: 4px;margin-right: 4px;">333333</span>
</div>
<div style="margin-left: 20px;">
<input type="checkbox" th:checked="${beckonpickEffect2}"
style="margin: 0px;line-height: 40px;">
<span
style="line-height: 40px; display: inline-block;margin-right: 4px;margin-right: 4px;">3333333</span>
</div>
<div style="margin-left: 20px;">
<input type="checkbox" th:checked="${beckonpickEffect3}"
style="margin: 0px;line-height: 40px;">
<span
style="line-height: 40px; display: inline-block;margin-right: 4px;margin-right: 4px;">1111111</span>
</div>
</td>
</tr>
<tr style="height: 60px">
<td style="border:1px solid black;text-align:center;width:50px;color:#74798C;" colspan="2">11111
</td>
<td style="border:1px solid black;text-align: center;color:#74798C;" colspan="8">
<span th:text="${idCard}"></span>
</td>
<tr style="height: 60px">
<td style="border:1px solid black;text-align:center;width:50px;color:#74798C;" colspan="2">
222222</td>
<td style="border:1px solid black;text-align: center;color:#74798C;" colspan="8">
<span th:text="${idCard}"></span>
</td>
</tr>
<tr style="height: 60px">
<td style="border:1px solid black;text-align:center;width:50px;color:#74798C;" colspan="2">
3333333</td>
<td style="border:1px solid black;text-align: center;color:#74798C;" colspan="8">
<span th:text="${idCard}"></span>
</td>
</tr>
<tr style="height: 60px">
<td style="border:1px solid black;text-align:center;width:50px;color:#74798C;" colspan="2">1111111
</td>
<td style="border:1px solid black;text-align: center;color:#74798C;" colspan="8">
<span th:text="${idCard}"></span>
</td>
</tr>
<tr style="height: 60px">
<td style="border:1px solid black;text-align:left;width:5%;color:#74798C; line-height: 10px;font-size: 10px;"
colspan="8">
<div style="margin-left: 20px;margin-top: 10px;">
说明:
</div>
<div style="margin-left: 20px;">
<p>1.111111111111111</p>
</div>
<div style="margin-left: 20px;">
<p> 2.11111111111111111111</p>
</div>
</td>
</td>
</tr>
</tbody>
</table>
</section>
</div>
</body>
代码效果如下
2.在vue3的页面用到弹框形式
<a-button type="primary" preIcon="ant-design:printer-outlined" @click="handlePrint(selectedRowKeys)">
selectedRowKeys:选择需要打印的数据(因为我这是选择表格的某一行打印,所以selectedRowKeys代表我选择的整行数据)
2.1
弹框
<a-modal style="width: 800px" :visible="showPrint" :footer="null" :closable="false">
<div id="printContent" v-html="htmlText"></div>
<button ref="printRef" v-print="printObj" style="display: none"> 打印 </button>
</a-modal>
2.2
方法
//打印
function handlePrint(arr) {
if (arr.length != 1) {
createMessage.warning('请选中一条数据进行打印!');
} else {
printTemplate({ id: arr[0] })
.then((res) => {
htmlText.value = res;
showPrint.value = true;
nextTick(() => {
showPrint.value = false;
printRef.value.click();
// printJS({
// printable: 'printContent',
// type: 'html',
// targetStyles: ['*'],
// maxWidth: 1000,
// // style: `.changesy{margin-top:40px}`,
// });
// printClick.value.click();
});
})
.catch((err) => {
console.log(err);
});
}
}
这个方法中 printTemplate({ id: arr[0] })
.then 这一部分是后台接口 这个接口主要用于告诉后台前端要打印表格哪一行的数据,并且让后台给 我们实现思路 1 中的模板赋值,就是下图红框值
结合 2.2方法中的 htmlText.value = res; 给我们弹框中的html赋值,(其实就是接口告诉后台打印的数据,后台将数据处理后 插入到我们给到后台的html模板中,然后我们前端在赋值到我们的html中 在打印)
最后打印!