File size: 18,721 Bytes
a6e6a7c
fef3466
 
a6e6a7c
1539a3f
 
 
a6e6a7c
58b47de
fef3466
1539a3f
 
a6e6a7c
 
 
1539a3f
 
 
 
 
 
 
 
a6e6a7c
 
1539a3f
fef3466
1539a3f
 
 
 
 
a6e6a7c
1539a3f
 
 
a6e6a7c
1539a3f
 
 
 
 
 
 
fef3466
1539a3f
 
 
 
 
 
 
fef3466
 
1539a3f
 
 
 
 
 
 
fef3466
 
1539a3f
 
 
 
 
 
 
 
 
fef3466
1539a3f
 
 
 
 
 
 
 
 
 
 
 
 
fef3466
1539a3f
 
 
 
 
 
 
79d3c9c
1539a3f
 
 
 
 
 
fef3466
1539a3f
 
 
 
 
 
fef3466
1539a3f
 
 
 
 
 
79d3c9c
1539a3f
 
 
 
fef3466
1539a3f
79d3c9c
1539a3f
 
 
 
 
 
 
 
79d3c9c
1539a3f
 
 
 
 
 
 
 
79d3c9c
1539a3f
 
 
 
 
 
 
58b47de
79d3c9c
58b47de
 
 
 
 
 
 
 
fef3466
58b47de
 
 
 
 
 
 
 
fef3466
58b47de
 
 
 
 
 
 
 
 
 
 
 
1539a3f
 
 
 
 
 
fef3466
1539a3f
 
 
79d3c9c
1539a3f
 
 
79d3c9c
1539a3f
 
 
 
79d3c9c
1539a3f
 
 
 
 
fef3466
1539a3f
 
 
 
fef3466
1539a3f
 
 
 
 
 
 
fef3466
79d3c9c
1539a3f
 
 
fef3466
1539a3f
 
58b47de
 
 
 
 
 
 
 
1539a3f
79d3c9c
1539a3f
63d66b6
1539a3f
 
 
fef3466
1539a3f
 
58b47de
 
 
1539a3f
 
 
 
58b47de
 
 
 
 
 
 
 
 
 
 
 
1539a3f
79d3c9c
1539a3f
 
fef3466
1539a3f
fef3466
1539a3f
 
fef3466
1539a3f
 
58b47de
 
 
 
 
 
 
 
 
 
fef3466
58b47de
 
1539a3f
 
 
 
 
 
 
 
 
fef3466
1539a3f
 
fef3466
1539a3f
 
 
 
fef3466
1539a3f
 
 
 
 
 
fef3466
79d3c9c
1539a3f
 
 
 
 
 
58b47de
 
 
 
 
 
 
 
1539a3f
 
 
 
58b47de
c2a315b
 
 
 
 
 
 
 
 
 
 
 
1539a3f
 
 
c2a315b
1539a3f
c2a315b
 
 
 
58b47de
 
 
 
c2a315b
 
 
 
 
 
 
 
 
 
 
 
 
 
1539a3f
 
58b47de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fef3466
1539a3f
c2a315b
58b47de
c2a315b
 
 
 
 
fef3466
c2a315b
 
 
79d3c9c
c2a315b
 
 
fef3466
 
c2a315b
 
 
 
 
 
 
 
58b47de
 
 
 
fef3466
58b47de
 
 
79d3c9c
58b47de
 
 
 
 
 
 
 
 
fef3466
58b47de
 
1539a3f
 
fef3466
 
 
 
 
 
 
 
 
1539a3f
 
 
 
fef3466
1539a3f
 
 
 
 
a6e6a7c
 
1539a3f
 
a6e6a7c
1539a3f
 
 
 
 
a6e6a7c
1539a3f
 
 
 
 
 
 
 
 
 
a6e6a7c
1539a3f
 
 
a6e6a7c
1539a3f
 
a6e6a7c
1539a3f
 
a6e6a7c
1539a3f
 
a6e6a7c
1539a3f
 
 
 
58b47de
 
 
 
fef3466
58b47de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1539a3f
 
 
 
fef3466
1539a3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6e6a7c
1539a3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fef3466
1539a3f
a6e6a7c
1539a3f
 
58b47de
 
1539a3f
58b47de
c2a315b
fef3466
 
 
c2a315b
 
 
 
58b47de
 
c2a315b
 
1539a3f
58b47de
1539a3f
7db7163
c2a315b
1539a3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7db7163
1539a3f
 
7db7163
1539a3f
 
 
 
