"""生成用于教程闭环验证的三页PDF业务报告。""" from pathlib import Path import pymupdf ROOT = Path(__file__).resolve().parent OUTPUT_PATH = ROOT / "sample-report.pdf" def add_header(page: pymupdf.Page, title: str, subtitle: str) -> None: """绘制统一页眉,便于观察PDF解析后的页级结构。""" page.draw_rect(pymupdf.Rect(0, 0, 595, 82), color=(0.08, 0.25, 0.43), fill=(0.08, 0.25, 0.43)) page.insert_text((42, 42), title, fontsize=20, color=(1, 1, 1), fontname="helv") page.insert_text((42, 65), subtitle, fontsize=10, color=(0.82, 0.9, 0.97), fontname="helv") def add_footer(page: pymupdf.Page, page_number: int) -> None: page.draw_line((42, 800), (553, 800), color=(0.82, 0.84, 0.87)) page.insert_text((42, 820), "AI Tutorial Pro verification document", fontsize=8, color=(0.4, 0.43, 0.47)) page.insert_text((520, 820), f"Page {page_number}", fontsize=8, color=(0.4, 0.43, 0.47)) def build_pdf(output_path: Path = OUTPUT_PATH) -> Path: """构造包含正文、表格和柱状图的最小可验证PDF。""" document = pymupdf.open() page = document.new_page(width=595, height=842) add_header(page, "North Region Q1 2026 Review", "Management summary and verified financial metrics") page.insert_text((42, 125), "Executive summary", fontsize=15, color=(0.08, 0.25, 0.43), fontname="helv") summary = ( "North Region revenue reached CNY 12.8 million in Q1 2026, up 31% year over year.\n" "Gross margin was 63.5%. Renewal revenue represented 58% of total revenue.\n" "The quarter ended with 186 active enterprise customers and a 94.2% renewal rate." ) page.insert_textbox(pymupdf.Rect(42, 145, 553, 235), summary, fontsize=11, lineheight=1.5, fontname="helv") page.insert_text((42, 280), "Metric", fontsize=11, color=(1, 1, 1), fontname="hebo") page.insert_text((285, 280), "Q1 2026", fontsize=11, color=(1, 1, 1), fontname="hebo") page.insert_text((430, 280), "YoY", fontsize=11, color=(1, 1, 1), fontname="hebo") page.draw_rect(pymupdf.Rect(35, 254, 560, 294), color=(0.13, 0.42, 0.66), fill=(0.13, 0.42, 0.66), overlay=False) rows = [ ("Revenue", "CNY 12.8 million", "+31%"), ("Gross margin", "63.5%", "+4.1 pp"), ("Enterprise customers", "186", "+24%"), ("Renewal rate", "94.2%", "+2.7 pp"), ] y = 326 for index, row in enumerate(rows): if index % 2 == 0: page.draw_rect(pymupdf.Rect(35, y - 22, 560, y + 10), color=(0.94, 0.96, 0.98), fill=(0.94, 0.96, 0.98)) page.insert_text((42, y), row[0], fontsize=10, fontname="helv") page.insert_text((285, y), row[1], fontsize=10, fontname="helv") page.insert_text((430, y), row[2], fontsize=10, fontname="helv") y += 38 page.insert_textbox( pymupdf.Rect(42, 525, 553, 630), "Management conclusion: growth remained healthy, but two operational risks require action: " "GPU capacity pressure during peak hours and delayed data labeling for the retail dataset.", fontsize=11, lineheight=1.5, fontname="helv", ) add_footer(page, 1) page = document.new_page(width=595, height=842) add_header(page, "Product Line Growth", "Year-over-year growth by product line, Q1 2026") page.insert_text((42, 125), "Growth comparison", fontsize=15, color=(0.08, 0.25, 0.43), fontname="helv") chart_left, chart_top, chart_bottom = 80, 185, 570 page.draw_line((chart_left, chart_top), (chart_left, chart_bottom), color=(0.35, 0.38, 0.42), width=1) page.draw_line((chart_left, chart_bottom), (530, chart_bottom), color=(0.35, 0.38, 0.42), width=1) values = [("Vision API", 42, (0.12, 0.55, 0.48)), ("Agent Platform", 28, (0.13, 0.42, 0.66)), ("RAG Suite", 17, (0.93, 0.57, 0.13))] x = 125 for label, value, color in values: height = value * 7 page.draw_rect(pymupdf.Rect(x, chart_bottom - height, x + 82, chart_bottom), color=color, fill=color) page.insert_text((x + 25, chart_bottom - height - 12), f"{value}%", fontsize=12, color=(0.15, 0.17, 0.2), fontname="hebo") page.insert_textbox(pymupdf.Rect(x - 8, chart_bottom + 12, x + 92, chart_bottom + 55), label, fontsize=9, align=1, fontname="helv") x += 145 page.insert_textbox( pymupdf.Rect(42, 670, 553, 745), "The Vision API was the fastest-growing product line at 42%. Agent Platform grew 28%, " "while RAG Suite grew 17%.", fontsize=11, lineheight=1.5, fontname="helv", ) add_footer(page, 2) page = document.new_page(width=595, height=842) add_header(page, "Risk Register and Actions", "Owners, deadlines and measurable mitigation work") page.insert_text((42, 125), "Priority risks", fontsize=15, color=(0.08, 0.25, 0.43), fontname="helv") risks = [ ( "R1 - Peak-hour GPU capacity", "Impact: response latency may exceed the 2-second service target.", "Action: add a second inference pool by 30 April 2026. Owner: Chen Wei.", ), ( "R2 - Retail dataset labeling delay", "Impact: the May model evaluation could slip by two weeks.", "Action: approve a backup labeling vendor by 18 April 2026. Owner: Liu Min.", ), ( "R3 - Customer onboarding documentation", "Impact: support workload is 14% above the quarterly plan.", "Action: publish the revised onboarding guide by 25 April 2026. Owner: Zhang Rui.", ), ] y = 165 for title, impact, action in risks: page.draw_rect(pymupdf.Rect(35, y, 560, y + 145), color=(0.82, 0.85, 0.88), fill=(0.97, 0.98, 0.99)) page.insert_text((52, y + 30), title, fontsize=12, color=(0.08, 0.25, 0.43), fontname="hebo") page.insert_textbox(pymupdf.Rect(52, y + 48, 542, y + 90), impact, fontsize=10, lineheight=1.35, fontname="helv") page.insert_textbox(pymupdf.Rect(52, y + 92, 542, y + 132), action, fontsize=10, lineheight=1.35, fontname="helv") y += 165 add_footer(page, 3) output_path.parent.mkdir(parents=True, exist_ok=True) document.save(output_path, garbage=4, deflate=True) document.close() return output_path if __name__ == "__main__": created = build_pdf() print(f"已生成测试PDF:{created}")