md2pdf添加图片功能支持
This commit is contained in:
@@ -16,12 +16,15 @@ output_schema:
|
|||||||
description: 生成的 PDF 文件路径
|
description: 生成的 PDF 文件路径
|
||||||
---
|
---
|
||||||
|
|
||||||
## 使用说明
|
## 功能说明
|
||||||
- 该技能使用 `reportlab` 将 Markdown 转换为 PDF。
|
- 该技能使用 `reportlab` 将 Markdown 转换为 PDF。
|
||||||
- 支持标题、加粗、斜体、列表、引用、分隔线等常用 Markdown 语法
|
- 支持标题、加粗、斜体、列表、引用、分隔线、图片等常用 Markdown 语法
|
||||||
|
- 图片:`` 独立成行时嵌入 PDF(居中,超宽时等比缩到版心宽);
|
||||||
|
相对路径按 **Markdown 文件所在目录** 解析(如任务目录里的图表),也支持绝对路径与 http(s) 图片;
|
||||||
|
图片加载失败退回 alt 文本占位,不会静默丢失;表格语法暂不支持
|
||||||
- 自动处理中文字体
|
- 自动处理中文字体
|
||||||
|
|
||||||
## 使用说明
|
## 调用方式
|
||||||
通过 `run_skill` 调用,参数:
|
通过 `run_skill` 调用,参数:
|
||||||
- `input`:Markdown 文件路径(必填)
|
- `input`:Markdown 文件路径(必填)
|
||||||
- `output`:PDF 输出路径(可选,默认与输入同名)
|
- `output`:PDF 输出路径(可选,默认与输入同名)
|
||||||
|
|||||||
+73
-6
@@ -5,18 +5,21 @@ md2pdf 技能主入口:将 Markdown 转换为 PDF(支持中文和常用格
|
|||||||
依赖 install.sh 预先准备好中文字体(fonts/cjk.ttf),本脚本只负责注册和转换。
|
依赖 install.sh 预先准备好中文字体(fonts/cjk.ttf),本脚本只负责注册和转换。
|
||||||
"""
|
"""
|
||||||
import sys
|
import sys
|
||||||
|
import io
|
||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
|
import urllib.request
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from reportlab.lib.pagesizes import A4
|
from reportlab.lib.pagesizes import A4
|
||||||
from reportlab.lib.units import mm
|
from reportlab.lib.units import mm
|
||||||
from reportlab.lib.styles import ParagraphStyle
|
from reportlab.lib.styles import ParagraphStyle
|
||||||
from reportlab.lib.enums import TA_LEFT
|
from reportlab.lib.enums import TA_LEFT
|
||||||
|
from reportlab.lib.utils import ImageReader
|
||||||
from reportlab.pdfbase import pdfmetrics
|
from reportlab.pdfbase import pdfmetrics
|
||||||
from reportlab.pdfbase.ttfonts import TTFont
|
from reportlab.pdfbase.ttfonts import TTFont
|
||||||
from reportlab.platypus import (
|
from reportlab.platypus import (
|
||||||
SimpleDocTemplate, Paragraph, Spacer, ListFlowable, ListItem, HRFlowable
|
SimpleDocTemplate, Paragraph, Spacer, ListFlowable, ListItem, HRFlowable, Image
|
||||||
)
|
)
|
||||||
|
|
||||||
# ---------- 字体注册 ----------
|
# ---------- 字体注册 ----------
|
||||||
@@ -115,13 +118,16 @@ def escape_xml(text):
|
|||||||
|
|
||||||
def parse_inline(text):
|
def parse_inline(text):
|
||||||
"""处理行内格式:加粗、斜体、行内代码、链接。入参必须是 escape_xml 之后的文本。"""
|
"""处理行内格式:加粗、斜体、行内代码、链接。入参必须是 escape_xml 之后的文本。"""
|
||||||
# 行内代码 `code`:先占位保护,避免其中的 * [ ] 被后续规则误伤
|
# 行内代码 `code`:先占位保护,避免其中的 * [ ] 被后续规则误伤
|
||||||
code_spans = []
|
code_spans = []
|
||||||
def _stash(m):
|
def _stash(m):
|
||||||
code_spans.append(m.group(1))
|
code_spans.append(m.group(1))
|
||||||
return '\x00%d\x00' % (len(code_spans) - 1)
|
return '\x00%d\x00' % (len(code_spans) - 1)
|
||||||
text = re.sub(r'`(.+?)`', _stash, text)
|
text = re.sub(r'`(.+?)`', _stash, text)
|
||||||
# 链接 [text](url) → reportlab <link>(可点击;转义后 URL 里的 & 已是 &,属性内合法)
|
# 行内图片 :段落里无法嵌图,降级为 alt 文本;必须在链接规则之前,
|
||||||
|
# 否则 [alt](src) 部分会被下面的链接规则改写成可点链接,图片变成“!+链接”残骸
|
||||||
|
text = re.sub(IMG_MD_RE, lambda m: m.group(1) or '[图片]', text)
|
||||||
|
# 链接 [text](url) → reportlab <link>(可点击;转义后 URL 里的 & 已是 &,属性内合法)
|
||||||
text = re.sub(r'\[([^\]]+)\]\(([^)\s]+)\)', r'<link href="\2" color="#3964FE">\1</link>', text)
|
text = re.sub(r'\[([^\]]+)\]\(([^)\s]+)\)', r'<link href="\2" color="#3964FE">\1</link>', text)
|
||||||
# 加粗 **text**
|
# 加粗 **text**
|
||||||
text = re.sub(r'\*\*(.+?)\*\*', r'<b>\1</b>', text)
|
text = re.sub(r'\*\*(.+?)\*\*', r'<b>\1</b>', text)
|
||||||
@@ -141,9 +147,57 @@ def is_ordered_list(line):
|
|||||||
"""判断是否为有序列表行"""
|
"""判断是否为有序列表行"""
|
||||||
return re.match(r'^\d+\.\s+', line)
|
return re.match(r'^\d+\.\s+', line)
|
||||||
|
|
||||||
|
# ---------- 图片 ----------
|
||||||
|
# Markdown 图片语法: 或 
|
||||||
|
IMG_MD_RE = re.compile(r'!\[([^\]]*)\]\(\s*([^)\s]+)(?:\s+"[^"]*")?\s*\)')
|
||||||
|
|
||||||
|
def load_image_source(src, md_dir):
|
||||||
|
"""加载 md 引用的图片,返回可交给 ImageReader 的对象(路径或 BytesIO),失败返回 None。
|
||||||
|
相对路径按 **md 文件所在目录** 解析:技能子进程的 cwd 是技能目录而非任务目录,
|
||||||
|
不能直接 open 相对路径;另外兼容绝对路径与 http(s) 远程图。"""
|
||||||
|
if re.match(r'^https?://', src):
|
||||||
|
try:
|
||||||
|
req = urllib.request.Request(src, headers={'User-Agent': 'Mozilla/5.0'})
|
||||||
|
with urllib.request.urlopen(req, timeout=15) as resp:
|
||||||
|
return io.BytesIO(resp.read())
|
||||||
|
except Exception as e:
|
||||||
|
print(f"图片下载失败:{src}({e})", file=sys.stderr)
|
||||||
|
return None
|
||||||
|
candidates = [Path(src)] if Path(src).is_absolute() else [md_dir / src, Path.cwd() / src]
|
||||||
|
for c in candidates:
|
||||||
|
try:
|
||||||
|
if c.is_file():
|
||||||
|
return str(c)
|
||||||
|
except OSError:
|
||||||
|
continue
|
||||||
|
print(f"图片未找到:{src}(在 {md_dir} 下未命中)", file=sys.stderr)
|
||||||
|
return None
|
||||||
|
|
||||||
|
def image_flowable(src, md_dir, max_width):
|
||||||
|
"""Markdown 图片 → platypus Image(居中,超宽时等比缩到版心宽);失败返回 None 由调用方文字兜底"""
|
||||||
|
obj = load_image_source(src, md_dir)
|
||||||
|
if obj is None:
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
iw, ih = ImageReader(obj).getSize()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"图片格式不支持:{src}({e})", file=sys.stderr)
|
||||||
|
return None
|
||||||
|
if not iw or not ih:
|
||||||
|
return None
|
||||||
|
if isinstance(obj, io.BytesIO):
|
||||||
|
obj.seek(0) # getSize 读过字节,绘制前要回到开头
|
||||||
|
# 像素按 96dpi 换算磅值;小图不放大,大图缩到版心宽
|
||||||
|
w = min(iw * 72.0 / 96.0, max_width)
|
||||||
|
h = ih * (w / iw)
|
||||||
|
img = Image(obj, width=w, height=h)
|
||||||
|
img.hAlign = 'CENTER'
|
||||||
|
return img
|
||||||
|
|
||||||
# ---------- Markdown 解析主函数 ----------
|
# ---------- Markdown 解析主函数 ----------
|
||||||
def parse_markdown(md_text, styles):
|
def parse_markdown(md_text, styles, md_dir=None, max_width=170 * mm):
|
||||||
"""解析 Markdown 文本,返回 platypus 故事列表"""
|
"""解析 Markdown 文本,返回 platypus 故事列表"""
|
||||||
|
md_dir = md_dir or Path.cwd()
|
||||||
story = []
|
story = []
|
||||||
lines = md_text.split('\n')
|
lines = md_text.split('\n')
|
||||||
current_list = [] # 当前列表项
|
current_list = [] # 当前列表项
|
||||||
@@ -179,6 +233,19 @@ def parse_markdown(md_text, styles):
|
|||||||
))
|
))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# 独立成行的图片: → 嵌入 PDF;加载失败退回 alt/路径文本,不丢行
|
||||||
|
m_img = IMG_MD_RE.fullmatch(line)
|
||||||
|
if m_img:
|
||||||
|
flush_list()
|
||||||
|
alt, src = m_img.group(1), m_img.group(2)
|
||||||
|
img = image_flowable(src, md_dir, max_width)
|
||||||
|
if img is not None:
|
||||||
|
story.append(img)
|
||||||
|
else:
|
||||||
|
label = alt or src
|
||||||
|
story.append(Paragraph(render_inline('[图片缺失:' + label + ']'), styles["body"]))
|
||||||
|
continue
|
||||||
|
|
||||||
# 标题
|
# 标题
|
||||||
if line.startswith('# '):
|
if line.startswith('# '):
|
||||||
flush_list()
|
flush_list()
|
||||||
@@ -234,8 +301,8 @@ def markdown_to_pdf(md_path, pdf_path):
|
|||||||
# 构建样式
|
# 构建样式
|
||||||
styles = build_styles(normal_font, bold_font)
|
styles = build_styles(normal_font, bold_font)
|
||||||
|
|
||||||
# 解析 Markdown
|
# 解析 Markdown(图片相对路径按 md 所在目录解析;版心宽 = A4 宽 - 左右边距)
|
||||||
story = parse_markdown(md_text, styles)
|
story = parse_markdown(md_text, styles, md_dir=Path(md_path).resolve().parent, max_width=A4[0] - 40 * mm)
|
||||||
|
|
||||||
# 创建 PDF
|
# 创建 PDF
|
||||||
doc = SimpleDocTemplate(
|
doc = SimpleDocTemplate(
|
||||||
|
|||||||
Reference in New Issue
Block a user