File size: 4,416 Bytes
3febaf2
 
 
 
c283503
15f6774
c283503
 
 
3febaf2
 
 
 
 
15f6774
 
 
c283503
 
15f6774
c283503
 
15f6774
 
 
 
 
 
 
 
 
 
 
 
c283503
 
 
 
15f6774
3febaf2
 
c283503
 
3febaf2
 
 
 
 
c283503
 
 
3febaf2
 
 
 
 
c283503
 
 
 
3febaf2
 
 
 
 
c283503
 
 
 
3febaf2
 
 
 
c283503
3febaf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15f6774
 
3febaf2
 
c283503
3febaf2
 
c283503
3febaf2
15f6774
 
c283503
 
 
 
3febaf2
 
 
 
 
15f6774
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
def create_main_pdf(markdown_text):
    buffer = io.BytesIO()
    doc = SimpleDocTemplate(
        buffer, 
        pagesize=(A4[1], A4[0]),  # Landscape A4: 841.89 x 595.27 points
        leftMargin=36,
        rightMargin=36,
        topMargin=36,
        bottomMargin=36
    )
    
    styles = getSampleStyleSheet()
    story = []
    
    page_height = A4[0] - 72
    title_height = 20
    spacer_height = 10
    available_content_height = page_height - title_height - spacer_height
    
    # Process columns first
    left_column, right_column = markdown_to_pdf_content(markdown_text)
    
    # Calculate total items by explicitly handling the unpacking
    total_items = 0
    for col in (left_column, right_column):
        for item in col:
            if isinstance(item, list):
                main_item, sub_items = item  # Unpack here
                total_items += 1 + len(sub_items)
            else:
                total_items += 1
    
    # Dynamic font sizes
    base_font_size = max(6, min(11, 200 / total_items))
    item_font_size = base_font_size
    subitem_font_size = base_font_size * 0.9
    section_font_size = base_font_size * 1.2
    
    # Create custom styles
    title_style = styles['Heading1']
    title_style.textColor = colors.darkblue
    title_style.alignment = 1
    title_style.fontSize = min(16, base_font_size * 1.5)
    
    section_style = ParagraphStyle(
        'SectionStyle',
        parent=styles['Heading2'],
        textColor=colors.darkblue,
        fontSize=section_font_size,
        leading=section_font_size * 1.2,
        spaceAfter=2
    )
    
    item_style = ParagraphStyle(
        'ItemStyle',
        parent=styles['Normal'],
        fontSize=item_font_size,
        leading=item_font_size * 1.2,
        fontName='Helvetica-Bold',
        spaceAfter=1
    )
    
    subitem_style = ParagraphStyle(
        'SubItemStyle',
        parent=styles['Normal'],
        fontSize=subitem_font_size,
        leading=subitem_font_size * 1.2,
        leftIndent=10,
        spaceAfter=1
    )
    
    # Add title
    story.append(Paragraph("Cutting-Edge ML Outline (ReportLab)", title_style))
    story.append(Spacer(1, spacer_height))
    
    # Prepare data for table
    left_cells = []
    for item in left_column:
        if isinstance(item, str) and item.startswith('<b>'):
            text = item.replace('<b>', '').replace('</b>', '')
            left_cells.append(Paragraph(text, section_style))
        elif isinstance(item, list):
            main_item, sub_items = item
            left_cells.append(Paragraph(main_item, item_style))
            for sub_item in sub_items:
                left_cells.append(Paragraph(sub_item, subitem_style))
        else:
            left_cells.append(Paragraph(item, item_style))
    
    right_cells = []
    for item in right_column:
        if isinstance(item, str) and item.startswith('<b>'):
            text = item.replace('<b>', '').replace('</b>', '')
            right_cells.append(Paragraph(text, section_style))
        elif isinstance(item, list):
            main_item, sub_items = item
            right_cells.append(Paragraph(main_item, item_style))
            for sub_item in sub_items:
                right_cells.append(Paragraph(sub_item, subitem_style))
        else:
            right_cells.append(Paragraph(item, item_style))
    
    # Make columns equal length
    max_cells = max(len(left_cells), len(right_cells))
    left_cells.extend([""] * (max_cells - len(left_cells)))
    right_cells.extend([""] * (max_cells - len(right_cells)))
    
    # Create table data
    table_data = list(zip(left_cells, right_cells))
    
    # Calculate column widths
    col_width = (A4[1] - 72) / 2.0
    
    # Create and style table
    table = Table(table_data, colWidths=[col_width, col_width], hAlign='CENTER')
    table.setStyle(TableStyle([
        ('VALIGN', (0, 0), (-1, -1), 'TOP'),
        ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
        ('BACKGROUND', (0, 0), (-1, -1), colors.white),
        ('GRID', (0, 0), (-1, -1), 0, colors.white),
        ('LINEAFTER', (0, 0), (0, -1), 0.5, colors.grey),
        ('LEFTPADDING', (0, 0), (-1, -1), 2),
        ('RIGHTPADDING', (0, 0), (-1, -1), 2),
        ('TOPPADDING', (0, 0), (-1, -1), 1),
        ('BOTTOMPADDING', (0, 0), (-1, -1), 1),
    ]))
    
    story.append(table)
    doc.build(story)
    buffer.seek(0)
    return buffer.getvalue()