feat(label-designer): PDF可编辑导入 + 线条组件

- 导入PDF提取文字层(按基线分行合并)为可编辑文本元素, 底图保留
- 坐标: viewport(1).transform ∘ item.transform -> pt -> mm; 基准测试X=25.4/Y=24.8/字号24pt完全一致, 像素复核11/11落点命中
- 新增线条: 实线/虚线 x 横线/竖线, 粗度0.1-20mm与长度1-600mm可调, 颜色可选
- 导出PDF支持线条(含setLineDash)
- About 面单设计条目更新

CHANGELOG记录
This commit is contained in:
personal82555 committed 2026-10-14 11:29:58 +08:00
1 parent 01fecba91b
commit 8f10f1f47b
5 files changed
+262 -5

No files matched your search

+245 -3
View File
@@ -146,6 +146,71 @@ function rectStyle(el: LabelElement) {
};
}
// 线条:方向由 w/h 派生(w>=h 横线,否则竖线);实线=纯色,虚线=重复渐变
function isHLine(el: LabelElement) {
return el.w >= el.h;
}
// —— 线条属性读写(横/竖 与 实/虚 四种组合)——
function lineThickness(el: LabelElement) {
return Math.round((isHLine(el) ? el.h : el.w) * 100) / 100;
}
function lineLength(el: LabelElement) {
return Math.round((isHLine(el) ? el.w : el.h) * 100) / 100;
}
function setLineDir(el: LabelElement, dir: 'h' | 'v') {
const len = lineLength(el);
const thk = lineThickness(el);
if (dir === 'h') {
el.w = len;
el.h = thk;
}
else {
el.w = thk;
el.h = len;
}
}
function setLineThickness(el: LabelElement, v: number | null) {
if (v == null || v <= 0) {
return;
}
if (isHLine(el)) {
el.h = v;
}
else {
el.w = v;
}
}
function setLineLength(el: LabelElement, v: number | null) {
if (v == null || v <= 0) {
return;
}
if (isHLine(el)) {
el.w = v;
}
else {
el.h = v;
}
}
function lineStyle(el: LabelElement) {
const c = el.stroke ?? '#000000';
if (!el.dash) {
return { background: c };
}
// 虚线段长与间隙按粗度缩放,保证细线也有清晰断口
const thick = Math.max(1, (isHLine(el) ? el.h : el.w) * pxPerMM.value);
const seg = Math.max(6, Math.round(thick * 4));
const dir = isHLine(el) ? 'to right' : 'to bottom';
return {
background: `repeating-linear-gradient(${dir}, ${c} 0, ${c} ${seg}px, transparent ${seg}px, transparent ${seg * 2}px)`,
};
}
// ---------- add elements ----------
function addElement(type: LabelElement['type'], file?: File) {
const d = doc.value;
@@ -165,6 +230,10 @@ function addElement(type: LabelElement['type'], file?: File) {
else if (type === 'rect') {
d.elements.push({ ...base, type, w: 30, h: 10, fill: '#ffffff', stroke: '#000000', strokeWidth: 0 });
}
else if (type === 'line') {
// 默认横线:长 50mm、粗 0.6mm、实线
d.elements.push({ ...base, type, w: 50, h: 0.6, stroke: '#000000', strokeWidth: 0.6, dash: false });
}
else if (type === 'image' && file) {
const reader = new FileReader();
reader.onload = () => {
@@ -277,15 +346,91 @@ async function onPdfFile(e: Event) {
ctx.fillRect(0, 0, canvas.width, canvas.height);
await page.render({ canvasContext: ctx, viewport }).promise;
const src = canvas.toDataURL('image/png');
// 提取文字层:把 PDF 里的文字转成可编辑文本元素(按基线分行合并)
const textEls: LabelElement[] = [];
try {
const PT = 25.4 / 72;
const tc = await page.getTextContent();
const vt = v1.transform; // [a,b,c,d,e,f]
type Part = { x: number; w: number; s: string; fs: number };
const groups: { y: number; parts: Part[] }[] = [];
for (const raw of (tc.items || []) as any[]) {
if (!raw || typeof raw.str !== 'string' || !raw.str.trim()) {
continue;
}
const m = raw.transform || [1, 0, 0, 1, 0, 0];
// 复合变换:viewport.transform ∘ item.transform
const c = vt[0] * m[2] + vt[2] * m[3];
const dd = vt[1] * m[2] + vt[3] * m[3];
const e = vt[0] * m[4] + vt[2] * m[5] + vt[4];
const f = vt[1] * m[4] + vt[3] * m[5] + vt[5];
const fs = Math.hypot(c, dd) || 10;
const wEst = typeof raw.width === 'number' && raw.width > 0 ? raw.width : raw.str.length * fs * 0.9;
const hit = groups.find(gr => Math.abs(gr.y - f) <= Math.max(2, fs * 0.5));
const part: Part = { x: e, w: wEst, s: raw.str, fs };
if (hit) {
hit.parts.push(part);
}
else {
groups.push({ y: f, parts: [part] });
}
}
for (const g of groups) {
const parts = [...g.parts].sort((p, q) => p.x - q.x);
let str = '';
let x0 = parts[0].x;
let x1 = parts[0].x + parts[0].w;
let fs = parts[0].fs;
for (let i = 0; i < parts.length; i++) {
const p = parts[i];
if (i > 0) {
const prev = parts[i - 1];
const gap = p.x - (prev.x + prev.w);
if (gap > fs * 0.25 && !str.endsWith(' ') && !p.s.startsWith(' ')) {
str += ' ';
}
}
str += p.s;
x1 = Math.max(x1, p.x + p.w);
fs = Math.max(fs, p.fs);
}
str = str.replace(/\s+/g, ' ').trim();
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',
});
}
}
catch { /* 无文字层或提取失败:仅保留底图 */ }
doc.value = {
widthMM: wMM,
heightMM: hMM,
bg: { src, name: file.name, pages: pdf.numPages },
elements: [],
elements: textEls,
};
selectedId.value = null;
selectedId.value = textEls[0]?.id ?? null;
zoom.value = 1;
if (pdf.numPages > 1) {
if (textEls.length) {
error.value = `已导入底图,并从 PDF 提取 ${textEls.length} 段文字为可编辑元素(可直接改字、移动)`;
}
else if (pdf.numPages > 1) {
error.value = `提示:该 PDF 共 ${pdf.numPages} 页,已导入第 1 页作为底图`;
}
await pdf.destroy();
@@ -369,6 +514,27 @@ async function renderToCanvas(): Promise<HTMLCanvasElement> {
ctx.strokeRect(x + sw / 2, y + sw / 2, w - sw, h - sw);
}
}
else if (el.type === 'line') {
const c = el.stroke ?? '#000000';
const horizontal = isHLine(el);
const lw = (horizontal ? el.h : el.w) * k;
ctx.strokeStyle = c;
ctx.lineWidth = Math.max(1, lw);
ctx.setLineDash(el.dash ? [Math.max(6, Math.round(lw * 4)), Math.max(6, Math.round(lw * 4))] : []);
const cx = x + w / 2;
const cy = y + h / 2;
ctx.beginPath();
if (horizontal) {
ctx.moveTo(x, cy);
ctx.lineTo(x + w, cy);
}
else {
ctx.moveTo(cx, y);
ctx.lineTo(cx, y + h);
}
ctx.stroke();
ctx.setLineDash([]);
}
else if (el.type === 'text') {
const fontPx = (el.fontSize ?? 10) * FONT_MM_PER_PT * k;
ctx.font = `${el.bold ? 'bold ' : ''}${fontPx}px "Microsoft YaHei","PingFang SC","Noto Sans CJK SC","Source Han Sans SC",sans-serif`;
@@ -639,6 +805,8 @@ onUnmounted(() => {
<div v-else-if="el.type === 'rect'" class="rectbox" :style="rectStyle(el)" />
<div v-else-if="el.type === 'line'" class="linebox" :style="lineStyle(el)" />
<div
v-if="el.id === selectedId"
class="handle"
@@ -681,6 +849,9 @@ onUnmounted(() => {
<c-button size="small" @click="addElement('qr')">
+二维码
</c-button>
<c-button size="small" @click="addElement('line')">
─ 线条
</c-button>
<c-button size="small" @click="addElement('rect')">
+遮盖块
</c-button>
@@ -797,6 +968,63 @@ onUnmounted(() => {
</div>
</template>
<template v-else-if="selected.type === 'line'">
<div class="pos-grid">
<n-form-item label="线条方向">
<n-radio-group
:value="selected.w >= selected.h ? 'h' : 'v'"
@update:value="(v: string) => setLineDir(selected, v as 'h' | 'v')"
>
<n-radio-button value="h">
─ 横线
</n-radio-button>
<n-radio-button value="v">
│ 竖线
</n-radio-button>
</n-radio-group>
</n-form-item>
<n-form-item label="线条样式">
<n-radio-group
:value="selected.dash ? 'dash' : 'solid'"
@update:value="(v: string) => { selected.dash = v === 'dash'; }"
>
<n-radio-button value="solid">
━ 实线
</n-radio-button>
<n-radio-button value="dash">
┄ 虚线
</n-radio-button>
</n-radio-group>
</n-form-item>
</div>
<n-form-item label="粗度 (mm)">
<n-input-number
:value="lineThickness(selected)"
:min="0.1"
:max="20"
:step="0.1"
style="width:100%"
@update:value="(v: number | null) => setLineThickness(selected, v)"
/>
</n-form-item>
<n-form-item label="长度 (mm)">
<n-input-number
:value="lineLength(selected)"
:min="1"
:max="600"
:step="1"
style="width:100%"
@update:value="(v: number | null) => setLineLength(selected, v)"
/>
</n-form-item>
<n-form-item label="线条颜色">
<n-color-picker v-model:value="selected.stroke" :modes="['hex']" :show-alpha="false" />
</n-form-item>
<div class="hint">
实线 / 虚线、横 / 竖可任意组合;粗度控制线宽、长度控制延伸,也可直接拖右下角手柄调整。
</div>
</template>
<template v-else-if="selected.type === 'image'">
<n-form-item label="替换图片">
<c-button size="small" @click="pickImage">
@@ -1006,6 +1234,20 @@ onUnmounted(() => {
box-sizing: border-box;
}
// 线条:填满元素框;透明外扩 5px 便于点中细线
.linebox {
position: relative;
width: 100%;
height: 100%;
box-sizing: border-box;
&::after {
content: '';
position: absolute;
inset: -5px;
}
}
.handle {
position: absolute;
right: -6px;
+3 -1
View File
@@ -1,5 +1,5 @@
// 面单模板库:坐标单位 mm,画布左上角为原点
export type ElType = 'text' | 'barcode' | 'qr' | 'image' | 'rect';
export type ElType = 'text' | 'barcode' | 'qr' | 'image' | 'rect' | 'line';
export interface LabelElement {
id: string;
@@ -21,6 +21,8 @@ export interface LabelElement {
fill?: string;
stroke?: string;
strokeWidth?: number; // mm
// line:方向由 w/h 派生(w>=h 横线,否则竖线);粗度=短边,长度=长边
dash?: boolean; // true=虚线,false/undefined=实线
// image
src?: string;
}