File size: 5,356 Bytes
86fce4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
"""

Simple test runner for template.docx conversion

Tests only the core functionality without LibreOffice

"""

import os
import sys
from pathlib import Path

# Add current directory to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from app import (
    validate_docx_structure, 
    preprocess_docx_for_perfect_conversion,
    analyze_template_font_sizes,
    setup_local_arial_font
)

def main():
    """Test the template conversion system"""
    print("🎯 Template.docx Conversion System Test")
    print("=" * 50)

    # Get script directory
    script_dir = Path(__file__).parent.absolute()
    print(f"📁 Script directory: {script_dir}")

    # Check files
    print("📁 Checking required files...")

    arial_path = script_dir / "arial.ttf"
    template_path = script_dir / "template.docx"
    
    if not arial_path.exists():
        print(f"❌ Arial font not found: {arial_path}")
        return False
    print(f"✅ Arial font found: {arial_path}")
    
    if not template_path.exists():
        print(f"❌ Template not found: {template_path}")
        return False
    print(f"✅ Template found: {template_path}")
    
    # Test Arial font setup
    print("\n🔤 Setting up Arial font...")
    if setup_local_arial_font():
        print("✅ Arial font setup successful")
    else:
        print("⚠️ Arial font setup had issues (may still work)")
    
    # Test template analysis
    print("\n📏 Analyzing template font sizes...")
    font_mapping = analyze_template_font_sizes(str(template_path))
    
    if font_mapping:
        print(f"✅ Found {len(font_mapping)} text patterns with font sizes")
        
        # Show specific patterns we care about
        important_patterns = {
            'size_12': ['{{serial_number}}', '{{date}}', 'الرقم التسلسلي', 'التاريخ'],
            'size_13': ['{{name_1}}', '{{location_1}}', 'اسم المالك', 'يسكن'],
            'size_14': ['الطرف البائع', 'الطرف المشتري']
        }
        
        for size_name, patterns in important_patterns.items():
            found_patterns = []
            for pattern in patterns:
                for text, size in font_mapping.items():
                    if pattern in text:
                        found_patterns.append(f"{pattern}{size}pt")
                        break
            
            if found_patterns:
                print(f"  • {size_name}: {', '.join(found_patterns[:3])}")
    else:
        print("❌ Font size analysis failed")
        return False
    
    # Test DOCX validation
    print("\n🔍 Validating DOCX structure...")
    validation_info = validate_docx_structure(str(template_path))
    
    print(f"✅ Validation completed:")
    print(f"  • Tables: {validation_info.get('has_tables', False)}")
    print(f"  • Images: {validation_info.get('has_images', False)}")
    print(f"  • RTL content: {validation_info.get('rtl_content_detected', False)}")
    print(f"  • Placeholders: {validation_info.get('placeholder_count', 0)}")
    print(f"  • Font families: {len(validation_info.get('font_families', set()))}")
    
    # Test preprocessing
    print("\n🔧 Testing preprocessing...")
    try:
        processed_path = preprocess_docx_for_perfect_conversion(str(template_path), validation_info)
        
        if processed_path != str(template_path):
            print("✅ Preprocessing applied successfully")
            print(f"  • Font settings applied")
            print(f"  • Arial font set as default")
            print(f"  • Specific font sizes applied")
            
            # Clean up
            try:
                os.unlink(processed_path)
                print("  • Temporary file cleaned up")
            except:
                pass
        else:
            print("ℹ️ No preprocessing needed")
        
    except Exception as e:
        print(f"❌ Preprocessing failed: {e}")
        return False
    
    # Summary
    print("\n" + "=" * 50)
    print("🎉 Template Conversion System Ready!")
    print("\n📋 Summary:")
    print("✅ Arial font from fonts/ directory will be used")
    print("✅ Font sizes will be preserved:")
    print("   • Size 12: Serial numbers, dates, times")
    print("   • Size 13: Names, IDs, locations, phones")
    print("   • Size 14: 'الطرف البائع', 'الطرف المشتري'")
    print("   • Size 12: All other text (default)")
    print("✅ RTL Arabic text will be handled correctly")
    print("✅ Tables and images will be preserved")
    print(f"✅ {validation_info.get('placeholder_count', 0)} placeholders will be maintained")
    
    print("\n🚀 To use the system:")
    print("1. Run: python app.py")
    print("2. Open the Gradio interface")
    print("3. Upload template.docx")
    print("4. Download the converted PDF")
    
    return True

if __name__ == "__main__":
    success = main()
    if success:
        print("\n✅ All tests passed! System is ready to use.")
    else:
        print("\n❌ Some tests failed. Please check the setup.")
    
    input("\nPress Enter to exit...")
    sys.exit(0 if success else 1)