Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -64,6 +64,10 @@ from google.cloud import storage, exceptions # Import exceptions for error hand
|
|
64 |
from google.cloud.exceptions import NotFound
|
65 |
from google.oauth2 import service_account
|
66 |
|
|
|
|
|
|
|
|
|
67 |
|
68 |
import logging
|
69 |
|
@@ -366,8 +370,7 @@ def save_to_google_storage(bucket_name, file_path, destination_blob_name, expira
|
|
366 |
)
|
367 |
print(f"✅ File uploaded to Google Cloud Storage. Signed URL: {signed_url}")
|
368 |
return signed_url
|
369 |
-
|
370 |
-
# Function to generate and save a document
|
371 |
def generate_document(task_description, md_content, user_name='jayw', bucket_name='curify'):
|
372 |
logger.debug("Starting to generate document")
|
373 |
|
@@ -380,34 +383,47 @@ def generate_document(task_description, md_content, user_name='jayw', bucket_nam
|
|
380 |
|
381 |
# Generate PDF file locally
|
382 |
local_filename = f"{truncated_hash}.pdf" # Use the truncated hash as the local file name
|
383 |
-
|
384 |
-
pdf.add_page()
|
385 |
|
386 |
-
#
|
387 |
-
font_path = 'NotoSansCJK-Regular.ttc'
|
388 |
try:
|
389 |
-
|
390 |
-
|
|
|
|
|
391 |
except Exception as e:
|
392 |
logger.error(f"Error loading font from {font_path}: {e}")
|
393 |
raise RuntimeError("Failed to load the unified font. Ensure the font file is accessible.")
|
394 |
|
|
|
|
|
|
|
395 |
# Process dictionary and render content
|
396 |
for key, value in md_content.items():
|
397 |
# Add key as a header
|
398 |
-
|
399 |
-
|
|
|
400 |
|
401 |
# Add value
|
402 |
-
|
403 |
if isinstance(value, list): # Handle lists
|
404 |
for item in value:
|
405 |
-
|
|
|
406 |
else: # Handle single strings
|
407 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
408 |
|
409 |
-
#
|
410 |
-
|
411 |
|
412 |
# Organize files into user-specific folders
|
413 |
destination_blob_name = f"{user_name}/{truncated_hash}.pdf"
|
@@ -416,7 +432,7 @@ def generate_document(task_description, md_content, user_name='jayw', bucket_nam
|
|
416 |
public_url = save_to_google_storage(bucket_name, local_filename, destination_blob_name)
|
417 |
logger.debug("Finished generating document")
|
418 |
return public_url
|
419 |
-
|
420 |
# In[10]:
|
421 |
|
422 |
|
|
|
64 |
from google.cloud.exceptions import NotFound
|
65 |
from google.oauth2 import service_account
|
66 |
|
67 |
+
from reportlab.lib.pagesizes import letter
|
68 |
+
from reportlab.pdfgen import canvas
|
69 |
+
from reportlab.pdfbase.ttfonts import TTFont
|
70 |
+
from reportlab.lib import colors
|
71 |
|
72 |
import logging
|
73 |
|
|
|
370 |
)
|
371 |
print(f"✅ File uploaded to Google Cloud Storage. Signed URL: {signed_url}")
|
372 |
return signed_url
|
373 |
+
# Function to generate and save a document using ReportLab
|
|
|
374 |
def generate_document(task_description, md_content, user_name='jayw', bucket_name='curify'):
|
375 |
logger.debug("Starting to generate document")
|
376 |
|
|
|
383 |
|
384 |
# Generate PDF file locally
|
385 |
local_filename = f"{truncated_hash}.pdf" # Use the truncated hash as the local file name
|
386 |
+
c = canvas.Canvas(local_filename, pagesize=letter)
|
|
|
387 |
|
388 |
+
# Load the .ttc font using ReportLab's TTFont and set it as the font for the canvas
|
389 |
+
font_path = 'NotoSansCJK-Regular.ttc' # Path to your .ttc file
|
390 |
try:
|
391 |
+
# Register the font from the .ttc file
|
392 |
+
cttc_font = TTFont('NotoSansCJK', font_path)
|
393 |
+
c.ttfonts.addRegisteredFont('NotoSansCJK', cttc_font)
|
394 |
+
c.setFont('NotoSansCJK', 12)
|
395 |
except Exception as e:
|
396 |
logger.error(f"Error loading font from {font_path}: {e}")
|
397 |
raise RuntimeError("Failed to load the unified font. Ensure the font file is accessible.")
|
398 |
|
399 |
+
# Set initial Y position for drawing text
|
400 |
+
y_position = 750 # Starting position for text
|
401 |
+
|
402 |
# Process dictionary and render content
|
403 |
for key, value in md_content.items():
|
404 |
# Add key as a header
|
405 |
+
c.setFont('NotoSansCJK', 14)
|
406 |
+
c.drawString(100, y_position, f"# {key}")
|
407 |
+
y_position -= 20
|
408 |
|
409 |
# Add value
|
410 |
+
c.setFont('NotoSansCJK', 12) # Regular font for content
|
411 |
if isinstance(value, list): # Handle lists
|
412 |
for item in value:
|
413 |
+
c.drawString(100, y_position, f"- {item}")
|
414 |
+
y_position -= 15
|
415 |
else: # Handle single strings
|
416 |
+
c.drawString(100, y_position, value)
|
417 |
+
y_position -= 15
|
418 |
+
|
419 |
+
# Check if the page needs to be broken (if Y position is too low)
|
420 |
+
if y_position < 100:
|
421 |
+
c.showPage() # Create a new page
|
422 |
+
c.setFont('NotoSansCJK', 12) # Reset font
|
423 |
+
y_position = 750 # Reset the Y position for the new page
|
424 |
|
425 |
+
# Save the PDF
|
426 |
+
c.save()
|
427 |
|
428 |
# Organize files into user-specific folders
|
429 |
destination_blob_name = f"{user_name}/{truncated_hash}.pdf"
|
|
|
432 |
public_url = save_to_google_storage(bucket_name, local_filename, destination_blob_name)
|
433 |
logger.debug("Finished generating document")
|
434 |
return public_url
|
435 |
+
|
436 |
# In[10]:
|
437 |
|
438 |
|