Update src/Report/Report.py
Browse files- src/Report/Report.py +73 -55
src/Report/Report.py
CHANGED
|
@@ -2,89 +2,107 @@ import os
|
|
| 2 |
import PyPDF2
|
| 3 |
from reportlab.lib.pagesizes import letter
|
| 4 |
from reportlab.pdfgen import canvas
|
| 5 |
-
import
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
"""Ensure the given directory exists."""
|
| 9 |
-
os.makedirs(directory, exist_ok=True)
|
| 10 |
|
| 11 |
-
def
|
| 12 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
pdf_merger = PyPDF2.PdfMerger()
|
|
|
|
|
|
|
| 14 |
pdf_list = [os.path.abspath(pdf) for pdf in pdf_list]
|
| 15 |
|
| 16 |
for pdf in pdf_list:
|
| 17 |
if not os.path.exists(pdf):
|
| 18 |
-
print(f"β οΈ
|
| 19 |
continue
|
| 20 |
|
| 21 |
try:
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
pdf_merger.append(pdf)
|
|
|
|
| 24 |
except Exception as e:
|
| 25 |
print(f"β οΈ Could not add {pdf}: {e}")
|
| 26 |
|
| 27 |
try:
|
| 28 |
pdf_merger.write(output_path)
|
| 29 |
-
print(f"π Merged PDF saved: {output_path}")
|
| 30 |
except Exception as e:
|
| 31 |
-
print(f"
|
| 32 |
finally:
|
| 33 |
pdf_merger.close()
|
| 34 |
|
| 35 |
-
def convert_png_to_pdf(png_path, output_pdf):
|
| 36 |
-
"""Convert a PNG image into a PDF and save it in a cloud-friendly location."""
|
| 37 |
-
from PIL import Image
|
| 38 |
-
|
| 39 |
-
try:
|
| 40 |
-
ensure_directory_exists(os.path.dirname(output_pdf))
|
| 41 |
-
|
| 42 |
-
img = Image.open(png_path)
|
| 43 |
-
img.convert('RGB').save(output_pdf, "PDF")
|
| 44 |
-
|
| 45 |
-
print(f"π PDF saved: {output_pdf}")
|
| 46 |
-
except Exception as e:
|
| 47 |
-
print(f"β Error converting PNG to PDF: {e}")
|
| 48 |
-
|
| 49 |
if __name__ == "__main__":
|
| 50 |
-
#
|
| 51 |
-
base_dir = "/home/user/app" # Hugging Face Spaces safe storage
|
| 52 |
-
template_dir = os.path.join(base_dir, "data/reports/template_PDF")
|
| 53 |
-
report_stats_dir = os.path.join(base_dir, "data/reports/report_stats")
|
| 54 |
-
output_dir = os.path.join(base_dir, "src/Report")
|
| 55 |
-
|
| 56 |
-
# β
Ensure directories exist
|
| 57 |
-
ensure_directory_exists(template_dir)
|
| 58 |
-
ensure_directory_exists(report_stats_dir)
|
| 59 |
-
ensure_directory_exists(output_dir)
|
| 60 |
-
|
| 61 |
-
# β
List of PDF files to merge
|
| 62 |
pdf_files = [
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
]
|
| 72 |
|
| 73 |
-
# β
Define elongated PDFs (for special processing)
|
| 74 |
elongated_pdfs = [
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
]
|
| 79 |
|
| 80 |
-
#
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
#
|
| 84 |
merge_pdfs(pdf_files, output_file, elongated_files=elongated_pdfs)
|
| 85 |
|
| 86 |
-
# β
Make the report available for download
|
| 87 |
-
visible_output = os.path.join(base_dir, "report.pdf")
|
| 88 |
-
shutil.copy(output_file, visible_output)
|
| 89 |
|
| 90 |
-
|
|
|
|
| 2 |
import PyPDF2
|
| 3 |
from reportlab.lib.pagesizes import letter
|
| 4 |
from reportlab.pdfgen import canvas
|
| 5 |
+
from screeninfo import get_monitors
|
| 6 |
+
import sys
|
| 7 |
+
import codecs
|
| 8 |
|
| 9 |
+
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.buffer)
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
def get_screen_size():
|
| 12 |
+
"""Get the primary monitor screen size."""
|
| 13 |
+
try:
|
| 14 |
+
monitor = get_monitors()[0] # Get primary monitor
|
| 15 |
+
screen_width = monitor.width
|
| 16 |
+
screen_height = monitor.height
|
| 17 |
+
return screen_width, int(screen_height * 1.5) # Scale height
|
| 18 |
+
except Exception as e:
|
| 19 |
+
print(f"Error getting screen size: {e}")
|
| 20 |
+
return 1000, 1500 # Default size
|
| 21 |
+
|
| 22 |
+
def add_padding_to_pdf(input_pdf, output_pdf, padding):
|
| 23 |
+
"""Add padding to a PDF file."""
|
| 24 |
+
try:
|
| 25 |
+
reader = PyPDF2.PdfReader(input_pdf)
|
| 26 |
+
writer = PyPDF2.PdfWriter()
|
| 27 |
+
|
| 28 |
+
for page in reader.pages:
|
| 29 |
+
original_width = page.mediabox.width
|
| 30 |
+
original_height = page.mediabox.height
|
| 31 |
+
|
| 32 |
+
# New dimensions with padding
|
| 33 |
+
new_width = original_width + 2 * padding
|
| 34 |
+
new_height = original_height + 2 * padding
|
| 35 |
+
|
| 36 |
+
# Create a new canvas
|
| 37 |
+
packet = canvas.Canvas(output_pdf, pagesize=(new_width, new_height))
|
| 38 |
+
packet.translate(padding, padding) # Center original content
|
| 39 |
+
packet.save()
|
| 40 |
+
|
| 41 |
+
writer.add_page(page)
|
| 42 |
+
|
| 43 |
+
with open(output_pdf, "wb") as out_file:
|
| 44 |
+
writer.write(out_file)
|
| 45 |
+
|
| 46 |
+
print(f"β
Padding added: {input_pdf} -> {output_pdf}")
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"β οΈ Error adding padding to {input_pdf}: {e}")
|
| 50 |
+
|
| 51 |
+
def merge_pdfs(pdf_list, output_path, elongated_files=[], normalize_size=True):
|
| 52 |
+
"""Merge multiple PDFs into a single PDF."""
|
| 53 |
pdf_merger = PyPDF2.PdfMerger()
|
| 54 |
+
|
| 55 |
+
# Resolve absolute paths
|
| 56 |
pdf_list = [os.path.abspath(pdf) for pdf in pdf_list]
|
| 57 |
|
| 58 |
for pdf in pdf_list:
|
| 59 |
if not os.path.exists(pdf):
|
| 60 |
+
print(f"β οΈ File not found: {pdf}")
|
| 61 |
continue
|
| 62 |
|
| 63 |
try:
|
| 64 |
+
if pdf in elongated_files:
|
| 65 |
+
print(f"π Processing elongated file: {pdf}")
|
| 66 |
+
# Add additional elongation logic here if needed
|
| 67 |
pdf_merger.append(pdf)
|
| 68 |
+
print(f"β
Added: {pdf}")
|
| 69 |
except Exception as e:
|
| 70 |
print(f"β οΈ Could not add {pdf}: {e}")
|
| 71 |
|
| 72 |
try:
|
| 73 |
pdf_merger.write(output_path)
|
| 74 |
+
print(f"π Merged PDF saved as: {output_path}")
|
| 75 |
except Exception as e:
|
| 76 |
+
print(f"Error saving merged PDF: {e}")
|
| 77 |
finally:
|
| 78 |
pdf_merger.close()
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
if __name__ == "__main__":
|
| 81 |
+
# List of PDF files to merge
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
pdf_files = [
|
| 83 |
+
"src/Report/1updated.pdf",
|
| 84 |
+
"data/reports/report_stats/2.pdf",
|
| 85 |
+
"data/reports/report_stats/3.pdf",
|
| 86 |
+
"data/reports/report_stats/objective.pdf",
|
| 87 |
+
"data/reports/template_PDF/brand marketing.pdf",
|
| 88 |
+
"data/reports/template_PDF/content marketing.pdf",
|
| 89 |
+
"data/reports/template_PDF/social media marketing.pdf",
|
| 90 |
+
"data/reports/report_stats/last.pdf"
|
| 91 |
]
|
| 92 |
|
|
|
|
| 93 |
elongated_pdfs = [
|
| 94 |
+
"data/reports/template_PDF/brand marketing.pdf",
|
| 95 |
+
"data/reports/template_PDF/content marketing.pdf",
|
| 96 |
+
"data/reports/template_PDF/social media marketing.pdf"
|
| 97 |
]
|
| 98 |
|
| 99 |
+
# Define output directory and strict file name
|
| 100 |
+
output_dir = "src/Report"
|
| 101 |
+
os.makedirs(output_dir, exist_ok=True) # Create the directory if it doesn't exist
|
| 102 |
+
output_file = os.path.join(output_dir, "report.pdf") # Enforce strict file name as report.pdf
|
| 103 |
|
| 104 |
+
# Merge the PDFs and save the result in the specified directory
|
| 105 |
merge_pdfs(pdf_files, output_file, elongated_files=elongated_pdfs)
|
| 106 |
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
|