fix(label-designer): 修复导入PDF文字重影

- 根因: 文字层提取后底图仍保留原字 -> 原字+叠加字两层同位(阴影/叠字)
- 修复: 提取时在 toDataURL 之前把底图对应文字擦白, 底图只留版式/条码/logo
- 只在近白低饱和背景擦除(中位亮度>=205且色差<=40), 彩色/深色区保留原字不叠加, 避免白块

验证: 基准PDF墨迹 0.15->0; ICLR 抽检9/9擦除; 视觉清晰单份无重影 ✓

CHANGELOG记录
This commit is contained in:
personal82555 committed 2026-10-14 12:01:44 +08:00
1 parent 8f10f1f47b
commit ea6acf3f5f
3 files changed
+66 -19

No files matched your search

+7
View File
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file. See [standa
## Version 2026.10.12-it-tools-pro
### 🩹 修复:导入 PDF 文字重影
- **根因**:文字层提取后,底图仍保留原字 → 原字 + 叠加字两层同位,出现"阴影/叠字"
- **修复**:提取时同步**把底图上对应文字擦白**(在 `toDataURL` 之前处理),底图只留版式、条码、logo 等非文字元素
- **只在近白低饱和背景擦除**(中位亮度 ≥205 且色差 ≤40):粉色条/深色区域保留原字且**不叠加**元素,避免彩色底上留白块
- 验证:基准 PDF 元素框内底图墨迹 0.15 → **0**(原字已擦);ICLR 两栏论文抽检 9/9 全部擦除;视觉复核文字**清晰单份无重影** ✓
### 📐 面单设计:PDF 可编辑导入 + 线条组件
- **导入 PDF 不再只是死图**:上传后自动用 pdf.js 提取**文字层**,按基线分行合并成**可编辑文本元素**(可直接改字、移动、调字号),底图仍保留做参照;无文字层(扫描件)则退回纯底图
- 坐标换算:`viewport(scale=1).transform ∘ item.transform` → pt → mm;实测单行 Helvetica 24pt 基准 PDF 提取结果 X=25.4mm / Y=24.8mm / 字号=24pt,与期望值**完全一致**;像素级复核 11/11 元素落点均压在底图墨迹上
+1 -1
View File
@@ -39,7 +39,7 @@ const groups: { title: string; items: Item[] }[] = [
{ name: '二维码条码识别', icon: '扫', desc: '上传/拖拽/粘贴图片一键识别,支持多码混合批量输出,本地 WASM 解码,图片不上传', tag: '全新' },
{ name: '在线翻译', icon: '译', desc: '中英日韩等 16 种语言互译,自动识别源语言,支持导入 Word/PDF/TXT 等文档整篇翻译、并发处理大文档、译文可下载 TXT/DOCX', tag: '全新' },
{ name: '图片OCR / PDF文字识别', icon: '识', desc: '本地 OCR 引擎,截图扫描件文字提取(准确率 99%+);PDF 逐页识别、预览、导出 TXT', tag: '全新' },
{ name: '面单设计', icon: '单', desc: '快递面单模板开箱即用;上传 PDF 会自动把**文字层提取成可编辑元素**(直接改字/移动,不再是死图);新增**实线/虚线 × 横线/竖线**线条组件可调粗度长度;300dpi 高清导出', tag: '全新' },
{ name: '面单设计', icon: '单', desc: '快递面单模板开箱即用;上传 PDF 自动把**文字层提取成可编辑元素**并**擦除底图原字**(可直接改字、不产生重影);新增**实线/虚线 × 横线/竖线**线条组件可调粗度长度;300dpi 高清导出', tag: '全新' },
{ name: 'IT网站收藏', icon: '藏', desc: '内置 91 个精选开发站点(社区/文档/工具/AI/设计/视频/媒体平台),数据存用户本地,可自选目录', tag: '全新' },
{ name: '文档格式转换', icon: '转', desc: 'Word/PDF/TXT/RTF/ODT/EPUB/MOBI/AZW3/HTML 九格式互转,自建 calibre 引擎,批量打包下载', tag: '全新' },
{ name: '视频工具', icon: '视', desc: '格式转换、压缩、裁剪、旋转、变速、消音、提取音频、截图、GIF 互转、加水印/去水印等 13 个工具 + AI 字幕生成,服务端 ffmpeg 高速处理,最大 2GB', tag: '全新' },
+58 -18
View File
@@ -345,9 +345,33 @@ async function onPdfFile(e: Event) {
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
await page.render({ canvasContext: ctx, viewport }).promise;
const src = canvas.toDataURL('image/png');
// 提取文字层:把 PDF 里的文字转成可编辑文本元素(按基线分行合并)
// 提取文字层:把 PDF 里的文字转成可编辑文本元素(按基线分行合并),
// 同时把底图上对应的文字擦掉,避免"原字 + 叠加字"形成重影
function boxBackground(xMm: number, yMm: number, wMm: number, hMm: number) {
const k = EXPORT_DPI / 25.4;
let x = Math.max(0, Math.round(xMm * k));
let y = Math.max(0, Math.round(yMm * k));
let w = Math.min(canvas.width - x, Math.round(wMm * k));
let h = Math.min(canvas.height - y, Math.round(hMm * k));
if (w <= 0 || h <= 0) {
return { lum: 255, spread: 0 };
}
const d = ctx.getImageData(x, y, w, h).data;
const lum: number[] = [];
const spr: number[] = [];
for (let i = 0; i < d.length; i += 4) {
const r = d[i];
const g = d[i + 1];
const b = d[i + 2];
lum.push(r * 0.299 + g * 0.587 + b * 0.114);
spr.push(Math.max(r, g, b) - Math.min(r, g, b));
}
lum.sort((a, b2) => a - b2);
spr.sort((a, b2) => a - b2);
const mid = Math.floor(lum.length / 2);
return { lum: lum[mid], spread: spr[mid] };
}
const textEls: LabelElement[] = [];
try {
const PT = 25.4 / 72;
@@ -399,26 +423,42 @@ async function onPdfFile(e: Event) {
if (!str || textEls.length >= 300) {
continue;
}
const wMM = Math.max(6, (x1 - x0) * PT + 2); // 略放宽,避免 pre-wrap 换行被裁
const hMM = Math.max(3, fs * 1.4 * PT);
const yMM = (g.y - fs * 0.9) * PT;
textEls.push({
id: newId(),
type: 'text',
x: Math.max(0, Math.round(x0 * PT * 10) / 10),
y: Math.max(0, Math.round(yMM * 10) / 10),
w: Math.round(wMM * 10) / 10,
h: Math.round(hMM * 10) / 10,
text: str,
fontSize: Math.round(fs * 10) / 10,
bold: false,
align: 'left',
color: '#000000',
});
const txtW = Math.max(1, (x1 - x0) * PT); // 精确文字宽度(用于擦除)
const elW = Math.max(6, txtW + 2); // 元素显示宽度,+2 防 pre-wrap 换行被裁
const elH = Math.max(3, fs * 1.4 * PT);
const elY = (g.y - fs * 0.9) * PT;
const elX = Math.max(0, x0 * PT);
// 只在「近白、低饱和」背景处擦除底图原字——彩色/深色区域保留原字且不叠加,避免白块与重影
const bg = boxBackground(elX, elY, txtW, elH);
if (bg.lum >= 205 && bg.spread <= 40) {
const kk = EXPORT_DPI / 25.4;
ctx.fillStyle = '#ffffff';
ctx.fillRect(
Math.max(0, Math.round(elX * kk)),
Math.max(0, Math.round(elY * kk)),
Math.max(1, Math.round(txtW * kk)),
Math.max(1, Math.round(elH * kk)),
);
textEls.push({
id: newId(),
type: 'text',
x: Math.round(elX * 10) / 10,
y: Math.max(0, Math.round(elY * 10) / 10),
w: Math.round(elW * 10) / 10,
h: Math.round(elH * 10) / 10,
text: str,
fontSize: Math.round(fs * 10) / 10,
bold: false,
align: 'left',
color: '#000000',
});
}
}
}
catch { /* 无文字层或提取失败:仅保留底图 */ }
const src = canvas.toDataURL('image/png'); // 擦除原字后再生成底图
doc.value = {
widthMM: wMM,
heightMM: hMM,