amine_dubs commited on
Commit
1913c15
·
1 Parent(s): 00634bc
Files changed (1) hide show
  1. backend/main.py +82 -64
backend/main.py CHANGED
@@ -728,87 +728,105 @@ async def download_translated_document(request: Request):
728
 
729
  elif filename.endswith('.pdf'):
730
  try:
731
- import fitz # PyMuPDF
732
- from io import BytesIO
733
-
734
- # Create a new PDF document
735
- doc = fitz.open()
736
- page = doc.new_page()
737
-
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,
810
- content={"success": False, "error": "PDF creation requires PyMuPDF library"}
811
- )
812
  except Exception as e:
813
  print(f"PDF creation error: {str(e)}")
814
  traceback.print_exc()
 
728
 
729
  elif filename.endswith('.pdf'):
730
  try:
731
+ # For PDF files, let's use a very basic approach with a text-based fallback
732
+ # Try to create a simple PDF with reportlab, which should be available
 
 
 
 
 
 
 
 
 
733
  try:
734
+ from reportlab.pdfgen import canvas
735
+ from reportlab.lib.pagesizes import letter
736
+ from io import BytesIO
737
+ from reportlab.pdfbase import pdfmetrics
738
+ from reportlab.pdfbase.ttfonts import TTFont
739
+ from reportlab.lib.colors import black
740
+
741
+ # Create a PDF in memory
742
+ buffer = BytesIO()
743
+ c = canvas.Canvas(buffer, pagesize=letter)
744
+
745
+ # Try to register a font that supports Arabic
746
+ try:
747
+ # Try to use a system font that supports Arabic
748
+ pdfmetrics.registerFont(TTFont('Arabic', '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'))
749
+ font_name = 'Arabic'
750
+ except:
751
+ # Default to built-in Helvetica which has limited Arabic support
752
+ font_name = 'Helvetica'
753
 
754
+ # Set font
755
+ c.setFont(font_name, 12)
756
+
757
+ # Check if text contains Arabic
758
+ has_arabic = any('\u0600' <= ch <= '\u06FF' for ch in content)
759
+
760
+ # Split text into lines
761
  lines = content.split('\n')
762
+ y_position = 750 # Start from top
763
 
764
+ # Draw text with proper handling for Arabic
765
  for line in lines:
766
  if line.strip():
767
+ # For Arabic, we write from right to left
 
768
  if has_arabic:
769
+ # Right-aligned text
770
+ text_width = c.stringWidth(line, font_name, 12)
771
+ c.drawString(letter[0] - 50 - text_width, y_position, line)
 
 
 
 
 
 
 
772
  else:
773
+ # Left-aligned text
774
+ c.drawString(50, y_position, line)
775
+ y_position -= 14
776
+
777
+ # Add a new page if we reach the bottom
778
+ if y_position < 50:
779
+ c.showPage()
780
+ y_position = 750
781
+
782
+ c.save()
783
+
784
+ # Get PDF content
785
+ pdf_content = buffer.getvalue()
786
+ buffer.close()
787
+
788
+ # Return PDF
789
+ return Response(
790
+ content=pdf_content,
791
+ media_type="application/pdf",
792
+ headers={"Content-Disposition": f"attachment; filename={filename}"}
793
+ )
794
+ except ImportError:
795
+ print("Reportlab not available, trying with PyMuPDF")
796
+ import fitz # PyMuPDF
797
+ from io import BytesIO
798
+
799
+ # Create a new PDF
800
+ doc = fitz.open()
801
+ page = doc.new_page()
802
+
803
+ # Add text - keep it very simple
804
+ page.insert_text((72, 72), content)
805
+
806
+ # Save PDF
807
+ pdf_bytes = BytesIO()
808
+ doc.save(pdf_bytes)
809
+ pdf_bytes.seek(0)
810
+ doc.close()
811
+
812
+ return Response(
813
+ content=pdf_bytes.getvalue(),
814
+ media_type="application/pdf",
815
+ headers={"Content-Disposition": f"attachment; filename={filename}"}
816
+ )
817
+
818
  except Exception as e:
819
+ print(f"PDF creation error: {str(e)}")
820
  traceback.print_exc()
821
+ # Fallback to text file
 
 
 
 
 
 
 
 
 
 
 
 
 
822
  return Response(
823
  content=content.encode('utf-8'),
824
+ media_type="text/plain; charset=utf-8",
825
  headers={
826
  "Content-Disposition": f"attachment; filename={filename.replace('.pdf', '.txt')}",
827
  "Content-Type": "text/plain; charset=utf-8"
828
  }
829
  )
 
 
 
 
 
 
 
 
 
 
 
 
830
  except Exception as e:
831
  print(f"PDF creation error: {str(e)}")
832
  traceback.print_exc()