File size: 4,181 Bytes
623e14e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python3
"""
Test script for DOCX to PDF conversion with Arabic RTL support
This script tests the conversion functionality locally
"""

import sys
import os
from pathlib import Path

# Add the current directory to Python path
sys.path.insert(0, str(Path(__file__).parent))

from app import convert_docx_to_pdf, setup_libreoffice, setup_font_environment

def test_arabic_conversion():
    """Test the Arabic DOCX to PDF conversion"""
    
    print("🧪 Testing Arabic DOCX to PDF Conversion")
    print("=" * 50)
    
    # Check LibreOffice setup
    print("1. Checking LibreOffice setup...")
    if not setup_libreoffice():
        print("❌ LibreOffice setup failed!")
        return False
    print("✅ LibreOffice setup successful")
    
    # Setup font environment
    print("\n2. Setting up font environment...")
    setup_font_environment()
    print("✅ Font environment setup completed")
    
    # Check for test files
    test_files_dir = Path("test_files")
    if not test_files_dir.exists():
        print(f"\n⚠️ Test files directory '{test_files_dir}' not found")
        print("Please create test_files/ directory and add sample DOCX files")
        return False
    
    docx_files = list(test_files_dir.glob("*.docx"))
    if not docx_files:
        print(f"\n⚠️ No DOCX files found in '{test_files_dir}'")
        print("Please add sample DOCX files to test the conversion")
        return False
    
    print(f"\n3. Found {len(docx_files)} DOCX files for testing:")
    for docx_file in docx_files:
        print(f"   📄 {docx_file.name}")
    
    # Test conversion for each file
    results_dir = Path("test_results")
    results_dir.mkdir(exist_ok=True)
    
    success_count = 0
    total_count = len(docx_files)
    
    for docx_file in docx_files:
        print(f"\n4. Testing conversion: {docx_file.name}")
        print("-" * 30)
        
        # Create a mock file object
        class MockFile:
            def __init__(self, path):
                self.name = str(path)
        
        mock_file = MockFile(docx_file)
        
        try:
            pdf_path, status_message = convert_docx_to_pdf(mock_file)
            
            if pdf_path and os.path.exists(pdf_path):
                # Move the result to test_results directory
                result_name = docx_file.stem + "_converted.pdf"
                result_path = results_dir / result_name
                
                import shutil
                shutil.move(pdf_path, result_path)
                
                print(f"✅ Conversion successful!")
                print(f"📁 Output: {result_path}")
                print(f"📊 Status: {status_message[:100]}...")
                success_count += 1
            else:
                print(f"❌ Conversion failed!")
                print(f"📊 Error: {status_message}")
                
        except Exception as e:
            print(f"❌ Conversion error: {str(e)}")
    
    # Summary
    print(f"\n🎯 Test Summary:")
    print(f"   Total files: {total_count}")
    print(f"   Successful: {success_count}")
    print(f"   Failed: {total_count - success_count}")
    print(f"   Success rate: {(success_count/total_count)*100:.1f}%")
    
    if success_count > 0:
        print(f"\n📁 Results saved in: {results_dir}")
    
    return success_count == total_count

def create_sample_test_files():
    """Create sample test files for testing"""
    test_files_dir = Path("test_files")
    test_files_dir.mkdir(exist_ok=True)
    
    print("📝 Creating sample test files...")
    print("Note: You need to manually create DOCX files with Arabic content")
    print("Suggested test cases:")
    print("1. Simple Arabic text document")
    print("2. Document with Arabic tables")
    print("3. Mixed Arabic/English document")
    print("4. Document with Arabic headers and footers")
    print("5. Document with Arabic bullet points")
    print(f"\nPlace your test DOCX files in: {test_files_dir.absolute()}")

if __name__ == "__main__":
    if len(sys.argv) > 1 and sys.argv[1] == "--create-samples":
        create_sample_test_files()
    else:
        test_arabic_conversion()