fef3466
1539a3f
fef3466
 
 
 
 
a6e6a7c
1539a3f
fef3466
 
 
 
 
 
 
 
 
 
 
1539a3f
 
 
fef3466
a6e6a7c
c2a315b
1539a3f
 
 
 
 
 
 
 
c2a315b
1539a3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2a315b
1539a3f
 
 
 
 
 
c2a315b
fef3466
1539a3f
 
c2a315b
fef3466
c2a315b
 
 
 
 
 
 
 
 
 
 
a28f249
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fef3466
a28f249
fef3466
a28f249
 
 
 
fef3466
a28f249
 
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
"""
Professional Q4 Report Generator - Soft Professional Theme
Comfortable, professional colors with random filename generation
"""
import markdown
from weasyprint import HTML, CSS
import requests
import os
import re
import secrets
from datetime import datetime
from typing import List, Dict, Optional
import logging
from pathlib import Path

# ---------------------------------------------------------
# LOGGING CONFIGURATION
# ---------------------------------------------------------
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger(__name__)

# ---------------------------------------------------------
# SOFT PROFESSIONAL THEME - COMFORTABLE COLORS
# ---------------------------------------------------------
CSS_STYLE = """
@page {
    size: letter;
    margin: 0.75in 0.75in 1in 0.75in;
    
    @top-center {
        content: element(header);
    }
    
    @bottom-center {
        content: element(footer);
    }
}

body {
    font-family: 'Helvetica', Arial, sans-serif;
    color: #2d3748;
    line-height: 1.65;
    font-size: 11pt;
}

.header {
    position: running(header);
    font-size: 9pt;
    color: #718096;
    border-bottom: 1px solid #e2e8f0;
    padding-bottom: 6px;
    margin-bottom: 20px;
}

.footer {
    position: running(footer);
    font-size: 9pt;
    color: #718096;
    border-top: 1px solid #e2e8f0;
    padding-top: 6px;
    text-align: center;
}

.footer::after {
    content: "Page " counter(page) " of " counter(pages);
}

.confidential-mark {
    color: #e53e3e;
    font-weight: bold;
    float: left;
}

.cover-page {
    text-align: center;
    padding-top: 3in;
    page-break-after: always;
}

.cover-title {
    font-size: 36pt;
    font-weight: bold;
    color: #4a5568;
    margin-bottom: 0.5in;
    letter-spacing: 1px;
    line-height: 1.2;
}

.cover-subtitle {
    font-size: 20pt;
    color: #5b7c99;
    margin-bottom: 1.2in;
    font-weight: 500;
}

.cover-meta {
    font-size: 15pt;
    color: #718096;
    margin-bottom: 0.25in;
    line-height: 1.5;
}

.cover-date {
    font-size: 12pt;
    color: #a0aec0;
    margin-top: 0.5in;
}

.cover-divider {
    width: 3in;
    height: 3px;
    background: #5b7c99;
    margin: 0.5in auto;
}

h1 {
    color: #4a5568;
    font-size: 20pt;
    border-bottom: 3px solid #5b7c99;
    padding-bottom: 10px;
    margin-top: 28px;
    margin-bottom: 18px;
    page-break-after: avoid;
    font-weight: bold;
}

h2 {
    color: #5b7c99;
    font-size: 15pt;
    margin-top: 22px;
    margin-bottom: 14px;
    page-break-after: avoid;
    font-weight: 600;
}

h3 {
    color: #6b8ba8;
    font-size: 13pt;
    margin-top: 18px;
    margin-bottom: 12px;
    page-break-after: avoid;
    font-weight: 600;
}

h4 {
    color: #7a9bb7;
    font-size: 12pt;
    margin-top: 16px;
    margin-bottom: 10px;
    page-break-after: avoid;
    font-weight: 600;
}

h5 {
    color: #718096;
    font-size: 11.5pt;
    margin-top: 14px;
    margin-bottom: 8px;
    page-break-after: avoid;
    font-weight: 600;
}

h6 {
    color: #a0aec0;
    font-size: 11pt;
    margin-top: 12px;
    margin-bottom: 8px;
    page-break-after: avoid;
    font-weight: 600;
}

p {
    margin: 10px 0;
    line-height: 1.65;
}

table {
    width: 100%;
    border-collapse: collapse;
    margin: 18px 0;
    font-size: 10pt;
    page-break-inside: avoid;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}

thead {
    background: #5b7c99;
}

th {
    background-color: #5b7c99;
    color: white;
    padding: 12px;
    text-align: left;
    font-weight: bold;
    border: 1px solid #4a6b85;
    font-size: 10.5pt;
}

td {
    padding: 10px 12px;
    border: 1px solid #e2e8f0;
    line-height: 1.5;
}

tr:nth-child(even) {
    background-color: #f7fafc;
}

tr:nth-child(odd) {
    background-color: #ffffff;
}

.highlight-box {
    background-color: #edf2f7;
    border-left: 5px solid #5b7c99;
    padding: 16px 20px;
    margin: 20px 0;
    page-break-inside: avoid;
    box-shadow: 0 2px 4px rgba(0,0,0,0.04);
}

.highlight-box p:first-child {
    margin-top: 0;
}

.highlight-box p:last-child {
    margin-bottom: 0;
}

.key-metric-box {
    background: #5b7c99;
    color: white;
    border-radius: 8px;
    padding: 18px 22px;
    margin: 20px 0;
    page-break-inside: avoid;
    box-shadow: 0 3px 6px rgba(0,0,0,0.12);
}

.key-metric-box h3,
.key-metric-box h4,
.key-metric-box h5 {
    color: white;
    margin-top: 0;
}

.key-metric-box p:first-child {
    margin-top: 0;
}

.key-metric-box p:last-child {
    margin-bottom: 0;
}

.key-metric-box strong {
    color: #ffffff;
}

blockquote {
    border-left: 5px solid #5b7c99;
    margin-left: 0;
    padding-left: 20px;
    color: #4a5568;
    font-style: italic;
    background-color: #f7fafc;
    padding: 14px 14px 14px 20px;
    margin: 18px 0;
    box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}

blockquote p:first-child {
    margin-top: 0;
}

blockquote p:last-child {
    margin-bottom: 0;
}

blockquote strong {
    font-weight: 700;
    color: #2d3748;
}

.figure-container {
    text-align: center;
    margin: 24px 0;
    page-break-inside: avoid;
}

.figure-container img {
    max-width: 100%;
    height: auto;
    border: 1px solid #e2e8f0;
    padding: 8px;
    background: white;
    box-shadow: 0 2px 4px rgba(0,0,0,0.08);
}

.figure-caption {
    font-size: 9.5pt;
    color: #718096;
    margin-top: 10px;
    font-style: italic;
    font-weight: 500;
}

.executive-summary {
    background-color: #ebf4ff;
    border: 2px solid #5b7c99;
    padding: 20px;
    margin: 24px 0;
    page-break-inside: avoid;
    border-radius: 5px;
}

.executive-summary p:first-child {
    margin-top: 0;
}

.executive-summary p:last-child {
    margin-bottom: 0;
}

.page-break {
    page-break-after: always;
}

/* COMPREHENSIVE LIST STYLING */
ul {
    margin: 16px 0;
    padding-left: 35px;
    list-style-type: disc;
    list-style-position: outside;
}

ol {
    margin: 16px 0;
    padding-left: 35px;
    list-style-type: decimal;
    list-style-position: outside;
}

li {
    margin: 6px 0;
    line-height: 1.6;
    padding-left: 5px;
    display: list-item;
}

li p {
    margin: 4px 0;
}

ul ul {
    margin: 8px 0;
    padding-left: 25px;
    list-style-type: circle;
}

ul ul ul {
    list-style-type: square;
}

ol ol {
    margin: 8px 0;
    padding-left: 25px;
    list-style-type: lower-alpha;
}

ol ol ol {
    list-style-type: lower-roman;
}

/* Lists inside special boxes */
.highlight-box ul,
.highlight-box ol,
.key-metric-box ul,
.key-metric-box ol,
.executive-summary ul,
.executive-summary ol,
blockquote ul,
blockquote ol {
    margin: 12px 0;
}

.key-metric-box li {
    color: white;
}

/* TEXT FORMATTING */
strong, b {
    font-weight: 700;
    color: #2d3748;
}

em, i {
    font-style: italic;
}

code {
    font-family: 'Courier New', monospace;
    background-color: #edf2f7;
    padding: 2px 6px;
    border-radius: 3px;
    font-size: 10pt;
    color: #5b7c99;
}

pre {
    background-color: #edf2f7;
    border: 1px solid #e2e8f0;
    border-radius: 4px;
    padding: 12px;
    overflow-x: auto;
    font-family: 'Courier New', monospace;
    font-size: 9.5pt;
    line-height: 1.4;
    margin: 16px 0;
}

pre code {
    background-color: transparent;
    padding: 0;
    color: #4a5568;
}

a {
    color: #5b7c99;
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

hr {
    border: none;
    border-top: 2px solid #e2e8f0;
    margin: 24px 0;
}
"""

