feat: add configurable watermark text in API settings

This commit is contained in:
hmo
2026-04-27 20:14:17 +08:00
parent 1c9d539653
commit c8d77187d1
3 changed files with 13 additions and 4 deletions
+1
View File
@@ -660,6 +660,7 @@ def export_pdf(plan_id):
content=content, content=content,
output_dir=current_app.config["PDF_OUTPUT_DIR"], output_dir=current_app.config["PDF_OUTPUT_DIR"],
rendered_report=rendered_report, # 传递渲染后的报告 rendered_report=rendered_report, # 传递渲染后的报告
watermark_text=api_config.get("watermark_text"),
) )
return send_file( return send_file(
+3 -3
View File
@@ -192,7 +192,7 @@ class PianoPDF:
self.elements.append(Spacer(1, 5*mm)) self.elements.append(Spacer(1, 5*mm))
def generate_pdf(plan_id, student_name, content, output_dir, rendered_report=None): def generate_pdf(plan_id, student_name, content, output_dir, rendered_report=None, watermark_text=None):
"""生成PDF文件""" """生成PDF文件"""
os.makedirs(output_dir, exist_ok=True) os.makedirs(output_dir, exist_ok=True)
output_path = os.path.join(output_dir, f"plan_{plan_id}.pdf") output_path = os.path.join(output_dir, f"plan_{plan_id}.pdf")
@@ -272,7 +272,7 @@ def generate_pdf(plan_id, student_name, content, output_dir, rendered_report=Non
# 水印函数(每页都绘制) # 水印函数(每页都绘制)
def draw_watermark(c, doc): def draw_watermark(c, doc):
if not CHINESE_FONT_OK: if not watermark_text or not CHINESE_FONT_OK:
return return
c.saveState() c.saveState()
try: try:
@@ -283,7 +283,7 @@ def generate_pdf(plan_id, student_name, content, output_dir, rendered_report=Non
c.translate(A4[0]/2, A4[1]/2) c.translate(A4[0]/2, A4[1]/2)
c.rotate(45) c.rotate(45)
# 绘制水印文字(居中) # 绘制水印文字(居中)
c.drawCentredString(0, 0, "仅供学习参考") c.drawCentredString(0, 0, watermark_text)
except Exception: except Exception:
pass # 字体问题则跳过水印 pass # 字体问题则跳过水印
c.restoreState() c.restoreState()
+9 -1
View File
@@ -57,6 +57,12 @@
<small class="text-muted">控制输出的随机性,值越大越有创造性</small> <small class="text-muted">控制输出的随机性,值越大越有创造性</small>
</div> </div>
<div class="mb-3">
<label class="form-label">PDF水印文本</label>
<input type="text" class="form-control" id="watermarkText" placeholder="留空则不显示水印">
<small class="text-muted">PDF每页中央显示的斜向水印文字</small>
</div>
<div class="d-flex gap-2"> <div class="d-flex gap-2">
<button type="button" class="btn btn-primary" onclick="saveApiConfig()"> <button type="button" class="btn btn-primary" onclick="saveApiConfig()">
<i class="bi bi-save"></i> 保存配置 <i class="bi bi-save"></i> 保存配置
@@ -171,7 +177,8 @@ async function saveApiConfig(silent = false, overrideProvider = null) {
model: document.getElementById('apiModel').value, model: document.getElementById('apiModel').value,
api_key: document.getElementById('apiKey').value, api_key: document.getElementById('apiKey').value,
base_url: document.getElementById('apiEndpoint').value, base_url: document.getElementById('apiEndpoint').value,
temperature: parseFloat(document.getElementById('apiTemperature').value) temperature: parseFloat(document.getElementById('apiTemperature').value),
watermark_text: document.getElementById('watermarkText').value.trim()
}; };
const r = await fetch('/api/config', { const r = await fetch('/api/config', {
@@ -199,6 +206,7 @@ async function loadApiConfig() {
document.getElementById('apiKey').value = config.api_key || ''; document.getElementById('apiKey').value = config.api_key || '';
document.getElementById('apiEndpoint').value = config.base_url || ''; document.getElementById('apiEndpoint').value = config.base_url || '';
document.getElementById('apiTemperature').value = config.temperature || 0.7; document.getElementById('apiTemperature').value = config.temperature || 0.7;
document.getElementById('watermarkText').value = config.watermark_text || '';
if (config.api_key_preview) { if (config.api_key_preview) {
document.getElementById('apiKeyPreview').textContent = '当前: ' + config.api_key_preview; document.getElementById('apiKeyPreview').textContent = '当前: ' + config.api_key_preview;