Files
piano-plan/docs/superpowers/specs/2026-04-28-pdf-qrcode-design.md
T

42 lines
1023 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# PDF URL→二维码功能设计
## 需求
导出 PDF 时,如果发现文本内容中有 URL,自动将 URL 转换为二维码,扫描可跳转。
## 规则
- **检测位置**:任意位置(AI报告内容、标题等所有文本)
- **替代方式**:二维码替代原 URL 文字
- **二维码大小**60x60pt(约2.5cm
- **放置位置**:URL 原文字位置
## 技术方案
**新增依赖:**
- `qrcode` Python 包
**修改文件:**
- `app/services/pdf_generator.py`
**实现步骤:**
1. 安装 qrcode 包
2.`generate_pdf()` 中,正则检测 URL`http://``https://` 开头)
3. 检测到 URL 时,生成二维码 PNG 图片
4. 用 ReportLab 的 `Image` flowable 嵌入 PDF,替代原 URL 文字
**二维码生成方式:**
```python
import qrcode
from io import BytesIO
def generate_qr_image(url, size=60):
qr = qrcode.make(url)
buf = BytesIO()
qr.save(buf, format='PNG')
buf.seek(0)
return buf
```
**URL 正则:** `https?://[^\s<>"{}|\\^`\[\]]+`