# ---------------------------------------------------------
# FILENAME GENERATION
# ---------------------------------------------------------
def generate_random_filename(base_name: str = "Q4_Report") -> str:
    """Generate a random filename to avoid duplicates."""
    timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
    random_suffix = secrets.token_hex(4)  # 8 character hex string
    return f"{base_name}_{timestamp}_{random_suffix}.pdf"

# ---------------------------------------------------------
# IMAGE HANDLING
# ---------------------------------------------------------
def fetch_image(url: str, output_dir: str) -> Optional[str]:
    """Download image from URL to persistent file for PDF rendering."""
    if not url.startswith(('http://', 'https://')):
        if os.path.exists(url):
            abs_path = os.path.abspath(url)
            return Path(abs_path).as_uri()
        return None
    
    try:
        img_dir = os.path.join(output_dir, 'images')
        os.makedirs(img_dir, exist_ok=True)
        
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
        }
        response = requests.get(url, headers=headers, timeout=15)
        response.raise_for_status()
        
        content_type = response.headers.get('content-type', '')
        ext = '.jpg'
        if 'png' in content_type:
            ext = '.png'
        elif 'gif' in content_type:
            ext = '.gif'
        elif 'svg' in content_type:
            ext = '.svg'
        elif 'webp' in content_type:
            ext = '.webp'
        
        timestamp = datetime.now().strftime('%Y%m%d_%H%M%S_%f')
        filename = f'img_{timestamp}{ext}'
        filepath = os.path.join(img_dir, filename)
        
        with open(filepath, 'wb') as f:
            f.write(response.content)
        
        abs_path = os.path.abspath(filepath)
        file_uri = Path(abs_path).as_uri()
        
        logger.info(f"Downloaded image: {url[:50]}... -> {filename}")
        return file_uri
        
    except Exception as e:
        logger.warning(f"Failed to download image: {url[:50]}... - {e}")
        return None

