fix(pdf-tools): 水印中文显示成??? — 嵌入Droid Sans Fallback中文字体子集

- Helvetica(WinAnsi)无法编码中文被替换为?, 改为含非西文字符时按需加载
  public/fonts/DroidSansFallback.ttf + @cantoo/fontkit embedFont(subset)
- 新增依赖 @cantoo/fontkit; 纯英文水印保持原Helvetica路径不下载字体
- 浏览器端到端验证: 中文文本层HAS_CN=true无???, 英文回归通过
This commit is contained in:
jinghui committed 2026-11-14 22:42:27 +08:00
1 parent 78c348198d
commit 3aefc2633b
5 files changed
+74 -6

No files matched your search

+15
View File
@@ -97,6 +97,21 @@
- 表格「类型」列同步显示中文功能名(原为 kind 英文原值);
- 仍带 `X-Admin-Token` 鉴权,无令牌 403。
### PDF 水印支持中文(2026.11 修复)
**问题**:PDF 工具「添加水印」输入中文后,生成的 PDF 里中文显示成 `???`。
**原因**:水印文字用 `embedFont('Helvetica')` 绘制——Helvetica 是 PDF 西文标准 14 字体(WinAnsi 编码),无法编码中文,pdf-lib 把超出编码范围的字符全部替换成 `?`。
**修复**(`src/tools/pdf-tools/pdf-tools.vue`):
- 新增依赖 `@cantoo/fontkit`(`@cantoo/pdf-lib` 官方推荐的 fontkit 实现);
- 中文字体 Droid Sans Fallback(4MB,覆盖 CJK,系统自带 `/usr/share/fonts/truetype/droid/`)放入 `public/fonts/DroidSansFallback.ttf`,由 nginx 直接提供;
- 水印文字含非西文字符(`charCodeAt > 0xFF`)时才按需 `fetch` 字体并 `registerFontkit` + `embedFont(bytes, { subset: true })`——子集只嵌入用到的字形,输出 PDF 增量很小(实测 3.5KB);纯英文水印仍走原 Helvetica 路径,不下载字体;
- 字节缓存在模块级变量,同一次页面会话只拉一次。
**验证**(浏览器端到端):上传测试 PDF → 中文水印「内部机密禁止外传」→ 执行 → pdfjs 提取文本层 `HAS_CN=true`、无 `???`;pdftoppm 渲染截图确认斜向水印字形正常;英文水印「CONFIDENTIAL 2026」回归通过(走 Helvetica,输出 1.6KB 无字体子集)。
### 历史记录 100 条上限(2026.11 新增)
所有功能里的历史记录最多显示 100 条(按时间倒序取最新 100 条):
+1
View File
@@ -37,6 +37,7 @@
"release": "node ./scripts/release.mjs"
},
"dependencies": {
"@cantoo/fontkit": "^2.0.12",
"@cantoo/pdf-lib": "^2.11.1",
"@it-tools/bip39": "^0.0.4",
"@it-tools/oggen": "^1.3.0",
+35 -4
View File
@@ -11,6 +11,9 @@ importers:
.:
dependencies:
'@cantoo/fontkit':
specifier: ^2.0.12
version: 2.0.12
'@cantoo/pdf-lib':
specifier: ^2.11.1
version: 2.11.1(html-entities@2.6.0)
@@ -1184,6 +1187,9 @@ packages:
resolution: {integrity: sha512-Vj1jF3cPfxg7OAfoI7QnVKLoILlm2JF9pnVHrX8qx7AHMiYWT+NDAA7jChlNgRS4WTLc/fD1lXLmPixluj+3Gg==}
engines: {node: '>=6.9.0'}
'@cantoo/fontkit@2.0.12':
resolution: {integrity: sha512-B9ZE4pA3cUf3pzn7UaPCtjyejd1ie16SLquCqdlGQiDncFgn8iD29dU9vD3dSgKkxAUx2ttvjg/li9JVkj5+fQ==}
'@cantoo/pdf-lib@2.11.1':
resolution: {integrity: sha512-lYuRJFfXgoP8WVE8HYSeoLfDcnSXmjMLmdZS7YvmYHngbcZfiqDOruHrZBqjYizkrNa4ad1yW4WCrPYyb26T3Q==}
peerDependencies:
@@ -2660,6 +2666,9 @@ packages:
resolution: {integrity: sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==}
engines: {node: '>=8'}
brotli@1.3.3:
resolution: {integrity: sha512-oTKjJdShmDuGW94SyyaoQvAjf30dZaHnjJ8uAF+u2/vGJkJbJPJAT1gDiOJP5v1Zb6f9KEyW/1HpuaWIXtGHPg==}
browserslist@4.21.9:
resolution: {integrity: sha512-M0MFoZzbUrRU4KNfCrDLnvyE7gub+peetoTid3TBIqtunaDJyXlwhakT+/VkvSXcfIzFfK/nkCs4nmyTmxdNSg==}
engines: {node: ^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7}
@@ -3114,6 +3123,9 @@ packages:
resolution: {integrity: sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==}
engines: {node: '>=8'}
dfa@1.2.0:
resolution: {integrity: sha512-ED3jP8saaweFTjeGX8HQPjeC1YYyZs98jGNZx6IiBvxW7JG5v492kamAQB3m2wop07CvU/RQmzcKr6bgcC5D/Q==}
diff-sequences@29.4.3:
resolution: {integrity: sha512-ofrBgwpPhCD85kMKtE9RYFFq6OC1A89oW2vvgWZNCwxrUpRUILopY7lsYyMDSjc8g6U6aiO0Qubg6r4Wgt5ZnA==}
engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0}
@@ -3849,6 +3861,7 @@ packages:
image-size@0.5.5:
resolution: {integrity: sha512-6TDAlDPZxUFCv+fuOkIoXT/V/f3Qbq8e37p+YOiYrUv3v9cc3/6x78VdfPgFVaB9dZYeLUfKgHRebpkm/oP2VQ==}
engines: {node: '>=0.10.0'}
deprecated: Please migrate to image-size@2.x
hasBin: true
import-fresh@3.3.0:
@@ -5064,6 +5077,9 @@ packages:
resolution: {integrity: sha512-l+sSefzHpj5qimhFSE5a8nufZYAM3sBSVMAPtYkmC+4EH2anSGaEMXSD0izRQbu9nfyQ9y5JrVmp7E8oZrUjvA==}
engines: {node: '>=8'}
restructure@3.0.2:
resolution: {integrity: sha512-gSfoiOEA0VPE6Tukkrr7I0RBdE0s7H1eFCDBk05l1KIQT1UIKNc5JZy6jdyW6eYH3aR3g5b3PuL77rq0hvwtAw==}
ret@0.1.15:
resolution: {integrity: sha512-TTlYpa+OL+vMMNG24xSlQGEJ3B/RzEfUlLct7b5G/ytav+wPrplCpVMFuwzXbkecJrb6IYo1iFb0S9v37754mg==}
engines: {node: '>=0.12'}
@@ -7295,6 +7311,13 @@ snapshots:
'@babel/helper-string-parser': 7.29.7
'@babel/helper-validator-identifier': 7.29.7
'@cantoo/fontkit@2.0.12':
dependencies:
brotli: 1.3.3
dfa: 1.2.0
fflate: 0.8.3
restructure: 3.0.2
'@cantoo/pdf-lib@2.11.1(html-entities@2.6.0)':
dependencies:
culori: 4.0.2
@@ -7461,7 +7484,7 @@ snapshots:
'@eslint/eslintrc@2.1.2':
dependencies:
ajv: 6.12.6
debug: 4.3.4
debug: 4.4.0
espree: 9.6.1
globals: 13.20.0
ignore: 5.2.4
@@ -7477,7 +7500,7 @@ snapshots:
'@humanwhocodes/config-array@0.11.10':
dependencies:
'@humanwhocodes/object-schema': 1.2.1
debug: 4.3.4
debug: 4.4.0
minimatch: 3.1.2
transitivePeerDependencies:
- supports-color
@@ -9018,6 +9041,10 @@ snapshots:
dependencies:
fill-range: 7.1.1
brotli@1.3.3:
dependencies:
base64-js: 1.5.1
browserslist@4.21.9:
dependencies:
caniuse-lite: 1.0.30001504
@@ -9486,6 +9513,8 @@ snapshots:
detect-libc@2.1.2:
optional: true
dfa@1.2.0: {}
diff-sequences@29.4.3: {}
dijkstrajs@1.0.2: {}
@@ -10345,14 +10374,14 @@ snapshots:
dependencies:
'@tootallnate/once': 2.0.0
agent-base: 6.0.2
debug: 4.3.4
debug: 4.4.0
transitivePeerDependencies:
- supports-color
https-proxy-agent@5.0.1:
dependencies:
agent-base: 6.0.2
debug: 4.3.4
debug: 4.4.0
transitivePeerDependencies:
- supports-color
@@ -11690,6 +11719,8 @@ snapshots:
onetime: 5.1.2
signal-exit: 3.0.7
restructure@3.0.2: {}
ret@0.1.15: {}
ret@0.2.2: {}
Binary file not shown.
+23 -2
View File
@@ -1,6 +1,7 @@
<script setup lang="ts">
import { useUserAuth } from '@/composable/user-auth';
import { degrees, PDFDocument, rgb } from '@cantoo/pdf-lib';
import fontkit from '@cantoo/fontkit';
import { getDocument, GlobalWorkerOptions } from 'pdfjs-dist';
import pdfWorkerUrl from 'pdfjs-dist/build/pdf.worker.min.js?url';
import { zipSync } from 'fflate';
@@ -387,16 +388,36 @@ async function doDecrypt() {
statusMsg.value = '解密完成(输出为高清图片版,无密码),点击下载';
}
// 中文字体(Droid Sans Fallback,覆盖 CJK):仅含非西文水印时按需加载,模块级缓存字节
let cjkFontBytes: ArrayBuffer | null = null;
async function getCjkFontBytes(): Promise<ArrayBuffer> {
if (!cjkFontBytes) {
const res = await fetch(`${import.meta.env.BASE_URL}fonts/DroidSansFallback.ttf`);
if (!res.ok) throw new Error('中文字体加载失败');
cjkFontBytes = await res.arrayBuffer();
}
return cjkFontBytes;
}
async function doWatermark() {
if (!wmText.value.trim()) {
throw new Error('请输入水印文字');
}
const src = await loadDoc(files.value[0]);
const pages = src.getPages();
const font = await src.embedFont('Helvetica');
const text = wmText.value.slice(0, 30);
// Helvetica(WinAnsi) 无法编码中文,会被替换成 ?;含非西文字符时嵌入中文字体子集
const needCjk = [...text].some(ch => ch.charCodeAt(0) > 0xff);
let font;
if (needCjk) {
src.registerFontkit(fontkit);
font = await src.embedFont(await getCjkFontBytes(), { subset: true });
}
else {
font = await src.embedFont('Helvetica');
}
for (const page of pages) {
const { width, height } = page.getSize();
const text = wmText.value.slice(0, 30);
const tw = font.widthOfTextAtSize(text, wmSize.value);
page.drawText(text, {
x: (width - tw) / 2,