Pdf2zh logo

Pdf2zh

Community
haorantang1999
pdf2zh

PDF英文转中文翻译。使用PDFMathTranslate进行PDF文档翻译,保留公式、图表、布局。 当用户提到PDF翻译、pdf2zh、英文PDF转中文、科学文档翻译时触发。

Overview

Publisherhaorantang1999
Repositorycc-pdf2zh-skill
Skill namepdf2zh
Stars
33
Forks
1
Bundled files
17
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 17 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by haorantang1999 on GitHub. Read the source before you install it.

Installation

Install the Pdf2zh AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/haorantang1999/cc-pdf2zh-skill/tree/main
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/haorantang1999/cc-pdf2zh-skill.git \
  .claude/skills/pdf2zh
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Pdf2zh in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Pdf2zh on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Pdf2zh is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

PDF to Chinese Translation Skill

PDF 英→中 翻译,基于 PDFMathTranslate (Apache-2.0)。保留原版公式、图表与排版,输出纯中文与中英双语两个 PDF。

0. 定位本 skill(每次使用前)

安装器会把 skill 装到:

Agent路径
Claude Code~/.claude/skills/pdf2zh
OpenCode~/.config/opencode/skills/pdf2zh
WorkBuddy~/.workbuddy/skills/pdf2zh

若当前环境没有 PDF2ZH_SKILL_DIR 变量,先定位并固定:

bash
# bash(按实际安装位置调整)
SKILL_DIR="$HOME/.claude/skills/pdf2zh"
set -a; source "$SKILL_DIR/.env"; set +a   # 载入 服务/模型/API key/术语 prompt

.env 由 install.py 生成:内含 PDF2ZH_SKILL_DIR、PDF2ZH_SERVICE、 PDF2ZH_MODEL、PDF2ZH_PROMPT 与所选服务的 xxx_API_KEY。 没装过就提示用户先运行 python install.py(见仓库 README)。

Windows 上 run.cmd 加载 .env 的写法有坑(见 §2.7 P0-1)。 如果怀疑配置没生效,先 echo %PDF2ZH_SERVICE% 确认变量确实被读到了。

1. 翻译(一行命令)

bash
"$SKILL_DIR/run.sh" document.pdf -o output/     # bash / Git Bash / macOS
"%SKILL_DIR%\run.cmd" document.pdf -o output\   # Windows cmd / PowerShell

run.sh / run.cmd 自动完成:加载 .env → 按配置选服务与模型 → 注入术语约束 prompt → 调用 pdf2zh(.venv 内的官方包)。

常用选项(直接透传给 pdf2zh):

bash
run.sh document.pdf -o output/        # 指定输出目录
run.sh document.pdf -li en -lo zh     # 源/目标语言(默认已是 en->zh)
run.sh document.pdf --babeldoc         # 用 babeldoc 引擎(公式/版面效果更好,推荐)
run.sh document.pdf -t 6               # 翻译并发线程数
run.sh document.pdf --prompt my.txt   # 指定术语表(换术语表时务必加 --ignore-cache!)
run.sh document.pdf --ignore-cache     # 忽略缓存,全部重新翻译
run.sh --dir /path/to/pdfs/           # 批量翻译整个目录
run.sh -i                             # 启动 GUI 界面

⚠️ -p 1-2(页码范围)在 babeldoc 引擎下会被忽略,照样翻完整本。 想只翻部分页,只能先切出那个范围的 PDF 再翻译。

输出文件(在输出目录,注意中间有 .zh.):

  • <文件名>.zh.mono.pdf — 纯中文版
  • <文件名>.zh.dual.pdf — 中英双语版(左右对照,页宽是源文件的 2 倍)

2. 术语约束(避免 LLM 翻出「法学硕士」)

默认 Google 翻译不支持自定义 prompt,LLM 常被翻成「法学硕士」。两条修复路线:

路线 A:翻译前约束(推荐,仅 LLM 服务)

LLM 服务(deepseek / openai / zhipu / qwen-mt / grok / gemini / silicon / modelscope / groq / openailiked 等)支持 --prompt, run.sh 已自动注入 assets/prompt.txt(内置通用术语表:LLM → 大语言模型 等, 支持 $lang_in / $lang_out / $text 占位符)。

仓库内提供多套现成术语表:

文件适用方向
assets/prompt.txt通用(默认注入)
assets/prompt_llm.txt大语言模型 / 多模态 / 扩散语言模型(dLLM)等方向,约 160 条
assets/prompt_3d.txt3D 视觉 / 新视角合成 / 3DGS 方向
bash
# 换一套术语表(务必同时加 --ignore-cache,见 §2.6)
run.sh paper.pdf -o out/ --babeldoc --prompt "$SKILL_DIR/assets/prompt_llm.txt" --ignore-cache

