fix: 打印不再输出浏览器页眉与序号(原 Web 版 + 插件版都修)
用户反馈:打印时页眉带日期时间,序号 1–44 也被印出来。两处根因如下。
1. 浏览器页眉(左上角日期时间 + 标题)
@page 边距非 0 时,浏览器会在「页边距那块空白」里画它自己的页眉页脚。
CSS 里没有关掉它的开关 —— 把边距清零、让那块空白消失,是 Chromium/Edge 上
唯一可靠的做法(Firefox 的页眉页脚是打印对话框里的独立选项,CSS 管不了)。
代价:标签纸在纸上的位置不能再靠 @page margin 给,改由 .page / .pt-paper 的
left / top 控制 —— 物理结果与之前完全一致,顶栏「上下/左右边距」的含义
变成「标签纸离纸边的距离」。
2. 序号角标与空格虚线引导照样打印
visibility 是**可被子元素覆盖的继承属性**。原来只隐 `.id` / `.pt-cell__idx`,
父元素确实隐了,但下面那条让标签纸整体可见的 `.page * { visibility: visible }`
会**直接命中里面的 <span>1</span>** 把它又显出来 —— 序号照样印在纸上,
而这条规则看上去毫无问题。现在所有「打印时不该出现」的选择器一律写成
`.x, .x *` 两段;插件版还要求它们排在 `.pt-paper *` 之后
(同特异性 + 同 !important 时后写的赢,写前面等于没写、还不报错)。
顺带修掉双击「隐藏(不打印)」的格子内容仍被打印的问题。
顺带加固:
- Ctrl+P 也走同一套样式 —— 加 beforeprint 监听(原来只有点插件按钮才注入)。
- 撤销样式改用 node.remove():对已脱离的节点调 removeChild 会抛 NotFoundError,
把整段注入打断在半路,症状是「这次打印什么都没生效」。
验证(Playwright + 本机 Edge,走真实打印管线):
- 插件版 13 项断言通过;原 Web 版 12 项断言通过。
- 两版都生成 PDF(displayHeaderFooter: true)并用浏览器 PDF 视图渲染确认:
页眉无日期时间、无序号、双击隐藏的格子留白、有内容的格子正常打印。
- 插件产物已升版本发布为 funjia.print-tag@1.0.1。
This commit is contained in:
+61
-8
@@ -99,7 +99,7 @@
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { computed, reactive, ref, watch } from "vue";
|
||||
import { computed, onBeforeUnmount, onMounted, reactive, ref, watch } from "vue";
|
||||
|
||||
// 导入所有模板组件(default = 标签内容渲染组件,formSchema/initialValues = 表单配置)
|
||||
import Error, { formSchema as formSchema_Error, initialValues as initialValues_Error } from './Error.vue';
|
||||
@@ -326,22 +326,72 @@ const onPrint = () => {
|
||||
}
|
||||
};
|
||||
|
||||
const applyPrintStyles = () => {
|
||||
const removePrintStyles = () => {
|
||||
// 用 `node.remove()` 而不是 `document.head.removeChild(node)`:节点可能已经
|
||||
// 不在 head 里了(上一次撤过、或宿主清过 DOM),对已脱离的节点调 removeChild
|
||||
// 会抛 NotFoundError,把整段注入打断在半路 —— 症状是「这次打印什么都没生效」。
|
||||
// remove() 对已脱离节点是安全的 no-op。
|
||||
if (printStyle) {
|
||||
document.head.removeChild(printStyle);
|
||||
printStyle.remove();
|
||||
printStyle = null;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 打印样式**临时注入、打完就撤**。
|
||||
*
|
||||
* ## 为什么 `@page` 的边距是 0
|
||||
*
|
||||
* 浏览器会在**页边距那块空白**里画它自己的页眉页脚(左上角日期时间 + 页面标题,
|
||||
* 底部 URL + 页码)。CSS 里没有「关掉页眉」的开关,把边距设成 0、让那块空白
|
||||
* 消失,是 Chromium / Edge 上唯一可靠的做法。
|
||||
*
|
||||
* 代价是标签纸在纸上的位置不能再靠 `@page margin` 给,改由 `.page` 的
|
||||
* `left` / `top` 控制 —— 顶栏那两个「上下边距 / 左右边距」的含义变成
|
||||
* 「标签纸离纸边的距离」,物理结果与之前完全一致。
|
||||
*
|
||||
* > 注意:Firefox 的页眉页脚是打印对话框里的独立选项,CSS 控制不了,
|
||||
* > 需要在打印设置里手动取消勾选。
|
||||
*/
|
||||
const applyPrintStyles = () => {
|
||||
removePrintStyles();
|
||||
|
||||
// 负数会让标签跑到纸外,按 0 处理
|
||||
const offsetX = Math.max(0, Number(marginHorizontal.value) || 0);
|
||||
const offsetY = Math.max(0, Number(marginHeight.value) || 0);
|
||||
|
||||
printStyle = document.createElement('style');
|
||||
printStyle.setAttribute('media', 'print');
|
||||
printStyle.textContent = `
|
||||
@page {
|
||||
margin: ${marginHeight.value}cm ${marginHorizontal.value}cm 0;
|
||||
}
|
||||
/* 边距清零:浏览器就不会在边距里画「日期时间 / 标题 / URL / 页码」 */
|
||||
@page { margin: 0; }
|
||||
|
||||
@media print {
|
||||
body { margin: 0; padding: 0; }
|
||||
body { margin: 0 !important; padding: 0 !important; }
|
||||
|
||||
/* 标签纸钉在「离纸边的偏移」处(边距已经是 0,位置只能自己给) */
|
||||
.page {
|
||||
position: absolute;
|
||||
left: ${offsetX}cm;
|
||||
top: ${offsetY}cm;
|
||||
width: 194mm;
|
||||
}
|
||||
}
|
||||
`;
|
||||
document.head.appendChild(printStyle);
|
||||
};
|
||||
|
||||
// Ctrl+P 不经过 onPrint(),没有这条监听就打不出「只要标签纸」的效果
|
||||
onMounted(() => {
|
||||
window.addEventListener('beforeprint', applyPrintStyles);
|
||||
window.addEventListener('afterprint', removePrintStyles);
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
window.removeEventListener('beforeprint', applyPrintStyles);
|
||||
window.removeEventListener('afterprint', removePrintStyles);
|
||||
removePrintStyles();
|
||||
});
|
||||
</script>
|
||||
|
||||
<style>
|
||||
@@ -682,6 +732,9 @@ body {
|
||||
}
|
||||
|
||||
@page {
|
||||
margin: 0.89cm 0.80cm 0;
|
||||
/* 刻意留 0:边距一旦非 0,浏览器就会在边距里画自己的页眉页脚
|
||||
(日期时间 / 标题 / URL / 页码)。标签纸在纸上的位置由
|
||||
applyPrintStyles() 注入的 .page { left / top } 控制,见那个函数的注释。 */
|
||||
margin: 0;
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user