修复销售单据打印方向固定及跨页问题
打印遵循浏览器和驱动所选纸张与方向,将完整单据适配到一页,保留预览尺寸及套打偏移。新增打印时序和重复打印回归测试,并更新设置说明。 验证:251项测试通过;32种Chrome PDF组合均为单页,内容完整。
This commit is contained in:
1 parent
020249d34b
commit
b9154a752d
5 files changed
+120
-13
No files matched your search
+31
-5
@@ -1,8 +1,31 @@
|
||||
'use strict';
|
||||
(()=>{
|
||||
const button=document.getElementById('print'),frame=document.querySelector('.print-frame'),content=document.querySelector('.content'),status=document.getElementById('fit-status');
|
||||
// 先压缩空行与行距,再根据真实排版尺寸缩放;保留每个商品及附加项,不裁掉尾部。
|
||||
const canvas=document.querySelector('.print-canvas'),foreignObject=canvas.querySelector('foreignObject'),printMedia=window.matchMedia('print');
|
||||
let printing=false;
|
||||
// 打印副本保留原始文字与表格。SVG 根据最终纸张自动适配,不依赖 beforeprint 的屏幕尺寸或异步 resize 时序。
|
||||
function preparePrint(){
|
||||
const widthMm=Number(frame.dataset.layoutWidthMm),heightMm=Number(frame.dataset.layoutHeightMm);
|
||||
if(!(widthMm>0&&heightMm>0))throw new Error('页面尺寸未就绪');
|
||||
const measure=document.createElement('div'),copy=content.cloneNode(true);
|
||||
Object.assign(measure.style,{position:'fixed',left:'0',top:'0',visibility:'hidden',pointerEvents:'none',width:widthMm+'mm',height:heightMm+'mm'});
|
||||
copy.style.transform='none';copy.style.width='100%';copy.classList.remove('compact');copy.removeAttribute('data-scale');
|
||||
measure.appendChild(copy);document.body.appendChild(measure);
|
||||
try{
|
||||
if(copy.scrollHeight>measure.clientHeight)copy.classList.add('compact');
|
||||
const width=Math.max(measure.clientWidth,copy.scrollWidth,copy.offsetWidth);
|
||||
copy.style.width=width+'px';
|
||||
const height=Math.ceil(Math.max(copy.scrollHeight,copy.offsetHeight,copy.getBoundingClientRect().height))+1;
|
||||
if(!(width>0&&height>1))throw new Error('页面尺寸未就绪');
|
||||
canvas.setAttribute('viewBox',`0 0 ${width} ${height}`);
|
||||
foreignObject.setAttribute('width',String(width));foreignObject.setAttribute('height',String(height));
|
||||
foreignObject.replaceChildren(copy);
|
||||
}finally{measure.remove();}
|
||||
}
|
||||
function failed(){document.body.classList.add('fit-error');button.disabled=true;status.textContent='页面暂未排好,请刷新后再打印。';return false;}
|
||||
// 屏幕沿用已保存的纸张预览;明细较多时保留全部商品、附加项和尾部内容。
|
||||
function fit(){
|
||||
if(printing||printMedia.matches)return true;
|
||||
try{
|
||||
content.style.transform='none';content.style.width='100%';content.classList.remove('compact');
|
||||
if(content.scrollHeight>frame.clientHeight)content.classList.add('compact');
|
||||
@@ -20,12 +43,15 @@
|
||||
scale=low;measure(scale);
|
||||
}
|
||||
content.style.transform='scale('+scale+')';content.dataset.scale=String(scale);
|
||||
document.body.classList.remove('fit-error');button.disabled=false;
|
||||
status.textContent=scale<.65?'已排为一页;内容较多,字号已缩小,请先查看预览。':'已排为一页,全部明细与注意事项完整保留。';
|
||||
preparePrint();document.body.classList.remove('fit-error');button.disabled=false;
|
||||
status.textContent=scale<.65?'内容较多,字号已缩小,请先查看打印预览。':'打印时按所选纸张和方向自动排为一页。';
|
||||
return true;
|
||||
}catch{document.body.classList.add('fit-error');button.disabled=true;status.textContent='页面暂未排好,请刷新后再打印。';return false;}
|
||||
}catch{return failed();}
|
||||
}
|
||||
const ready=()=>document.fonts?.ready||Promise.resolve();
|
||||
ready().then(fit);window.addEventListener('load',fit);window.addEventListener('resize',fit);window.addEventListener('beforeprint',fit);
|
||||
ready().then(fit);window.addEventListener('load',fit);window.addEventListener('resize',fit);
|
||||
window.addEventListener('beforeprint',()=>{printing=true;try{preparePrint();document.body.classList.remove('fit-error');}catch{failed();}});
|
||||
window.addEventListener('afterprint',()=>{printing=false;fit();});
|
||||
printMedia.addEventListener('change',event=>{if(!event.matches){printing=false;fit();}});
|
||||
button.addEventListener('click',async()=>{await ready();if(fit())window.print();});
|
||||
})();
|
||||
Reference in new issue
Block a user