ChristopherJKoen commited on
Commit
1f0784e
·
1 Parent(s): ed33547

Fix PDF export to honor section page variants

Browse files
server/app/api/routes/sessions.py CHANGED
@@ -337,9 +337,9 @@ def export_pdf(
337
  session = store.get_session(session_id)
338
  if not session:
339
  raise HTTPException(status_code=404, detail="Session not found.")
340
- pages = store.ensure_pages(session)
341
  export_path = Path(store.session_dir(session_id)) / "export.pdf"
342
- render_report_pdf(store, session, pages, export_path)
343
  return FileResponse(
344
  export_path,
345
  media_type="application/pdf",
 
337
  session = store.get_session(session_id)
338
  if not session:
339
  raise HTTPException(status_code=404, detail="Session not found.")
340
+ sections = store.ensure_sections(session)
341
  export_path = Path(store.session_dir(session_id)) / "export.pdf"
342
+ render_report_pdf(store, session, sections, export_path)
343
  return FileResponse(
344
  export_path,
345
  media_type="application/pdf",
server/app/services/pdf_reportlab.py CHANGED
@@ -215,7 +215,7 @@ def _draw_image_fit(
215
  def render_report_pdf(
216
  store: SessionStore,
217
  session: dict,
218
- pages: List[dict],
219
  output_path: Path,
220
  ) -> Path:
221
  width, height = A4
@@ -260,29 +260,47 @@ def render_report_pdf(
260
  uploads = (session.get("uploads") or {}).get("photos") or []
261
  by_id = {item.get("id"): item for item in uploads if item.get("id")}
262
 
 
 
 
 
 
 
263
  print_pages: List[dict] = []
264
- for page_index, page in enumerate(pages):
265
- template = page.get("template") or {}
266
- photo_ids = page.get("photo_ids") or []
267
- photo_entries = []
268
- for pid in photo_ids:
269
- item = by_id.get(pid)
270
- if not item:
271
- continue
272
- path = store.resolve_upload_path(session, pid)
273
- if path and path.exists():
274
- label = _safe_text(item.get("name") or path.name)
275
- photo_entries.append({"path": path, "label": label})
276
- chunks = _chunk(photo_entries, max_photos_per_page) or [[]]
277
- for chunk_index, chunk in enumerate(chunks):
278
- print_pages.append(
279
- {
280
- "page_index": page_index,
281
- "template": template,
282
- "photos": chunk,
283
- "variant": "full" if chunk_index == 0 else "photos",
284
- }
285
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286
 
287
  if not print_pages:
288
  print_pages = [
 
215
  def render_report_pdf(
216
  store: SessionStore,
217
  session: dict,
218
+ sections_or_pages: List[dict],
219
  output_path: Path,
220
  ) -> Path:
221
  width, height = A4
 
260
  uploads = (session.get("uploads") or {}).get("photos") or []
261
  by_id = {item.get("id"): item for item in uploads if item.get("id")}
262
 
263
+ sections: List[dict]
264
+ if sections_or_pages and isinstance(sections_or_pages[0], dict) and "pages" in sections_or_pages[0]:
265
+ sections = sections_or_pages
266
+ else:
267
+ sections = [{"id": "section-1", "title": "Section 1", "pages": sections_or_pages or []}]
268
+
269
  print_pages: List[dict] = []
270
+ for section_index, section in enumerate(sections):
271
+ section_pages = section.get("pages") or []
272
+ for page_index, page in enumerate(section_pages):
273
+ template = page.get("template") or {}
274
+ base_variant = (
275
+ (page.get("variant") or "").strip().lower() if isinstance(page, dict) else ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276
  )
277
+ if base_variant not in ("full", "photos"):
278
+ base_variant = "full" if page_index == 0 else "photos"
279
+ photo_ids = page.get("photo_ids") or []
280
+ photo_entries = []
281
+ for pid in photo_ids:
282
+ item = by_id.get(pid)
283
+ if not item:
284
+ continue
285
+ path = store.resolve_upload_path(session, pid)
286
+ if path and path.exists():
287
+ label = _safe_text(item.get("name") or path.name)
288
+ photo_entries.append({"path": path, "label": label})
289
+ chunks = _chunk(photo_entries, max_photos_per_page) or [[]]
290
+ for chunk_index, chunk in enumerate(chunks):
291
+ if base_variant == "photos":
292
+ variant = "photos"
293
+ else:
294
+ variant = "full" if chunk_index == 0 else "photos"
295
+ print_pages.append(
296
+ {
297
+ "page_index": page_index,
298
+ "template": template,
299
+ "photos": chunk,
300
+ "variant": variant,
301
+ "section_index": section_index,
302
+ }
303
+ )
304
 
305
  if not print_pages:
306
  print_pages = [