更正前端内容

This commit is contained in:
2026-01-09 14:45:22 +08:00
parent 04b46d8fee
commit 732646096f

View File

@@ -27,7 +27,7 @@
<script> <script>
import domToImage from 'dom-to-image'; import domToImage from 'dom-to-image';
import printJS from 'print-js'; // import printJS from 'print-js'; // 改为自建 iframe 打印,避免多余空白页
import html2canvas from 'html2canvas'; // 新增:引入高清渲染库 import html2canvas from 'html2canvas'; // 新增:引入高清渲染库
import { Message } from 'element-ui'; import { Message } from 'element-ui';
@@ -157,93 +157,74 @@ export default {
finalCanvas = scaledCanvas; finalCanvas = scaledCanvas;
} }
// 6. 创建临时打印容器避免影响原DOM // 6. 生成 DataURL使用 printJS 的 image 模式(单图),并减少额外样式以避免隐藏内容
const printContainerId = 'temp-print-container-' + new Date().getTime(); const dataUrl = finalCanvas.toDataURL('image/png');
const tempPrintContainer = document.createElement('div');
tempPrintContainer.id = printContainerId;
tempPrintContainer.style.cssText = 'position: absolute; left: -9999px; top: -9999px;';
// 将Canvas插入临时容器
finalCanvas.style.cssText = `width: ${paperWidthMm}mm; height: ${paperHeightMm}mm; max-width: 100%; max-height: 100%; object-fit: contain;`;
tempPrintContainer.appendChild(finalCanvas);
document.body.appendChild(tempPrintContainer);
// 7. 调用printJS打印高清Canvas而非原HTML // 6. 创建隐藏 iframe 进行打印,避免 printJS 的分页行为
printJS({ const iframe = document.createElement('iframe');
printable: tempPrintContainer, // 打印临时容器 iframe.style.position = 'fixed';
type: 'html', iframe.style.right = '0';
header: null, iframe.style.bottom = '0';
footer: null, iframe.style.width = '0';
scanStyles: false, // 禁用自动扫描样式,手动控制 iframe.style.height = '0';
// 自定义打印样式确保Canvas适配纸张、无多余边距、强制单页 iframe.style.border = '0';
style: ` document.body.appendChild(iframe);
@page {
size: ${paperWidthMm}mm ${paperHeightMm}mm; const doc = iframe.contentWindow.document;
margin: 2mm !important; /* 留出页边距,避免紧贴边框 */ doc.open();
padding: 0 !important; doc.write(`
} <!doctype html>
* { <html>
-webkit-print-color-adjust: exact !important; <head>
print-color-adjust: exact !important; <style>
} @page {
html, body { size: ${paperWidthMm}mm ${paperHeightMm}mm;
margin: 0 !important; margin: 2mm;
padding: 0 !important; }
width: ${paperWidthMm}mm !important; * {
height: ${paperHeightMm}mm !important; -webkit-print-color-adjust: exact;
overflow: hidden !important; print-color-adjust: exact;
} box-sizing: border-box;
#${printContainerId} { }
width: calc(${paperWidthMm}mm - 4mm) !important; /* 两侧各2mm的页边距后可用宽度 */ html, body {
height: calc(${paperHeightMm}mm - 4mm) !important; /* 上下各2mm的页边距后可用高度 */ margin: 0;
margin: 0 !important; padding: 0;
padding: 0 !important; width: 100%;
display: flex !important; height: 100%;
align-items: center !important; overflow: hidden;
justify-content: center !important; }
} body {
#${printContainerId} canvas { display: flex;
width: 100% !important; align-items: center;
height: 100% !important; justify-content: center;
max-width: calc(${paperWidthMm}mm - 4mm) !important; }
max-height: calc(${paperHeightMm}mm - 4mm) !important; img {
object-fit: contain !important; width: calc(100% - 4mm);
border: none !important; height: calc(100% - 4mm);
} object-fit: contain;
@media print { display: block;
body * { visibility: hidden !important; } page-break-after: avoid;
#${printContainerId}, #${printContainerId} * { visibility: visible !important; } page-break-before: avoid;
#${printContainerId} { page-break-inside: avoid;
position: absolute !important; }
top: 0 !important; </style>
left: 0 !important; </head>
page-break-inside: avoid !important; <body>
break-inside: avoid !important; <img src="${dataUrl}" />
page-break-after: avoid !important; </body>
break-after: avoid !important; </html>
page-break-before: avoid !important; `);
break-before: avoid !important; doc.close();
orphans: 999 !important;
widows: 999 !important; iframe.onload = () => {
} setTimeout(() => {
} iframe.contentWindow.focus();
`, iframe.contentWindow.print();
printContainer: true, setTimeout(() => {
onAfterPrint: () => { document.body.removeChild(iframe);
// 8. 打印完成后清理临时容器 }, 1000);
if (document.body.contains(tempPrintContainer)) { }, 300);
document.body.removeChild(tempPrintContainer); };
}
Message.success('打印完成');
},
onError: (error) => {
// 异常时也清理临时容器
if (document.body.contains(tempPrintContainer)) {
document.body.removeChild(tempPrintContainer);
}
console.error('打印失败:', error);
Message.error('打印失败,请重试');
}
});
} catch (error) { } catch (error) {
console.error('打印准备失败:', error); console.error('打印准备失败:', error);
Message.error('打印内容准备失败,请重试'); Message.error('打印内容准备失败,请重试');