rairo commited on
Commit
2ef53c5
Β·
verified Β·
1 Parent(s): f2f2fdc

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +23 -12
main.py CHANGED
@@ -666,12 +666,21 @@ def download_story_archive(story_id):
666
  logging.info(f"πŸ“„ Generating PDF for {story_id}")
667
  pdf = FPDF()
668
  pdf.set_auto_page_break(auto=True, margin=15)
 
 
 
 
 
669
  pdf.add_page()
670
- pdf.set_font("Arial", size=12)
 
671
 
672
- pdf.multi_cell(190, 10, f"Story ID: {story_id}")
673
- pdf.multi_cell(190, 10, f"Title: {title}")
674
- pdf.ln(10) # Add spacing before sections start
 
 
 
675
 
676
  bucket = storage.bucket()
677
 
@@ -681,11 +690,13 @@ def download_story_archive(story_id):
681
  image_url = section_obj.get("image_url", "")
682
 
683
  pdf.add_page()
684
- pdf.set_font("Arial", style='B', size=14)
685
- pdf.multi_cell(190, 10, f"Section {idx + 1}")
 
686
  pdf.ln(5)
687
- pdf.set_font("Arial", size=12)
688
- pdf.multi_cell(190, 10, section_text)
 
689
  pdf.ln(10)
690
 
691
  if image_url:
@@ -694,7 +705,7 @@ def download_story_archive(story_id):
694
  file_path = extract_firebase_path(image_url)
695
  if not file_path:
696
  logging.error(f"❌ Could not parse image URL => {image_url}")
697
- pdf.multi_cell(190, 10, "[Image URL invalid]")
698
  continue
699
 
700
  with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as temp_file:
@@ -703,13 +714,13 @@ def download_story_archive(story_id):
703
  blob = bucket.blob(file_path)
704
  blob.download_to_filename(temp_img_path)
705
 
706
- # Adjust image placement and size to fit allocated space
707
- pdf.image(temp_img_path, x=10, w=pdf.w - 20, h=100) # Full allocated width, fixed height
708
  os.unlink(temp_img_path)
709
  logging.info(f"βœ… Image embedded in PDF for section {idx + 1}")
710
  except Exception as img_error:
711
  logging.error(f"❌ Error embedding image for section {idx + 1}: {str(img_error)}")
712
- pdf.multi_cell(190, 10, "[Image could not be included]")
713
 
714
  # Save PDF to memory
715
  pdf_buffer = io.BytesIO()
 
666
  logging.info(f"πŸ“„ Generating PDF for {story_id}")
667
  pdf = FPDF()
668
  pdf.set_auto_page_break(auto=True, margin=15)
669
+
670
+ # --- Add and use DejaVu fonts for Unicode support ---
671
+ pdf.add_font("DejaVu", "", "DejaVuSans.ttf", uni=True)
672
+ pdf.add_font("DejaVu", "B", "dejaVu-sans-bold.ttf", uni=True)
673
+
674
  pdf.add_page()
675
+ # Use our newly added DejaVu font
676
+ pdf.set_font("DejaVu", size=12)
677
 
678
+ # Keep text within a safe width (190) so it doesn't go off-screen
679
+ max_width = 190
680
+
681
+ pdf.multi_cell(max_width, 10, f"Story ID: {story_id}")
682
+ pdf.multi_cell(max_width, 10, f"Title: {title}")
683
+ pdf.ln(10) # spacing before sections
684
 
685
  bucket = storage.bucket()
686
 
 
690
  image_url = section_obj.get("image_url", "")
691
 
692
  pdf.add_page()
693
+ # Use bold font for section headers
694
+ pdf.set_font("DejaVu", "B", 14)
695
+ pdf.multi_cell(max_width, 10, f"Section {idx + 1}")
696
  pdf.ln(5)
697
+
698
+ pdf.set_font("DejaVu", size=12)
699
+ pdf.multi_cell(max_width, 10, section_text)
700
  pdf.ln(10)
701
 
702
  if image_url:
 
705
  file_path = extract_firebase_path(image_url)
706
  if not file_path:
707
  logging.error(f"❌ Could not parse image URL => {image_url}")
708
+ pdf.multi_cell(max_width, 10, "[Image URL invalid]")
709
  continue
710
 
711
  with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as temp_file:
 
714
  blob = bucket.blob(file_path)
715
  blob.download_to_filename(temp_img_path)
716
 
717
+ # Insert the image, using full page width minus margins
718
+ pdf.image(temp_img_path, x=10, w=pdf.w - 20, h=100)
719
  os.unlink(temp_img_path)
720
  logging.info(f"βœ… Image embedded in PDF for section {idx + 1}")
721
  except Exception as img_error:
722
  logging.error(f"❌ Error embedding image for section {idx + 1}: {str(img_error)}")
723
+ pdf.multi_cell(max_width, 10, "[Image could not be included]")
724
 
725
  # Save PDF to memory
726
  pdf_buffer = io.BytesIO()