一个领域配一张表,别去改共享的 assets/prompt.txt。 共享文件被改脏后,所有方向的论文都会带上错误的术语映射 (例如把 Gaussian → 高斯、latent → 潜在表示 塞进一篇讲扩散模型的论文)。 新领域就照着 prompt_llm.txt 的格式另存一份,放到自己的项目目录里。

google / bing / deepl / argos 等机器翻译服务不接受 prompt,路线 A 无效。

路线 B:翻译后修补(所有服务通用)

bash
python "$SKILL_DIR/assets/apply_glossary.py" paper-mono.pdf \
  -g "$SKILL_DIR/assets/glossary.txt" -o paper-mono-fixed.pdf

用 PyMuPDF 定位原词位置、真正擦除原字形(redaction,不是盖白块),再按原 基线写入正确术语,周边排版不变。擦除区宽度按新词实际渲染宽度撑开,中译英 时新词通常更长,否则原词会露出来与新词重叠。字号会按列宽自动微缩以免串栏。

模板 assets/glossary.txt 已预置 LLM 常见词条;长词组放短词组前;字面量匹配。

典型工作流:

bash
"$SKILL_DIR/run.sh" paper.pdf -o out/                 # 1. 翻译
python "$SKILL_DIR/assets/apply_glossary.py" \
  out/paper-mono.pdf -g my-glossary.txt -o out/fixed.pdf   # 2. 修补

2.5 必做:翻译后自检

pdf2zh 不会100% 翻译成功,交付前必须验一遍,否则会把半成品交给用户。

直接用随 skill 提供的检查器(覆盖 9 类已知坑):

bash
python "$SKILL_DIR/assets/qa_check.py" 原.pdf 译文.pdf
python "$SKILL_DIR/assets/qa_check.py" 原.pdf 译文-dual.pdf --dual
python "$SKILL_DIR/assets/qa_check.py" 原.pdf 译文.pdf --proper "dLLM,SEDD,LLaDA"

它会检查:页数 / 页面尺寸 / 图像数 / 逐页 CJK 占比 / 数值保真 / 专名保真 / 漏译块 / 逐字竖排工件 / BabelDOC 推广页脚。退出码 0 = 通过(可能仍有 WARN 要人工确认)。

摘要经常被整块漏译

babeldoc 的版面分析偶尔会把 Abstract 当成独立文本块跳过,表现为:首页标题、 作者、关键词都是中文,唯独摘要整段还是英文。这是已知的高频坑。

手工体检(找出漏译的英文块):

bash
python - <<'PY'
import fitz, re
doc = fitz.open("out/paper-mono.pdf")
for pno in range(doc.page_count):
    for b in doc[pno].get_text("blocks"):
        t = b[4].strip()
        if len(t) < 40: continue
        en = len(re.findall(r"[A-Za-z]{3,}", t))
        cn = sum(1 for c in t if "\u4e00" <= c <= "\u9fff")
        if en >= 12 and cn / (cn + en) < 0.35:
            print(f"p{pno+1}  ratio={cn/(cn+en):.2f}  {t[:70]!r}")
PY

哪些英文残留是正常的,别乱改

图内对话气泡 / 表格数字 / JSON 输出示例 / 参考文献 / 作者名与机构 —— 论文原本就该保留原文。只补真正的正文漏译。

补译整块漏译文本的正确姿势

自己写脚本覆盖时,PyMuPDF 有几个坑(实测于 1.25.x):

  1. page.insert_textbox(fontfile=...) 会静默丢掉全部 CJK 字形,渲染成一串 ?。必须用 TextWriter + fitz.Font,且 origin 要传 fitz.Point:
    python
    tw = fitz.TextWriter(page.rect)
    tw.append(fitz.Point(x, y), line, font=font, fontsize=fs)
    tw.write_text(page, color=(0, 0, 0))
  2. page.draw_rect() 只是在内容流上叠一个白色矩形,底下的原 glyph 依然存在 —— 可选中、可复制、get_text() 仍能提取到。必须先redact 再写字:
    python
    page.add_redact_annot(rect)
    page.apply_redactions(images=fitz.PDF_REDACT_IMAGE_NONE,
                          graphics=fitz.PDF_REDACT_LINE_ART_NONE,
                          text=fitz.PDF_REDACT_TEXT_REMOVE)
    且redact 与写新字必须分两阶段:apply_redactions 会把落在擦除矩形内的 东西一并删掉,同一趟循环里先写字再 apply 会把新字也删没。
  3. doc.save() 在 1.25 里参数是 garbage=,没有 garbage_collection=。
  4. PyMuPDF 判定 redact 相交用的是字体框而非墨迹框 —— 页脚矩形会连带切掉 标题。要用 assets/fix_watermark.py 处理(它把页脚+标题一起擦除,再用原 字体原基线把标题写回去)。
  5. page.get_images(full=True) 会因内联图 xref=0 漏计,统计图像数必须用 page.get_image_info(),否则会误报"图表丢失"。

