File size: 5,399 Bytes
b20698b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""
PDF Report Generator for Farmer.Chat
Exports query results as downloadable PDF
"""

from reportlab.lib.pagesizes import letter, A4
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, PageBreak
from reportlab.lib import colors
from reportlab.lib.enums import TA_CENTER, TA_LEFT
from datetime import datetime
import json
import os
from typing import Dict, Any


def generate_pdf_report(
    query: str,
    advice: str,
    data: Dict[str, Any],
    location: Dict[str, Any]
) -> str:
    """
    Generate PDF report from query results
    
    Args:
        query: Farmer's question
        advice: Generated advice
        data: Compiled data from MCP servers
        location: Location information
    
    Returns:
        str: Path to generated PDF file
    """
    # Create output directory
    output_dir = "./pdf_reports"
    os.makedirs(output_dir, exist_ok=True)
    
    # Generate filename
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    filename = f"farmer_chat_report_{timestamp}.pdf"
    filepath = os.path.join(output_dir, filename)
    
    # Create PDF
    doc = SimpleDocTemplate(filepath, pagesize=letter)
    story = []
    styles = getSampleStyleSheet()
    
    # Custom styles
    title_style = ParagraphStyle(
        'CustomTitle',
        parent=styles['Heading1'],
        fontSize=24,
        textColor=colors.HexColor('#2E7D32'),
        spaceAfter=30,
        alignment=TA_CENTER
    )
    
    heading_style = ParagraphStyle(
        'CustomHeading',
        parent=styles['Heading2'],
        fontSize=16,
        textColor=colors.HexColor('#1B5E20'),
        spaceAfter=12,
        spaceBefore=20
    )
    
    # Title
    story.append(Paragraph("🌾 Farmer.Chat Report", title_style))
    story.append(Spacer(1, 0.2*inch))
    
    # Metadata section
    metadata_data = [
        ["Report Generated:", datetime.now().strftime("%B %d, %Y at %I:%M %p")],
        ["Location:", f"{location['name']}"],
        ["Coordinates:", f"{location['lat']}°N, {location['lon']}°E"],
        ["Data Sources:", f"{len(data.get('successful_servers', []))} MCP Servers"]
    ]
    
    metadata_table = Table(metadata_data, colWidths=[2*inch, 4*inch])
    metadata_table.setStyle(TableStyle([
        ('BACKGROUND', (0, 0), (0, -1), colors.HexColor('#E8F5E9')),
        ('TEXTCOLOR', (0, 0), (-1, -1), colors.black),
        ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
        ('FONTNAME', (0, 0), (0, -1), 'Helvetica-Bold'),
        ('FONTNAME', (1, 0), (1, -1), 'Helvetica'),
        ('FONTSIZE', (0, 0), (-1, -1), 10),
        ('BOTTOMPADDING', (0, 0), (-1, -1), 8),
        ('GRID', (0, 0), (-1, -1), 1, colors.grey)
    ]))
    
    story.append(metadata_table)
    story.append(Spacer(1, 0.3*inch))
    
    # Query section
    story.append(Paragraph("Your Query", heading_style))
    story.append(Paragraph(query, styles['Normal']))
    story.append(Spacer(1, 0.2*inch))
    
    # Advice section
    story.append(Paragraph("Recommendations", heading_style))
    
    # Split advice into paragraphs
    advice_paragraphs = advice.split('\n\n')
    for para in advice_paragraphs:
        if para.strip():
            story.append(Paragraph(para.strip(), styles['Normal']))
            story.append(Spacer(1, 0.1*inch))
    
    story.append(Spacer(1, 0.2*inch))
    
    # Data section
    story.append(Paragraph("Data Sources", heading_style))
    
    compiled_data = data.get('data', {})
    
    for server_name, server_data in compiled_data.items():
        # Server heading
        server_title = server_name.replace('_', ' ').title()
        story.append(Paragraph(f"<b>{server_title}</b>", styles['Normal']))
        story.append(Spacer(1, 0.05*inch))
        
        # Server data table
        server_items = []
        if isinstance(server_data, dict):
            for key, value in server_data.items():
                if isinstance(value, (str, int, float)):
                    display_key = key.replace('_', ' ').title()
                    server_items.append([display_key, str(value)])
        
        if server_items:
            data_table = Table(server_items, colWidths=[2.5*inch, 3.5*inch])
            data_table.setStyle(TableStyle([
                ('BACKGROUND', (0, 0), (0, -1), colors.HexColor('#F1F8E9')),
                ('TEXTCOLOR', (0, 0), (-1, -1), colors.black),
                ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
                ('FONTNAME', (0, 0), (0, -1), 'Helvetica'),
                ('FONTNAME', (1, 0), (1, -1), 'Helvetica'),
                ('FONTSIZE', (0, 0), (-1, -1), 9),
                ('BOTTOMPADDING', (0, 0), (-1, -1), 6),
                ('GRID', (0, 0), (-1, -1), 0.5, colors.grey)
            ]))
            story.append(data_table)
            story.append(Spacer(1, 0.15*inch))
    
    # Footer
    story.append(Spacer(1, 0.3*inch))
    footer_style = ParagraphStyle(
        'Footer',
        parent=styles['Normal'],
        fontSize=9,
        textColor=colors.grey,
        alignment=TA_CENTER
    )
    story.append(Paragraph("Generated by Farmer.Chat - Agricultural Intelligence System", footer_style))
    story.append(Paragraph("Powered by Multi-stage MCP Pipeline", footer_style))
    
    # Build PDF
    doc.build(story)
    
    return filepath