Fixed Bug - Fail to Export as PDF
This commit is contained in:
@@ -422,8 +422,8 @@ class PDFRenderer:
|
||||
# 格式: <canvas id="chart-N" data-config-id="chart-config-N"></canvas>
|
||||
canvas_pattern = rf'<canvas[^>]+data-config-id="{re.escape(config_id)}"[^>]*></canvas>'
|
||||
|
||||
# 替换canvas为SVG
|
||||
html = re.sub(canvas_pattern, svg_html, html)
|
||||
# 【修复】替换canvas为SVG,使用lambda避免反斜杠转义问题
|
||||
html = re.sub(canvas_pattern, lambda m: svg_html, html)
|
||||
logger.debug(f"已替换图表 {widget_id} 的canvas为SVG")
|
||||
else:
|
||||
logger.warning(f"未找到图表 {widget_id} 对应的配置脚本")
|
||||
@@ -468,7 +468,10 @@ class PDFRenderer:
|
||||
# 暂时使用简单的替换方案
|
||||
# 找到第一个math-block div并替换
|
||||
math_block_pattern = r'<div class="math-block">\$\$[^$]*\$\$</div>'
|
||||
html = re.sub(math_block_pattern, svg_html, html, count=1)
|
||||
# 【修复】转义svg_html中的反斜杠,避免re.sub将其解释为转义序列
|
||||
# 使用re.escape处理替换字符串中的特殊字符
|
||||
escaped_svg_html = svg_html.replace('\\', r'\\')
|
||||
html = re.sub(math_block_pattern, lambda m: svg_html, html, count=1)
|
||||
logger.debug(f"已替换公式 {math_id} 为SVG")
|
||||
|
||||
return html
|
||||
|
||||
70
export_pdf.py
Normal file
70
export_pdf.py
Normal file
@@ -0,0 +1,70 @@
|
||||
#!/usr/bin/env python
|
||||
"""
|
||||
PDF导出脚本
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
# 添加项目路径到sys.path
|
||||
sys.path.insert(0, '/Users/mayiding/Desktop/GitMy/BettaFish')
|
||||
|
||||
def export_pdf(ir_file_path):
|
||||
"""导出PDF"""
|
||||
try:
|
||||
# 读取IR文件
|
||||
print(f"正在读取报告文件: {ir_file_path}")
|
||||
with open(ir_file_path, 'r', encoding='utf-8') as f:
|
||||
document_ir = json.load(f)
|
||||
|
||||
# 导入PDF渲染器
|
||||
from ReportEngine.renderers.pdf_renderer import PDFRenderer
|
||||
|
||||
# 创建PDF渲染器
|
||||
print("正在初始化PDF渲染器...")
|
||||
renderer = PDFRenderer()
|
||||
|
||||
# 生成PDF
|
||||
print("正在生成PDF...")
|
||||
pdf_bytes = renderer.render_to_bytes(document_ir, optimize_layout=True)
|
||||
|
||||
# 确定输出文件名
|
||||
topic = document_ir.get('metadata', {}).get('topic', 'report')
|
||||
output_dir = Path('/Users/mayiding/Desktop/GitMy/BettaFish/final_reports/pdf')
|
||||
output_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
pdf_filename = f"report_{topic}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
|
||||
output_path = output_dir / pdf_filename
|
||||
|
||||
# 保存PDF文件
|
||||
print(f"正在保存PDF到: {output_path}")
|
||||
with open(output_path, 'wb') as f:
|
||||
f.write(pdf_bytes)
|
||||
|
||||
print(f"✅ PDF导出成功!")
|
||||
print(f"文件位置: {output_path}")
|
||||
print(f"文件大小: {len(pdf_bytes) / 1024 / 1024:.2f} MB")
|
||||
|
||||
return str(output_path)
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ PDF导出失败: {str(e)}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return None
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 使用最新的报告文件
|
||||
latest_report = "/Users/mayiding/Desktop/GitMy/BettaFish/final_reports/ir/report_ir_人工智能行情发展走势_20251119_235407.json"
|
||||
|
||||
if os.path.exists(latest_report):
|
||||
print("="*50)
|
||||
print("开始导出PDF")
|
||||
print("="*50)
|
||||
result = export_pdf(latest_report)
|
||||
if result:
|
||||
print(f"\n📄 PDF文件已生成: {result}")
|
||||
else:
|
||||
print(f"❌ 报告文件不存在: {latest_report}")
|
||||
Reference in New Issue
Block a user