← Back to home@2927683690myn-rgb

dsh-md2pdf

DeepSeek Harness 插件:Markdown → A4 PDF,自带精简 reportlab,Android/Termux 可用(无 LibreOffice、无 pillow)

Stars
0
Language
Python
Created
Oct 26, 2026
Updated
Oct 26, 2026

Introduction

dsh-md2pdf

给 DeepSeek Harness 用的 Markdown → PDF 插件:把 .md 转成带中文字体、标题层级、表格、代码块的 A4 PDF。

不依赖 LibreOffice、不联网、不装系统字体 —— 自带一份精简的 reportlab,所以在 Android / Termux 这种「没有 pillow、也没有 LibreOffice」的环境里也能直接跑。

为什么会有这个插件

DSH 运行在 Android 手机上时,pip install reportlab 会去编译 47 MB 的 Pillow 源码包(因为没有现成 wheel),基本必然失败;weasyprint / pandoc / LibreOffice 又都装不上。这里绕开的办法是:

  1. 只装 reportlab,不让 pip 解析 pillow:pip install --no-deps --target ./libs "reportlab==4.2.5";
  2. reportlab 一 import 就会 from PIL import Image,于是手写一个 PIL 桩(libs/PIL/,只声明 open/new/frombytes 等符号,实际被调用才抛异常),reportlab 的纯文字排版路径根本不会碰到它;
  3. 中文字体用 reportlab 内置的 CID 字体 UnicodeCIDFont('STSong-Light'),不读系统字体文件(NotoSansCJK-Regular.ttc / MiSansL3.otf 都是 CFF,reportlab 不能嵌)。

工具

插件注册一个工具:

md_to_pdf(input, output?)

参数类型说明
inputstring,必填源 .md 路径(相对路径按会话工作目录解析)
outputstring,可选输出的 .pdf 路径,默认同名换后缀

返回 { path, bytes, pages }。

支持:#~###### 六级标题、普通段落(自动断行、首行不缩进)、**粗体** / *斜体*、`行内代码`、无序/有序列表、引用块、Gfm 表格(带表头底色、repeatRows=1 跨页重复)、围栏代码块(XPreformatted 等宽保留空格,Paragraph 会把连续空格压掉)、水平线、页码、A4 页边距、中文标题加粗。

安装

cd ~/.dsh/profiles/web

# 1. 放进插件目录
git clone <this-repo> plugins/dsh-md2pdf

# 2. 让 node 能解析到这个包
ln -s ../plugins/dsh-md2pdf node_modules/dsh-md2pdf

# 3. 记在 package.json 的 dependencies 里(可选,但建议)
#    "dsh-md2pdf": "file:./plugins/dsh-md2pdf"

# 4. 启用:在 cordis.patch.yml 末尾加
#    - insert:
#        - id: md2pdf
#          name: dsh-md2pdf

装完热加载即生效,不需要重启 DSH。

仓库里已经带了 libs/(reportlab 4.2.5 + PIL 桩),克隆下来就能用;想自己重建见上面的 --no-deps 命令。

目录

dsh-md2pdf/
├── index.js      # Cordis 插件:注册 md_to_pdf 工具,负责找 python、spawn 脚本
├── md2pdf.py     # 真正的转换器(reportlab,约 5.5 KB)
└── libs/         # vendored reportlab 4.2.5 + 最小 PIL 桩

已知限制

  • 只支持上面列出的 Markdown 子集:没有图片、脚注、数学公式、嵌套引用。
  • 字体只有 STSong-Light 一种(中文正文/标题、英文同样走它),没有等宽字体,代码块靠固定宽度对齐。
  • 依赖本机有 python3;路径按 $DSH_PYTHON → Termux 默认路径 → python3 的顺序探测。

许可

MIT。libs/reportlab 是 ReportLab 的 BSD 授权代码,libs/PIL 是本仓库自带的桩实现。