From 85f7f9abb48b79333caf0fd3489a4846cc8bcf97 Mon Sep 17 00:00:00 2001
From: funjia <13429193897@163.com>
Date: Sun, 4 Oct 2026 00:32:03 +0800
Subject: [PATCH] =?UTF-8?q?fix:=20=E6=89=93=E5=8D=B0=E4=B8=8D=E5=86=8D?=
=?UTF-8?q?=E8=BE=93=E5=87=BA=E6=B5=8F=E8=A7=88=E5=99=A8=E9=A1=B5=E7=9C=89?=
=?UTF-8?q?=E4=B8=8E=E5=BA=8F=E5=8F=B7=EF=BC=88=E5=8E=9F=20Web=20=E7=89=88?=
=?UTF-8?q?=20+=20=E6=8F=92=E4=BB=B6=E7=89=88=E9=83=BD=E4=BF=AE=EF=BC=89?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
用户反馈:打印时页眉带日期时间,序号 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 }`
会**直接命中里面的 1** 把它又显出来 —— 序号照样印在纸上,
而这条规则看上去毫无问题。现在所有「打印时不该出现」的选择器一律写成
`.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。
---
plugin/README.md | 3 +-
plugin/plugin.config.json | 2 +-
plugin/src/components/LabelCell.vue | 16 ++++++-
plugin/src/pages/PrintTagPage.vue | 68 +++++++++++++++++++++++++---
src/components/LabelCell.vue | 15 ++++++-
src/components/Main.vue | 69 +++++++++++++++++++++++++----
6 files changed, 153 insertions(+), 20 deletions(-)
diff --git a/plugin/README.md b/plugin/README.md
index 432f9ec..8295283 100644
--- a/plugin/README.md
+++ b/plugin/README.md
@@ -5,7 +5,8 @@
- 插件 id:`funjia.print-tag`
- 产物:`dist/PrintTagBundle.iife.js` + `dist/PrintTagBundle.css`(IIFE,自注册)
-- 上架位置:`funjia-plugin-repository` 插件仓库的 `repository/plugins/funjia.print-tag/1.0.0/`
+- 上架位置:`funjia-plugin-repository` 插件仓库的 `repository/plugins/funjia.print-tag/<版本>/`
+ (版本号在 `plugin.config.json` 里改,当前见该文件)
仓库根目录那份 `src/` 是**原独立 Web 版**,两者并存、互不影响。
diff --git a/plugin/plugin.config.json b/plugin/plugin.config.json
index 0bd094f..9811b9a 100644
--- a/plugin/plugin.config.json
+++ b/plugin/plugin.config.json
@@ -1,7 +1,7 @@
{
"id": "funjia.print-tag",
"name": "标签打印",
- "version": "1.0.0",
+ "version": "1.0.1",
"kind": "iife",
"artifact": "PrintTagBundle.iife.js",
"style": "PrintTagBundle.css",
diff --git a/plugin/src/components/LabelCell.vue b/plugin/src/components/LabelCell.vue
index 89a19c7..82b79fc 100644
--- a/plugin/src/components/LabelCell.vue
+++ b/plugin/src/components/LabelCell.vue
@@ -125,9 +125,19 @@ const emit = defineEmits(["select", "toggle-hide"]);
/* ====== 打印 ====== */
@media print {
- /* 序号角标与空格引导均不打印 */
+ /**
+ * ⚠️ 选择器必须写成 `.x, .x *` 两段 —— 序号角标里的数字在一个 `` 里,
+ * 而 `visibility` 是**可被子元素覆盖的继承属性**:只隐父 div 的话,
+ * 任何「让标签纸整体可见」的规则(`.pt-paper * { visibility: visible }`)
+ * 会直接命中那个 span 把它又显出来,序号就印在纸上了。
+ *
+ * 页面里的打印样式(`PrintTagPage.vue` 的 applyPrintStyles)也有同一组规则,
+ * 那份是给「按 Ctrl+P」等不经过插件按钮的路径兜底的;两份都要留。
+ */
.pt-cell__idx,
- .pt-cell--placeholder {
+ .pt-cell__idx *,
+ .pt-cell--placeholder,
+ .pt-cell--placeholder * {
visibility: hidden !important;
border-color: transparent !important;
background: transparent !important;
@@ -142,6 +152,8 @@ const emit = defineEmits(["select", "toggle-hide"]);
background: transparent !important;
}
+ /* 双击隐藏的格子:内容不打印,但仍要占住格子位置 */
+ .pt-hide,
.pt-hide * {
visibility: hidden !important;
}
diff --git a/plugin/src/pages/PrintTagPage.vue b/plugin/src/pages/PrintTagPage.vue
index 8f18d46..ce3b3f7 100644
--- a/plugin/src/pages/PrintTagPage.vue
+++ b/plugin/src/pages/PrintTagPage.vue
@@ -696,20 +696,70 @@ let printStyle: HTMLStyleElement | null = null;
* 原因:`body * { visibility: hidden }` 这种「只留标签纸」的规则如果常驻,
* 用户去打印宿主里**任何别的页面**都会是一张白纸 —— 插件 CSS 是全局注入的,
* 它分不清「现在打印的是不是我的页面」。临时注入把作用范围钉死在这一次打印上。
+ *
+ * ## 三处必须这么写,改之前先看完
+ *
+ * **① `@page { margin: 0 }` 是「不要页眉页脚」的唯一开关。**
+ *
+ * 浏览器会在**页边距那块空白**里画自己的页眉页脚(日期时间 + 标题 / URL + 页码),
+ * 这是它的打印设置,CSS 里没有 `header: none` 这种东西。把边距设成 0,
+ * 那块空白没了,页眉页脚自然也就不画了 —— 这是 Chromium/Edge 上唯一可靠的做法。
+ * 代价是标签纸在纸上的位置不能再靠 `@page margin` 给,改由 `.pt-paper` 的
+ * `top` / `left` 控制:物理结果和原来一模一样(边距设置就是「离纸边的距离」)。
+ *
+ * **② 「不该打印的东西」必须连后代一起隐,而且要写在 `.pt-paper *` 之后。**
+ *
+ * `visibility` 是**继承属性,但子元素可以覆盖它**。原来只写
+ * `.pt-cell__idx { visibility: hidden }`,父 div 确实隐了,可下面这条
+ * `.pt-paper * { visibility: visible !important }` 会**直接命中里面的 `1`**
+ * 把它又显出来 —— 序号照样印在纸上。所以必须写成 `.x, .x *` 形式。
+ *
+ * 同理,同特异性 + 同 `!important` 时**后写的赢**,所以这一组必须排在
+ * `.pt-paper *` 后面,否则完全不起作用(改了没效果、又不报错)。
+ *
+ * **③ 双击隐藏的格子(`.pt-hide`)也走同一条路。**
+ *
+ * 否则「隐藏(不打印)」只藏了内容的外层,`.pt-paper *` 又把里面的字放出来,
+ * 用户以为隐藏了、纸上却印着。
*/
const applyPrintStyles = () => {
- if (printStyle) document.head.removeChild(printStyle);
+ // 用 `node.remove()` 而不是 `document.head.removeChild(node)`:
+ // 节点可能已经不在 head 里了(上一次撤过、或宿主清过 DOM),
+ // 对已脱离的节点调 removeChild 会抛 NotFoundError,把整段注入**打断在半路** ——
+ // 症状是「这次打印页眉序号全都没生效」,而没人会往「上次留下的引用」上想。
+ // remove() 对已脱离节点是安全的 no-op。
+ if (printStyle) printStyle.remove();
+
+ // 边距 → 标签纸相对纸边的偏移(cm)。防呆:负数按 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 {
html, body { margin: 0 !important; padding: 0 !important; background: #fff !important; }
+
+ /* 先全隐,只留标签纸:否则宿主的侧边栏、顶栏、插件的面板都会跟着打印 */
body * { visibility: hidden !important; }
.pt-paper, .pt-paper * { visibility: visible !important; }
- /* 用 absolute 钉回纸张原点:不管宿主把内容区摆在哪,标签都从 (0,0) 开始 */
- .pt-paper { position: absolute; left: 0; top: 0; width: 194mm; box-shadow: none !important; }
- .pt-cell--placeholder, .pt-cell__idx { visibility: hidden !important; }
+
+ /* ②③ 再把不该打印的连后代一起隐掉 —— 必须在这一组之前先把上面那条写完 */
+ .pt-cell__idx, .pt-cell__idx *,
+ .pt-cell--placeholder, .pt-cell--placeholder *,
+ .pt-hide, .pt-hide * { visibility: hidden !important; }
+
+ /* 标签纸钉在纸边偏移处:@page 边距已经是 0,位置只能自己给 */
+ .pt-paper {
+ position: absolute;
+ left: ${offsetX}cm;
+ top: ${offsetY}cm;
+ width: 194mm;
+ box-shadow: none !important;
+ }
}
`;
document.head.appendChild(printStyle);
@@ -717,7 +767,8 @@ const applyPrintStyles = () => {
const onAfterPrint = () => {
if (printStyle) {
- document.head.removeChild(printStyle);
+ // 同上:remove() 对已脱离的节点安全
+ printStyle.remove();
printStyle = null;
}
};
@@ -734,6 +785,10 @@ const onPrint = () => {
/* ================================================================== */
onMounted(async () => {
+ // beforeprint 覆盖「用户按 Ctrl+P」以及宿主自己的打印入口 —— 那些路径不会经过
+ // onPrint(),没有这条监听,打印出来的就是屏幕上的样子(带着宿主的顶栏和时间)。
+ // 注册在页面级、卸载时摘掉,所以只影响这一个页面。
+ window.addEventListener("beforeprint", applyPrintStyles);
window.addEventListener("afterprint", onAfterPrint);
if (snEnabled.value) await loadSnMeta();
@@ -742,6 +797,7 @@ onMounted(async () => {
});
onBeforeUnmount(() => {
+ window.removeEventListener("beforeprint", applyPrintStyles);
window.removeEventListener("afterprint", onAfterPrint);
onAfterPrint();
});
diff --git a/src/components/LabelCell.vue b/src/components/LabelCell.vue
index 8bd6c2a..8d2377f 100644
--- a/src/components/LabelCell.vue
+++ b/src/components/LabelCell.vue
@@ -163,9 +163,18 @@ const emit = defineEmits(['select', 'toggle-hide']);
}
@media print {
- /* 序号角标与空格引导均不打印 */
+ /**
+ * 序号角标与空格引导均不打印。
+ *
+ * ⚠️ 选择器必须写成 `.x, .x *` 两段:序号里的数字在一个 `` 里,
+ * 而 `visibility` 是**可被子元素覆盖的继承属性**。只隐父 div 的话,
+ * 让标签纸整体可见的那条 `.page * { visibility: visible }` 会直接命中
+ * 那个 span 把它又显出来 —— 序号照样印在纸上,而且这条规则看上去毫无问题。
+ */
.id,
- .placeholder {
+ .id *,
+ .placeholder,
+ .placeholder * {
visibility: hidden !important;
border-color: transparent !important;
background: transparent !important;
@@ -180,6 +189,8 @@ const emit = defineEmits(['select', 'toggle-hide']);
background: transparent !important;
}
+ /* 双击隐藏的格子:内容不打印,但仍占住格子位置 */
+ .hide,
.hide * {
visibility: hidden !important;
}
diff --git a/src/components/Main.vue b/src/components/Main.vue
index a7e85ef..524f475 100644
--- a/src/components/Main.vue
+++ b/src/components/Main.vue
@@ -99,7 +99,7 @@