awacke1 commited on
Commit
c283503
·
verified ·
1 Parent(s): 0e2b634

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -25
app.py CHANGED
@@ -128,50 +128,70 @@ def create_main_pdf(markdown_text):
128
  buffer = io.BytesIO()
129
  doc = SimpleDocTemplate(
130
  buffer,
131
- pagesize=(A4[1], A4[0]), # Landscape
132
- leftMargin=50,
133
- rightMargin=50,
134
- topMargin=50,
135
- bottomMargin=50
136
  )
137
 
138
  styles = getSampleStyleSheet()
139
  story = []
140
 
141
- # Create custom styles
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  title_style = styles['Heading1']
143
  title_style.textColor = colors.darkblue
144
- title_style.alignment = 1 # Center alignment
 
145
 
146
  section_style = ParagraphStyle(
147
  'SectionStyle',
148
  parent=styles['Heading2'],
149
  textColor=colors.darkblue,
150
- spaceAfter=6
 
 
151
  )
152
 
153
  item_style = ParagraphStyle(
154
  'ItemStyle',
155
  parent=styles['Normal'],
156
- fontSize=11,
157
- leading=14,
158
- fontName='Helvetica-Bold'
 
159
  )
160
 
161
  subitem_style = ParagraphStyle(
162
  'SubItemStyle',
163
  parent=styles['Normal'],
164
- fontSize=10,
165
- leading=12,
166
- leftIndent=20
 
167
  )
168
 
169
  # Add title
170
  story.append(Paragraph("Cutting-Edge ML Outline (ReportLab)", title_style))
171
- story.append(Spacer(1, 20))
172
-
173
- # Process markdown content
174
- left_column, right_column = markdown_to_pdf_content(markdown_text)
175
 
176
  # Prepare data for table
177
  left_cells = []
@@ -208,20 +228,27 @@ def create_main_pdf(markdown_text):
208
  # Create table data
209
  table_data = list(zip(left_cells, right_cells))
210
 
211
- # Calculate column widths
212
- col_width = (A4[1] - 120) / 2.0
213
 
214
  # Create and style table
215
- table = Table(table_data, colWidths=[col_width, col_width])
216
  table.setStyle(TableStyle([
217
  ('VALIGN', (0, 0), (-1, -1), 'TOP'),
218
- ('ALIGN', (0, 0), (0, -1), 'LEFT'),
219
- ('ALIGN', (1, 0), (1, -1), 'LEFT'),
220
  ('BACKGROUND', (0, 0), (-1, -1), colors.white),
221
- ('GRID', (0, 0), (-1, -1), 0.5, colors.white),
222
- ('LINEAFTER', (0, 0), (0, -1), 1, colors.grey),
 
 
 
 
223
  ]))
224
 
 
 
 
 
225
  story.append(table)
226
  doc.build(story)
227
  buffer.seek(0)
 
128
  buffer = io.BytesIO()
129
  doc = SimpleDocTemplate(
130
  buffer,
131
+ pagesize=(A4[1], A4[0]), # Landscape A4: 841.89 x 595.27 points
132
+ leftMargin=36, # Reduced margins to maximize content area
133
+ rightMargin=36,
134
+ topMargin=36,
135
+ bottomMargin=36
136
  )
137
 
138
  styles = getSampleStyleSheet()
139
  story = []
140
 
141
+ # Available height for content (excluding title and spacer)
142
+ page_height = A4[0] - 72 # Total height minus top and bottom margins
143
+ title_height = 20 # Approximate height of title
144
+ spacer_height = 10 # Reduced spacer
145
+ available_content_height = page_height - title_height - spacer_height
146
+
147
+ # Count total items for dynamic sizing
148
+ left_column, right_column = markdown_to_pdf_content(markdown_text)
149
+ total_items = sum(1 + (len(sub_items) if isinstance(item, list) else 0)
150
+ for col in (left_column, right_column)
151
+ for item in col)
152
+
153
+ # Dynamic font sizes based on content length
154
+ base_font_size = max(6, min(11, 200 / total_items)) # Between 6 and 11
155
+ item_font_size = base_font_size
156
+ subitem_font_size = base_font_size * 0.9
157
+ section_font_size = base_font_size * 1.2
158
+
159
+ # Create custom styles with dynamic sizes
160
  title_style = styles['Heading1']
161
  title_style.textColor = colors.darkblue
162
+ title_style.alignment = 1
163
+ title_style.fontSize = min(16, base_font_size * 1.5)
164
 
165
  section_style = ParagraphStyle(
166
  'SectionStyle',
167
  parent=styles['Heading2'],
168
  textColor=colors.darkblue,
169
+ fontSize=section_font_size,
170
+ leading=section_font_size * 1.2,
171
+ spaceAfter=2
172
  )
173
 
174
  item_style = ParagraphStyle(
175
  'ItemStyle',
176
  parent=styles['Normal'],
177
+ fontSize=item_font_size,
178
+ leading=item_font_size * 1.2,
179
+ fontName='Helvetica-Bold',
180
+ spaceAfter=1
181
  )
182
 
183
  subitem_style = ParagraphStyle(
184
  'SubItemStyle',
185
  parent=styles['Normal'],
186
+ fontSize=subitem_font_size,
187
+ leading=subitem_font_size * 1.2,
188
+ leftIndent=10,
189
+ spaceAfter=1
190
  )
191
 
192
  # Add title
193
  story.append(Paragraph("Cutting-Edge ML Outline (ReportLab)", title_style))
194
+ story.append(Spacer(1, spacer_height))
 
 
 
195
 
196
  # Prepare data for table
197
  left_cells = []
 
228
  # Create table data
229
  table_data = list(zip(left_cells, right_cells))
230
 
231
+ # Calculate column widths (maximize usable width)
232
+ col_width = (A4[1] - 72) / 2.0 # Total width minus margins divided by 2
233
 
234
  # Create and style table
235
+ table = Table(table_data, colWidths=[col_width, col_width], hAlign='CENTER')
236
  table.setStyle(TableStyle([
237
  ('VALIGN', (0, 0), (-1, -1), 'TOP'),
238
+ ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
 
239
  ('BACKGROUND', (0, 0), (-1, -1), colors.white),
240
+ ('GRID', (0, 0), (-1, -1), 0, colors.white), # Remove grid lines
241
+ ('LINEAFTER', (0, 0), (0, -1), 0.5, colors.grey), # Center divider
242
+ ('LEFTPADDING', (0, 0), (-1, -1), 2),
243
+ ('RIGHTPADDING', (0, 0), (-1, -1), 2),
244
+ ('TOPPADDING', (0, 0), (-1, -1), 1),
245
+ ('BOTTOMPADDING', (0, 0), (-1, -1), 1),
246
  ]))
247
 
248
+ # Ensure table fits within available height
249
+ table.spaceBefore = 0
250
+ table.spaceAfter = 0
251
+
252
  story.append(table)
253
  doc.build(story)
254
  buffer.seek(0)