dsh-pdf2zh: Integrating PDFMathTranslate into DeepSeek Harness

Preface

The design philosophy of DeepSeek Harness (DSH) is “everything is a plugin” — developers can extend the toolset on demand. In model inference scenarios, beyond calling an LLM, you can also expose local tools as model-callable functions. Translating PDF papers is a common need, but traditional approaches either rely on a web frontend or require manually running command-line tools. The dsh-pdf2zh plugin wraps the PDFMathTranslate (pdf2zh) command-line tool into a DSH tool, enabling the model to invoke translation directly through natural language.

What Is This

dsh-pdf2zh is a DeepSeek Harness Host plugin developed by maintainer yeahlvy, categorized under model inference. It integrates PDFMathTranslate’s functionality into the DSH environment by exposing a model tool named pdf2zh. The plugin itself consists of a single ESM JavaScript file index.js of approximately 500 lines, with no third-party npm dependencies and no Web UI.

Core Features

The plugin provides a pdf2zh tool with the following capabilities:

  • Translates PDF papers via pdf2zh, preserving formulas, figures, table of contents, and layout
  • Produces two PDFs simultaneously: a pure translation version (*-mono.pdf) and a bilingual side-by-side version (*-dual.pdf, with original and translated pages alternating)
  • Automatically locates the local pdf2zh runtime; supports explicit specification in configuration
  • Adapts to the DSH sandbox environment by redirecting cache and temp directories via environment variables, preventing pdf2zh from failing at the import stage due to writing to ~/.cache
  • Accepts a local PDF file or an online URL as input
  • Configurable parameters include source language, target language, translation service, page range, thread count, translation kernel, and output directory

Installation & Setup

1. Install the pdf2zh Backend

Python 3.11 or 3.12 is required. The current pdf2zh combination on PyPI has an import bug as of 2026-11, so two dependencies must be pinned simultaneously:

python -m venv <workspace>\.pdf2zh\runtime
<workspace>\.pdf2zh\runtime\Scripts\python.exe -m pip install pdf2zh "tencentcloud-sdk-python-tmt==3.0.1419" "bitstring==4.3.1"

The first translation will automatically download the layout model (~75 MB) and CJK fonts (~14 MB); subsequent runs use the local cache. On macOS/Linux, replace Scripts\python.exe with bin/python.

2. Install This Plugin

git clone https://github.com/yeahlvy/dsh-pdf2zh.git

Then in DSH (or simply tell the agent):

plugin_manager action=install_bundle target=<absolute-path>/dsh-pdf2zh

After installation, the pdf2zh tool will appear in the model tool list. The plugin’s configuration line takes effect through the bundle’s cordis.patch.yml; to change the configuration later, modify the pdf2zh line in the profile-level patch (%USERPROFILE%\.dsh\profiles\<profile>\cordis.patch.yml) — plugin upgrades will not affect it.

After modifying the plugin source code, a profile reload is required (toggle the bundle off and on in Plugin Manager, or restart DSH) for the new code to take effect.

Configuration

The plugin is configured via the pdf2zh line. Fields include:

Field Default Description
pythonPath '' Absolute path to the Python interpreter with pdf2zh installed (the venv’s Scripts\python.exe). If left empty, it auto-detects <session-workspace>/.pdf2zh/runtime, .venv, venv, and PATH.
pdf2zhPath '' Can also point directly to the pdf2zh console script; takes priority over pythonPath.
hfEndpoint '' When set, injects HF_ENDPOINT into the child process. Note: pdf2zh 1.9.x downloads models directly via httpx through babeldoc, so this variable has no actual effect — leave it empty.
defaultLangOut zh Default target language.
defaultService bing Default translation service. google may be rate-limited with 429 and retry infinitely on some networks; bing works without an API key.
timeoutMinutes 30 Timeout per translation; on timeout, the entire process tree is terminated.
outputDir '' Default output directory. If empty, outputs to pdf2zh-out/ in the same directory as the input PDF; for online links, outputs to the session workspace pdf2zh-out/.
cacheDir '' pdf2zh/model cache root directory. If empty, uses <session-work-dir>/.pdf2zh-cache; must be within the workspace.
extraArgs [] Additional arguments appended to pdf2zh.

Tool Parameters

When invoking the pdf2zh tool, the following parameters can be passed:

Parameter Required Description
file ✅ Local PDF path (workspace-relative or absolute, must end in .pdf) or an http(s) URL.
langIn Source language (-li), e.g., en, zh, ja, auto.
langOut Target language (-lo); defaults to defaultLangOut.
service Translation service (-s): bing, google, deepl, openai, ollama, etc.
pages Page range (-p), e.g., 1, 1-3.
threads Number of concurrent threads (-t).
mode fast (default) or babeldoc (experimental --babeldoc backend). Note: pdf2zh 1.9.11 does not have --ultrafast/--mode precise.
outputDir Output directory; relative paths are resolved against the session working directory.
ignoreCache --ignore-cache.
compatible --compatible compatibility mode.

Relative paths are resolved against the current session working directory. The return value includes monoPath, dualPath, outputDir, elapsed time, and the tail of the log. After translation completes, use the present tool to display the PDF.

Implementation Highlights

  1. Runtime Discovery: Explicit configuration → local convention directories (.pdf2zh/runtime, .venv, venv) → PATH; if not found, returns an error with installation instructions instead of failing silently.
  2. DSH Sandbox Adaptation: pdf2zh writes to ~/.cache/pdf2zh at import time, and babeldoc writes to ~/.cache/babeldoc, but the sandbox only allows writes within the workspace. The plugin sets the following for the child process:
    - USERPROFILE/HOME → <cacheDir>/home (on Windows, os.path.expanduser("~") only recognizes USERPROFILE);
    - XDG_CACHE_HOME/HF_HOME/TORCH_HOME/MPLCONFIGDIR → corresponding directories under the cache root;
    - TMP/TEMP → <cacheDir>/tmp (otherwise Python’s tempfile falls back to cwd, and pdf2zh cleans up input files under temp on success, which could accidentally delete workspace PDFs);
    - PYTHONIOENCODING=utf-8 to avoid garbled CJK log output.
  3. Timeout & Cancellation: The tool times itself and uses taskkill /T /F to terminate the process tree on Windows; exec.signal cancellation also kills the child process.
  4. Output Detection: Success is determined by exit code + presence of <outDir>/<stem>-mono.pdf/-dual.pdf; for online links, pdf2zh may rename the downloaded file to a temp name, so a suffix-based fallback search is used.
  5. Plain-Text Output: output.render only renders results as a text card (paths, elapsed time, log tail); the actual file is handled by present.

Use Cases & Caveats

Suitable for scenarios requiring PDF paper translation into Chinese or other languages, especially for researchers, students, or technical documentation translators. The plugin runs with the permissions of the current DSH process; review the source code and license before installing.

Known limitations:

  • PDF only; scanned/image-only PDFs depend on pdf2zh’s OCR capability.
  • Version differences in pdf2zh render some upstream parameters unavailable.
  • For online links, the output file names are temporary names generated by pdf2zh (e.g., tmpXXXX-mono.pdf).
  • The first run requires internet access to download models and fonts.
  • Translation quality and terminology are determined by the translation service; the plugin performs no post-processing.

Conclusion

dsh-pdf2zh enables the model to directly invoke a PDF translation tool, integrating PDFMathTranslate’s capabilities into the DSH ecosystem. Through sandbox adaptation, runtime discovery, and output detection mechanisms, it transforms a command-line tool into a model-callable function. For more information, visit the plugin catalog page or the GitHub repository.

羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

Xiaoye