feat: add watermark to generated PDF (仅供学习参考, diagonal, semi-transparent)
This commit is contained in:
@@ -11,6 +11,7 @@ from reportlab.platypus import (
|
||||
from reportlab.pdfbase import pdfmetrics
|
||||
from reportlab.pdfbase.ttfonts import TTFont
|
||||
from reportlab.lib.enums import TA_CENTER, TA_LEFT
|
||||
from reportlab.pdfgen import canvas as pdfcanvas
|
||||
|
||||
# 注册中文字体
|
||||
# Windows 和 Linux 使用不同路径
|
||||
@@ -269,5 +270,23 @@ def generate_pdf(plan_id, student_name, content, output_dir, rendered_report=Non
|
||||
elif line:
|
||||
pdf.add_paragraph(line)
|
||||
|
||||
doc.build(pdf.elements)
|
||||
# 水印函数(每页都绘制)
|
||||
def draw_watermark(c, doc):
|
||||
if not CHINESE_FONT_OK:
|
||||
return
|
||||
c.saveState()
|
||||
try:
|
||||
c.setFont("Chinese", 14)
|
||||
# 浅灰色半透明
|
||||
c.setFillColor(colors.Color(0.6, 0.6, 0.6, alpha=0.25))
|
||||
# 旋转45度
|
||||
c.translate(A4[0]/2, A4[1]/2)
|
||||
c.rotate(45)
|
||||
# 绘制水印文字(居中)
|
||||
c.drawCentredString(0, 0, "仅供学习参考")
|
||||
except Exception:
|
||||
pass # 字体问题则跳过水印
|
||||
c.restoreState()
|
||||
|
||||
doc.build(pdf.elements, onFirstPage=draw_watermark, onLaterPages=draw_watermark)
|
||||
return output_path
|
||||
Reference in New Issue
Block a user