Spaces:
Sleeping
Sleeping
Update main.py
Browse files
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 |
-
|
|
|
|
| 671 |
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 685 |
-
pdf.
|
|
|
|
| 686 |
pdf.ln(5)
|
| 687 |
-
|
| 688 |
-
pdf.
|
|
|
|
| 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(
|
| 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 |
-
#
|
| 707 |
-
pdf.image(temp_img_path, x=10, w=pdf.w - 20, h=100)
|
| 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(
|
| 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()
|