增强打印预览功能,新增离线打印功能,新增缩放控制按钮以提升用户体验。优化打印数据准备逻辑,支持实时预览缩放,确保打印效果的一致性。同时,重构相关视图和服务以增强系统的可维护性和扩展性。

This commit is contained in:
geht
2026-05-14 11:25:17 +08:00
parent 8bcc34aee0
commit 296bc2a4b2
7 changed files with 702 additions and 109 deletions

View File

@@ -23,7 +23,7 @@ public static class NativePrintRenderService
/// 将模板 JSON + 数据对象渲染为完整的可打印 HTML 页面(自包含,无外部依赖)。
/// 屏幕样式与后端前端预览保持一致:深灰底(#525659+ 白纸居中 + 页间分割线。
/// </summary>
public static string RenderToHtml(string templateJson, JsonObject data)
public static string RenderToHtml(string templateJson, JsonObject data, bool enableScreenAutoFit = true)
{
var schema = JsonNode.Parse(templateJson) ?? throw new ArgumentException("无效模板 JSON");
var page = schema["page"] ?? throw new ArgumentException("模板缺少 page 配置");
@@ -86,29 +86,32 @@ public static class NativePrintRenderService
sb.Append(" .qhmes-native-screen-page-sep { display: none !important; }\n");
sb.Append(" }\n");
sb.Append("</style>\n");
sb.Append("<script>\n");
sb.Append("(function(){\n");
sb.Append(" function fitPage(){\n");
sb.Append(" var p=document.querySelector('.qhmes-native-print-root');\n");
sb.Append(" if(!p)return;\n");
sb.Append(" var vw=window.innerWidth||document.documentElement.clientWidth||1;\n");
sb.Append(" var vh=window.innerHeight||document.documentElement.clientHeight||1;\n");
sb.Append(" var pw=p.offsetWidth||1;\n");
sb.Append(" var ph=p.offsetHeight||1;\n");
sb.Append(" var maxW=Math.max(1,vw-40);\n");
sb.Append(" var maxH=Math.max(1,vh-56);\n");
sb.Append(" var s=Math.min(maxW/pw,maxH/ph,1);\n");
sb.Append(" p.style.transform='scale('+s+')';\n");
sb.Append(" p.style.transformOrigin='top center';\n");
sb.Append(" p.style.marginBottom=((1-s)*ph)+'px';\n");
sb.Append(" }\n");
sb.Append(" window.addEventListener('load',fitPage);\n");
sb.Append(" window.addEventListener('resize',fitPage);\n");
sb.Append(" setTimeout(fitPage,0);\n");
sb.Append(" setTimeout(fitPage,150);\n");
sb.Append(" setTimeout(fitPage,400);\n");
sb.Append("})();\n");
sb.Append("</script>\n");
if (enableScreenAutoFit)
{
sb.Append("<script>\n");
sb.Append("(function(){\n");
sb.Append(" function fitPage(){\n");
sb.Append(" var p=document.querySelector('.qhmes-native-print-root');\n");
sb.Append(" if(!p)return;\n");
sb.Append(" var vw=window.innerWidth||document.documentElement.clientWidth||1;\n");
sb.Append(" var vh=window.innerHeight||document.documentElement.clientHeight||1;\n");
sb.Append(" var pw=p.offsetWidth||1;\n");
sb.Append(" var ph=p.offsetHeight||1;\n");
sb.Append(" var maxW=Math.max(1,vw-40);\n");
sb.Append(" var maxH=Math.max(1,vh-56);\n");
sb.Append(" var s=Math.min(maxW/pw,maxH/ph,1);\n");
sb.Append(" p.style.transform='scale('+s+')';\n");
sb.Append(" p.style.transformOrigin='top center';\n");
sb.Append(" p.style.marginBottom=((1-s)*ph)+'px';\n");
sb.Append(" }\n");
sb.Append(" window.addEventListener('load',fitPage);\n");
sb.Append(" window.addEventListener('resize',fitPage);\n");
sb.Append(" setTimeout(fitPage,0);\n");
sb.Append(" setTimeout(fitPage,150);\n");
sb.Append(" setTimeout(fitPage,400);\n");
sb.Append("})();\n");
sb.Append("</script>\n");
}
sb.Append("</head>\n<body>\n");
sb.Append($"<div class=\"qhmes-native-print-root\" style=\"width:{wStr}mm;min-height:{thStr}mm;height:auto;overflow:visible;box-sizing:border-box;\">\n");