amine_dubs
commited on
Commit
·
00634bc
1
Parent(s):
af0b13b
- backend/main.py +55 -30
- project_report.md +1 -1
backend/main.py
CHANGED
|
@@ -730,8 +730,6 @@ async def download_translated_document(request: Request):
|
|
| 730 |
try:
|
| 731 |
import fitz # PyMuPDF
|
| 732 |
from io import BytesIO
|
| 733 |
-
import tempfile
|
| 734 |
-
import os
|
| 735 |
|
| 736 |
# Create a new PDF document
|
| 737 |
doc = fitz.open()
|
|
@@ -740,45 +738,72 @@ async def download_translated_document(request: Request):
|
|
| 740 |
# Check if text contains Arabic
|
| 741 |
has_arabic = any('\u0600' <= c <= '\u06FF' for c in content)
|
| 742 |
|
| 743 |
-
#
|
| 744 |
try:
|
| 745 |
-
#
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
page.
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 765 |
except Exception as e:
|
| 766 |
-
print(f"Error inserting text: {e}")
|
| 767 |
-
|
| 768 |
-
page.insert_text((50, 50), content, fontsize=11)
|
| 769 |
|
| 770 |
-
# Save
|
| 771 |
pdf_bytes = BytesIO()
|
| 772 |
doc.save(pdf_bytes)
|
|
|
|
| 773 |
doc.close()
|
| 774 |
|
| 775 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 776 |
return Response(
|
| 777 |
content=pdf_bytes.getvalue(),
|
| 778 |
media_type="application/pdf",
|
| 779 |
headers={"Content-Disposition": f"attachment; filename={filename}"}
|
| 780 |
)
|
| 781 |
-
|
| 782 |
except ImportError:
|
| 783 |
return JSONResponse(
|
| 784 |
status_code=501,
|
|
@@ -787,7 +812,7 @@ async def download_translated_document(request: Request):
|
|
| 787 |
except Exception as e:
|
| 788 |
print(f"PDF creation error: {str(e)}")
|
| 789 |
traceback.print_exc()
|
| 790 |
-
# Return a text file
|
| 791 |
return Response(
|
| 792 |
content=content.encode('utf-8'),
|
| 793 |
media_type="text/plain; charset=utf-8",
|
|
|
|
| 730 |
try:
|
| 731 |
import fitz # PyMuPDF
|
| 732 |
from io import BytesIO
|
|
|
|
|
|
|
| 733 |
|
| 734 |
# Create a new PDF document
|
| 735 |
doc = fitz.open()
|
|
|
|
| 738 |
# Check if text contains Arabic
|
| 739 |
has_arabic = any('\u0600' <= c <= '\u06FF' for c in content)
|
| 740 |
|
| 741 |
+
# Use a plain and simple approach that works across PyMuPDF versions
|
| 742 |
try:
|
| 743 |
+
font = "helv" # Default PyMuPDF font with basic Unicode support
|
| 744 |
+
rect = fitz.Rect(72, 72, page.rect.width - 72, page.rect.height - 72)
|
| 745 |
+
|
| 746 |
+
# Important: Break the content into shorter lines for better handling
|
| 747 |
+
lines = content.split('\n')
|
| 748 |
+
y_pos = 72 # Starting y position
|
| 749 |
+
|
| 750 |
+
for line in lines:
|
| 751 |
+
if line.strip():
|
| 752 |
+
# Explicitly encode line as UTF-8 if it contains Arabic
|
| 753 |
+
text_to_insert = line
|
| 754 |
+
if has_arabic:
|
| 755 |
+
# For Arabic text, position on right side of page
|
| 756 |
+
page.insert_text(
|
| 757 |
+
point=(page.rect.width - 72, y_pos), # Right-aligned position
|
| 758 |
+
text=text_to_insert,
|
| 759 |
+
fontname=font,
|
| 760 |
+
fontsize=11,
|
| 761 |
+
color=(0, 0, 0), # Black text
|
| 762 |
+
rotate=0,
|
| 763 |
+
align=1 # Right alignment (1=right, 0=left, 2=center)
|
| 764 |
+
)
|
| 765 |
+
else:
|
| 766 |
+
# For non-Arabic text
|
| 767 |
+
page.insert_text(
|
| 768 |
+
point=(72, y_pos),
|
| 769 |
+
text=text_to_insert,
|
| 770 |
+
fontname=font,
|
| 771 |
+
fontsize=11
|
| 772 |
+
)
|
| 773 |
+
# Move to next line
|
| 774 |
+
y_pos += 14 # Line spacing
|
| 775 |
except Exception as e:
|
| 776 |
+
print(f"Error inserting text into PDF: {e}")
|
| 777 |
+
traceback.print_exc()
|
|
|
|
| 778 |
|
| 779 |
+
# Save PDF to a BytesIO buffer
|
| 780 |
pdf_bytes = BytesIO()
|
| 781 |
doc.save(pdf_bytes)
|
| 782 |
+
pdf_bytes.seek(0) # Important: Reset position to start of buffer
|
| 783 |
doc.close()
|
| 784 |
|
| 785 |
+
# Log PDF size for debugging
|
| 786 |
+
pdf_size = len(pdf_bytes.getvalue())
|
| 787 |
+
print(f"Generated PDF size: {pdf_size} bytes")
|
| 788 |
+
|
| 789 |
+
if pdf_size == 0:
|
| 790 |
+
print("WARNING: Generated PDF has zero size!")
|
| 791 |
+
# Return a plain text version as fallback
|
| 792 |
+
return Response(
|
| 793 |
+
content=content.encode('utf-8'),
|
| 794 |
+
media_type="text/plain; charset=utf-8",
|
| 795 |
+
headers={
|
| 796 |
+
"Content-Disposition": f"attachment; filename={filename.replace('.pdf', '.txt')}",
|
| 797 |
+
"Content-Type": "text/plain; charset=utf-8"
|
| 798 |
+
}
|
| 799 |
+
)
|
| 800 |
+
|
| 801 |
+
# Return PDF content
|
| 802 |
return Response(
|
| 803 |
content=pdf_bytes.getvalue(),
|
| 804 |
media_type="application/pdf",
|
| 805 |
headers={"Content-Disposition": f"attachment; filename={filename}"}
|
| 806 |
)
|
|
|
|
| 807 |
except ImportError:
|
| 808 |
return JSONResponse(
|
| 809 |
status_code=501,
|
|
|
|
| 812 |
except Exception as e:
|
| 813 |
print(f"PDF creation error: {str(e)}")
|
| 814 |
traceback.print_exc()
|
| 815 |
+
# Return a text file as fallback
|
| 816 |
return Response(
|
| 817 |
content=content.encode('utf-8'),
|
| 818 |
media_type="text/plain; charset=utf-8",
|
project_report.md
CHANGED
|
@@ -479,7 +479,7 @@ function triggerDownload(url, filename) {
|
|
| 479 |
|
| 480 |
### 6.1. Dockerization
|
| 481 |
|
| 482 |
-
* **Base Image:** Uses an official `python:3.
|
| 483 |
* **Dependency Management:** Copies `requirements.txt` and installs dependencies early to leverage Docker caching.
|
| 484 |
* **Code Copying:** Copies the necessary application code (`backend`, `templates`, `static`) into the container.
|
| 485 |
* **Directory Creation:** Ensures necessary directories (`templates`, `static`, `uploads`) exist within the container.
|
|
|
|
| 479 |
|
| 480 |
### 6.1. Dockerization
|
| 481 |
|
| 482 |
+
* **Base Image:** Uses an official `python:3.12-slim` image for a smaller footprint.
|
| 483 |
* **Dependency Management:** Copies `requirements.txt` and installs dependencies early to leverage Docker caching.
|
| 484 |
* **Code Copying:** Copies the necessary application code (`backend`, `templates`, `static`) into the container.
|
| 485 |
* **Directory Creation:** Ensures necessary directories (`templates`, `static`, `uploads`) exist within the container.
|