7/24 收银台 日期筛选,合计

This commit is contained in:
Zy
2026-07-24 09:23:50 +08:00
parent 0c4c9676b5
commit 903c92fef0
8 changed files with 143 additions and 219 deletions

View File

@@ -275,228 +275,87 @@ export function printTicketDiv(elementId: string, title: string = '票据打印'
}
const win = window.open('', '_blank', 'width=1200,height=1000');
if (!win) return;
// ==============================================
// 【核心1】复制 全局样式 + Element Plus 样式
// ==============================================
const styles = Array.from(document.querySelectorAll('link[rel="stylesheet"], style:not([scoped])'))
.map((item) => item.outerHTML)
.join('');
// ==============================================
// 【核心2】手动注入打印区域的 scoped 样式(关键!)
// ==============================================
const styles = '';
const printScopeStyle = `
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
margin: 0;
padding: 0.3cm;
background: #fff;
font-size: 12px;
font-family: "Microsoft YaHei", sans-serif;
page-break-inside: avoid !important;
}
/* 隐藏后台布局 */
.el-container, .el-aside, .el-header, .el-footer {
display: none !important;
}
/* ========== 核心:强制 宽25cm 高10cm 无边距 ========== */
@page {
size: 25cm 10cm;
margin: 0;
}
/* 打印容器:固定尺寸,限制单页 */
#${elementId} {
width: 25cm !important;
height: 10cm !important;
margin: 0 auto;
overflow: hidden;
page-break-after: avoid !important;
page-break-inside: avoid !important;
}
/* ========== 表格:压缩高度 + 禁止拆分 ========== */
table {
border-collapse: collapse;
width: 100% !important;
table-layout: fixed;
margin: 0.1cm 0;
page-break-inside: avoid !important;
transform-origin: top left;
/* 初始不缩放,由JS动态控制 */
page-break-after: avoid !important;
}
table th, table td {
border: 1px solid #333;
font-size: 10px;
height:40px;
min-width:80px;
text-align: center;
padding: 1px 2px; /* 缩小内边距,减少高度 */
line-height: 1.1;
white-space: normal;
overflow: hidden;
}
</style>
`;
// ==============================================
// 【核心3】写入新窗口
// ==============================================
win.document.write(`
<!DOCTYPE html>
<html>
<head>
<title>${title}</title>
<meta charset="UTF-8">
${styles}
${printScopeStyle}
</head>
<body>
${el.outerHTML} <!-- 直接用innerHTML复制完整结构 -->
</body>
</html>
`);
win.document.close();
win.onload = () => {
const printContainer = win.document.getElementById(elementId);
if (!printContainer) {
win.close();
return;
}
setTimeout(() => {
const containerHeightPx = printContainer.clientHeight;
const contentHeightPx = printContainer.scrollHeight;
const containerWidthPx = printContainer.clientWidth;
const contentWidthPx = printContainer.scrollWidth;
// 固定顶部偏移量(约0.2cm),确保打印边缘不裁切内容╬D2╬╬2Σ╬╬╬╬2╬2222╬╬2╬22╬22╬22╬2╬╬2╬╬
const offsetPx = 20; // 可根据实际效果调整
// 计算可用高度(减去偏移)
const availableHeight = containerHeightPx - offsetPx;
// 如果内容高度超过可用高度,或宽度超过容器宽度,则等比缩放
let scale = 1;
let needScale = false;
if (contentHeightPx > availableHeight) {
scale = availableHeight / contentHeightPx;
needScale = true;
}
if (contentWidthPx > containerWidthPx) {
const scaleW = containerWidthPx / contentWidthPx;
if (scaleW < scale) {
scale = scaleW;
needScale = true;
}
}
// 应用 transform:先缩放(如果需要),再整体下移固定偏移
if (needScale) {
printContainer.style.transform = `translateY(${offsetPx}px) scale(${scale})`;
} else {
// 即使不缩放,也下移避免顶部贴边
printContainer.style.transform = `translateY(${offsetPx}px)`;
}
// 确保缩放原点在左上,保证缩放后位置正确
printContainer.style.transformOrigin = 'top left';
// 执行打印
win.print();
win.close();
callback && callback();
}, 300); // 等待渲染完成
};
}
export function printTicketDiv2(elementId: string, title: string = '票据打印', callback?: () => void) {
const el = document.getElementById(elementId);
if (!el) {
console.error('未找到打印元素');
return;
}
const win = window.open('', '_blank', 'width=1200,height=1000');
if (!win) return;
// 复制全局非scoped样式(Element Plus、全局公共样式)
const styles = Array.from(document.querySelectorAll('link[rel="stylesheet"], style:not([scoped])'))
.map((item) => item.outerHTML)
.join('');
// 专属打印样式,根治空白第二页
const printScopeStyle = `
<style>
<style>
* {
box-sizing: border-box;
margin: 0 !important;
padding: 0 !important;
}
html, body {
height: auto !important;
min-height: auto !important;
margin: 0 !important;
padding: 0 !important;
width: 100%;
height: 100%;
background: #fff;
font-size: 12px;
font-family: "Microsoft YaHei", sans-serif;
page-break-inside: avoid !important;
}
/* 隐藏弹窗无关组件:输入框、按钮、弹窗头部 */
.el-input {
/* 隐藏后台布局 */
.el-container, .el-aside, .el-header, .el-footer {
display: none !important;
}
/* 纸张自适应宽高,无边距 */
/* ✅ @page 提升到外层,不要嵌套在 @media print 内部!修复不生效BUG */
@page {
size: auto auto;
margin: 0mm !important;
size: 25cm 10cm !important;
margin: 0 !important;
}
/* 打印容器核心禁止分页 */
/* 自定义纸张 25cm × 10cm 无边距 */
@media print {
html {
height: 10cm !important;
overflow: hidden !important;
}
body {
/* 强制内容左上角对齐,禁止浏览器自动居中 */
transform-origin: top left !important;
position: absolute !important;
left: 0 !important;
top: 0 !important;
height: 10cm !important;
overflow: hidden !important;
}
/* =========票据容器:贴左上角,不再居中========= */
#${elementId} {
margin: 0 auto !important;
overflow: visible;
width: 25cm !important;
height: 10cm !important;
box-sizing: border-box;
page-break-inside: avoid !important;
page-break-before: avoid !important;
page-break-after: avoid !important;
break-inside: avoid !important;
break-after: avoid !important;
break-inside: avoid;
break-before: avoid;
break-after: avoid;
padding: 8mm; /* 内部留白,模拟页边距 */
}
.title{
font-weight: 800;
}
/* 表格禁止截断、压缩高度 */
table {
border-collapse: collapse;
width: 100% !important;
table-layout: fixed;
margin: 0.1cm 0;
page-break-inside: avoid !important;
break-inside: avoid !important;
break-inside: avoid;
table-layout: auto;
}
table th, table td {
border: 1px solid #333;
font-size: 10px;
height:32px; /* 缩小行高减少整体高度 */
min-width:80px;
height:40px;
text-align: center;
padding: 1px 2px;
line-height: 1.1;
white-space: normal;
overflow: hidden;
page-break-inside: avoid !important;
}
}
</style>
`;
// 只复制票据内部innerHTML,不带外层el-dialog多余结构
const printContent = el.innerHTML;
win.document.write(`
const html = `
<!DOCTYPE html>
<html>
<head>
@@ -506,10 +365,12 @@ export function printTicketDiv2(elementId: string, title: string = '票据打印
${printScopeStyle}
</head>
<body>
<div id="${elementId}">${printContent}</div>
${el.outerHTML}
</body>
</html>
`);
`;
win.document.write(html);
win.document.close();
win.onload = () => {
setTimeout(() => {