# ---------------------------------------------------------
# MARKDOWN PREPROCESSING
# ---------------------------------------------------------
def preprocess_markdown(content: str) -> str:
    """Preprocess markdown content to ensure proper parsing inside HTML blocks."""
    container_tags = ['div', 'blockquote', 'section', 'article', 'aside']
    
    for tag in container_tags:
        pattern = rf'<{tag}(\s+[^>]*?)?(?!\s+markdown\s*=)>'
        
        def add_markdown_attr(match):
            attrs = match.group(1) or ''
            if attrs:
                return f'<{tag}{attrs} markdown="1">'
            else:
                return f'<{tag} markdown="1">'
        
        content = re.sub(pattern, add_markdown_attr, content, flags=re.IGNORECASE)
    
    return content

# ---------------------------------------------------------
# Q4 REPORT GENERATOR CLASS
# ---------------------------------------------------------
class Q4ReportGenerator:
    """Generates executive-quality Q4 reports with soft professional colors."""
    
    def __init__(self, 
                 title: str = "Q4 Performance Report",
                 subtitle: str = None,
                 author: str = None,
                 department: str = None,
                 date: str = None,
                 header_text: str = None,
                 confidential: bool = True):
        self.title = title
        self.subtitle = subtitle or "Fourth Quarter Analysis"
        self.author = author or "Data Analytics Team"
        self.department = department or "Analytics Department"
        self.date = date or datetime.now().strftime("%B %d, %Y")
        self.confidential = confidential
        self.downloaded_images = []
        
        if header_text:
            self.header_text = header_text
        else:
            parts = [self.department, self.subtitle]
            self.header_text = " | ".join(parts)
    
    def _build_header(self) -> str:
        return f'<div class="header">{self.header_text}</div>'
    
    def _build_footer(self) -> str:
        footer_html = '<div class="footer">'
        if self.confidential:
            footer_html += '<span class="confidential-mark">CONFIDENTIAL</span>'
        footer_html += '</div>'
        return footer_html
    
    def _build_cover_page(self) -> str:
        return f"""
        <div class="cover-page">
            <div class="cover-title">{self.title}</div>
            <div class="cover-divider"></div>
            <div class="cover-subtitle">{self.subtitle}</div>
            <div class="cover-meta">{self.department}</div>
            <div class="cover-meta">Prepared by: {self.author}</div>
            <div class="cover-date">{self.date}</div>
        </div>
        """
    
    def _process_section(self, section: Dict, output_dir: str) -> str:
        """Process section with complete markdown parsing support."""
        html = ""
        
        content = section.get("content", "")
        if content:
            preprocessed_content = preprocess_markdown(content)
            
            html_content = markdown.markdown(
                preprocessed_content,
                extensions=[
                    'extra', 'sane_lists', 'tables', 'fenced_code',
                    'nl2br', 'codehilite', 'toc', 'md_in_html',
                    'attr_list', 'def_list', 'abbr', 'footnotes', 'smarty'
                ],
                extension_configs={
                    'codehilite': {
                        'guess_lang': False,
                        'noclasses': True,
                        'css_class': 'highlight'
                    }
                }
            )
            
            html += f"<div class='section'>{html_content}</div>"
        
        # Handle images
        images = section.get("images", [])
        for img_data in images:
            if isinstance(img_data, dict):
                img_url = img_data.get("url", "")
                caption = img_data.get("caption", "Figure: Visualization")
            else:
                img_url = img_data
                caption = "Figure: Visualization"
            
            if img_url:
                file_uri = fetch_image(img_url, output_dir)
                if file_uri:
                    self.downloaded_images.append(file_uri)
                    html += f"""
                    <div class="figure-container">
                        <img src="{file_uri}" alt="{caption}">
                        <div class="figure-caption">{caption}</div>
                    </div>
                    """
        
        if section.get("page_break", False):
            html += '<div class="page-break"></div>'
        
        return html
    
    def generate(self, 
                 sections: List[Dict], 
                 filename: str = None,
                 output_dir: str = "./output",
                 cover_page: bool = True) -> tuple[bool, str]:
        """
        Generate PDF report with random filename.
        Returns: (success: bool, output_path: str)
        """
        try:
            os.makedirs(output_dir, exist_ok=True)
            
            # Generate random filename if not provided
            if filename is None:
                filename = generate_random_filename("Q4_Report")
            elif not filename.endswith('.pdf'):
                # Add random suffix even if filename provided
                base = filename.replace('.pdf', '')
                timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
                random_suffix = secrets.token_hex(4)
                filename = f"{base}_{timestamp}_{random_suffix}.pdf"
            
            output_path = os.path.join(output_dir, filename)
            
            logger.info(f"Starting Q4 report generation: {output_path}")
            logger.info("Theme: Soft Professional Colors")
            
            # Build HTML body
            body_html = ""
            if cover_page:
                body_html += self._build_cover_page()
            
            for idx, section in enumerate(sections):
                logger.info(f"Processing section {idx + 1}/{len(sections)}")
                body_html += self._process_section(section, output_dir)
            
            # Construct full HTML document
            full_html = f"""
            <!DOCTYPE html>
            <html>
            <head>
                <meta charset="UTF-8">
                <title>{self.title}</title>
            </head>
            <body>
                {self._build_header()}
                {self._build_footer()}
                {body_html}
            </body>
            </html>
            """
            
            # Generate PDF
            logger.info("Rendering PDF with WeasyPrint...")
            html_doc = HTML(string=full_html)
            css_doc = CSS(string=CSS_STYLE)
            
            html_doc.write_pdf(output_path, stylesheets=[css_doc])
            
            logger.info(f"✓ PDF Generated Successfully: {output_path}")
            return True, output_path
            
        except Exception as e:
            logger.error(f"✗ Error generating report: {e}", exc_info=True)
            return False, ""
    
    def cleanup_images(self, output_dir: str):
        """Optional: Clean up downloaded images after PDF generation."""
        try:
            img_dir = os.path.join(output_dir, 'images')
            if os.path.exists(img_dir):
                import shutil
                shutil.rmtree(img_dir)
                logger.info("Cleaned up temporary images")
        except Exception as e:
            logger.warning(f"Failed to cleanup images: {e}")


# ---------------------------------------------------------
# EXAMPLE USAGE
# ---------------------------------------------------------
if __name__ == "__main__":
    generator = Q4ReportGenerator(
        title="Q4 2024 Performance Report",
        subtitle="Executive Summary & Analysis",
        author="Analytics Team",
        department="Business Intelligence",
        confidential=True
    )
    
    sections = [
        {
            "content": """
# Executive Summary

This report presents our **Q4 2024** performance metrics.

## Key Highlights

- Revenue increased by **23%**
- Customer satisfaction at **94%**
- Market share grew to **31%**

### Performance Metrics

| Metric | Q3 | Q4 | Change |
|--------|-------|-------|---------|
| Revenue | $2.3M | $2.8M | +23% |
| Customers | 1,200 | 1,450 | +21% |
| NPS Score | 72 | 78 | +8% |

<div class="highlight-box">

**Important Note:** All figures are preliminary and subject to final audit.

</div>
            """,
            "page_break": False
        }
    ]
    
    success, output_path = generator.generate(
        sections=sections,
        filename="Q4_2024_Report",  # Will add random suffix automatically
        output_dir="./reports"
    )
    
    if success:
        print(f"✓ Report generated: {output_path}")
    else:
        print("✗ Report generation failed!")