图内旋转标签会被重排成竖排字符条

图里的竖排边注(Model Architecture / Attention Mask / Query / Inference / Training / Classifier …)在翻译后可能被压平成一列单字,或被拆成多行。 用 assets/fix_figure_labels.py 修:它自动找出源文件里的旋转标签、在译文里 定位对应的工件块,先 redact 真擦除、再把源 PDF 对应区域原样栅格化贴回。

bash
python "$SKILL_DIR/assets/fix_figure_labels.py" 原.pdf 译文.pdf 修好.pdf --apply
python "$SKILL_DIR/assets/fix_figure_labels.py" 原.pdf 对照版.pdf 修好.pdf --dual --apply

2.6 缓存与术语生效(最容易踩的认知坑)

pdf2zh 把每个文本块的译文缓存进 SQLite:~/.cache/pdf2zh/cache.v1.db。 同内容再翻译直接命中缓存、不再调用模型。

坑(已由 patches/repair_env.py 修补):pdf2zh 1.9.x 的 OpenAIlikedTranslator 在 super().__init__() 之后才设 self.prompttext,导致缓存键用的是默认 prompt。表现是:改了术语表重跑,输出纹丝不动,静默复用旧译文。

所以:

换术语表 / 换 prompt / 改译文策略后,必须加 --ignore-cache 全量重跑。

其他与缓存相关的事实:

  • 缓存库是跨论文共享的:翻译别的论文也会往同一个库里写。
  • 模型偶发把整块内容吞掉(尤其整段参考文献被合并成一个大段落送进模型时), 这种损坏会静默进入缓存,之后每次重跑都复用这份坏译文。
  • 审计与修复:
bash
python "$SKILL_DIR/assets/audit_cache.py"                 # 审计全部条目
python "$SKILL_DIR/assets/audit_cache.py" --repair 990 --apply   # 修复指定行

--repair 会把该行按 {{vN}} 拆成单条分别翻译、逐条校验 arXiv ID / [n] 标签 / 小数 token,按「术语 prompt → 简化 prompt → 原文回退」三级降级,再重组写回缓存。

审计判据是关键 token 是否丢失,不是长度比 —— 实测中文译文长度只有英文的 0.17~0.44,用长度比当主判据会误报一大片。

2.7 已知上游缺陷与坑位清单

下列问题在 pdf2zh 1.9.11 / babeldoc 0.2.33 上实测存在。

代码级(由 patches/repair_env.py 自动修补,装完即生效)

级别缺陷症状修法
P0-1run.cmd 用了 for /f "usebackq".env 从未被加载 → 服务静默退回 google、--prompt 丢失、术语全失效,全程不报错去掉 usebackq
P0-2pdf2zh.py 把 --prompt 读两次第二次拿 string.Template 去 open() → ValueError: prompt error.,任务崩isinstance 判断后复用
P0-3babeldoc 用了 np.fromstringnumpy 2 已移除该 API → 版面分析崩溃改 np.frombuffer
P0-4translator.py 缓存键不含 prompt换术语表重跑输出不变,静默复用旧译文super().__init__(..., prompt=prompt)

这四个都在 site-packages 里的第三方包中,每次重装/升级 pdf2zh 都会被覆盖回 原版。install.py 会在装完 venv 后自动跑一次 patches/repair_env.py; 手动升级过引擎的话,记得补跑:

bash
python "$SKILL_DIR/patches/repair_env.py"

机器级(本机 WMI 异常时,由 assets/run_pdf2zh.py 自动探测)

级别缺陷症状修法
P1-1本机 WMI 服务假死(winmgmt 显示 Running,但查询无响应)platform.machine() / platform.platform() 永久挂起 → pdf2zh 卡在 import 阶段(日志 0 字节、CPU=0,看着像"没启动");openai 客户端每次 HTTP 请求也调 platform.platform() → 翻译线程逐个卡死,babeldoc 线程池永远等不到 worker 返回("翻译完了但排版阶段无限等待")启动器先在子进程里探测(默认 8s 超时),确认卡死才让 platform._wmi_query 直接抛 OSError;标准库随即走它自带的回退分支(环境变量 / 注册表),platform() / machine() / processor() 取值仍是真值

该补丁只在探测到 WMI 无响应时才生效,健康机器上完全不做改动。 手动控制:PDF2ZH_WMI_SHIM=1 / 0 强制开 / 关, PDF2ZH_WMI_PROBE_TIMEOUT=8 调整探测超时秒数。 注意:run.sh / run.cmd 走的是 assets/run_pdf2zh.py; 若你绕过入口直接调 pdf2zh.exe,就不会带上这层防护。

