|
|
|
|
|
""" |
|
|
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 |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
print("1. Checking LibreOffice setup...") |
|
|
if not setup_libreoffice(): |
|
|
print("❌ LibreOffice setup failed!") |
|
|
return False |
|
|
print("✅ LibreOffice setup successful") |
|
|
|
|
|
|
|
|
print("\n2. Setting up font environment...") |
|
|
setup_font_environment() |
|
|
print("✅ Font environment setup completed") |
|
|
|
|
|
|
|
|
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}") |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
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): |
|
|
|
|
|
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)}") |
|
|
|
|
|
|
|
|
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() |
|
|
|