行为级(引擎本身的问题,需要人工介入)

现象说明应对
参考文献整段被合并成一个大段落送模型,模型吞条目文献某一条整条丢失assets/audit_cache.py --repair
推理型模型返回 reasoning_content 而 content 为空,或字面量 (empty)译文块退化但不报错审计缓存 + 三级降级重译
模型回显 {{vN}} 占位符重组后条目错配修复时强制占位符序列与原文一致
图内旋转标签被重排成竖排字符条视觉上标签变形assets/fix_figure_labels.py
BabelDOC 注入推广页脚成品里混入第三方宣传语assets/fix_watermark.py
-p 1-2 页码范围被忽略以为只翻 2 页,实际全翻先切 PDF

3. 支持的翻译服务(pdf2zh v1.9+ 官方)

服务名(-s 值)env 变量默认模型说明
deepseekDEEPSEEK_API_KEYdeepseek-chat便宜、中文好,推荐
openaiOPENAI_API_KEYgpt-4o-miniChatGPT,需科学上网
zhipuZHIPU_API_KEYglm-4-flash免费额度大
qwen-mtALI_API_KEYqwen-mt-turbo翻译专用模型,术语最稳
grokGROK_API_KEYgrok-2-1212
geminiGEMINI_API_KEYgemini-1.5-flash
siliconSILICON_API_KEYQwen/Qwen2.5-7B-Instruct硅基流动,国产平替
modelscopeMODELSCOPE_API_KEYQwen/Qwen2.5-32B-Instruct魔搭
groqGROQ_API_KEYllama-3-3-70b-versatile极速
azure-openaiAZURE_OPENAI_*gpt-4o-mini
tencentTENCENTCLOUD_SECRET_ID/SECRET_KEY—腾讯云
deeplDEEPL_AUTH_KEY—机器翻译,无术语控制
ollamaOLLAMA_HOSTgemma2本地模型,0 token 成本
openailikedOPENAILIKED_BASE_URL/API_KEY/MODEL—任意 OpenAI 兼容端点
google / bing / argos无—免费机器翻译,术语不可控

Kimi(Moonshot)官方提供 OpenAI 兼容 API:用 openailiked + OPENAILIKED_BASE_URL=https://api.moonshot.cn/v1。Claude 官方 API 不是 OpenAI 兼容协议,需先搭 OpenAI→Anthropic 兼容网关,再走 openailiked。 install.py 的交互向导已覆盖以上全部选项。

安全:API key 只存在于 skill 目录的 .env(install.py 写入,权限 600, 仓库 .gitignore 已排除)。不要 key 写进 SKILL.md / README / 任何文档或 commit。

4. 配套脚本一览

脚本用途
assets/qa_check.py译文 PDF 一站式验收(9 项检查)
assets/audit_cache.py翻译缓存审计 / 修复
assets/fix_figure_labels.py修复图内旋转标签被重排成竖排
assets/fix_watermark.py去 BabelDOC 推广页脚 + 原样写回标题
assets/apply_glossary.py路线 B:翻译后按术语表修补
assets/run_pdf2zh.py带 WMI 挂死防护的启动器(run.sh / run.cmd 实际调用它)
patches/repair_env.py修补上游 pdf2zh / babeldoc 的 4 个缺陷(幂等)
patches/wmi_shim.py绕开本机 WMI 查询卡死(由启动器自动探测调用)

5. 其他

  • 更换服务:改 .env 里的 PDF2ZH_SERVICE / PDF2ZH_MODEL / xxx_API_KEY, 或重跑 python install.py --skip-venv --skip-copy(只走配置向导)。
  • 升级引擎:python install.py --skip-copy --skip-config 会重装最新 pdf2zh —— 升级后记得补跑 patches/repair_env.py(或直接重跑完整 install)。
  • 想用 conda 代替 uv?官方包在 conda-forge:conda create -n pdf2zh -c conda-forge pdf2zh, 然后把 run.sh 里的 PDF2ZH 指向 $(which pdf2zh) 即可。

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Pdf2zh AI skill do?

PDF英文转中文翻译。使用PDFMathTranslate进行PDF文档翻译,保留公式、图表、布局。 当用户提到PDF翻译、pdf2zh、英文PDF转中文、科学文档翻译时触发。

Why use Pdf2zh on TypingMind?

Because you install it once and use it with any model. Pdf2zh is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Pdf2zh in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/haorantang1999/cc-pdf2zh-skill/tree/main. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Pdf2zh?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Pdf2zh?

As many as you like. As long as a model supports skills, you can use Pdf2zh with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Pdf2zh AI skill free?

Yes. It is published on GitHub by haorantang1999 under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