pdf-4 / test_template_conversion.py
fokan's picture
Initial commit with static file serving and inline PDF viewing
623e14e
#!/usr/bin/env python3
"""
Test script for template.docx conversion with specific font sizes
Tests the new Arial font integration and font size preservation
"""
import os
import sys
from pathlib import Path
import tempfile
import shutil
# Add current directory to path to import app module
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from app import (
setup_libreoffice,
validate_docx_structure,
preprocess_docx_for_perfect_conversion,
analyze_template_font_sizes,
setup_local_arial_font
)
def test_arial_font_setup():
"""Test local Arial font setup"""
print("🔤 Testing local Arial font setup...")
# Check if Arial font exists in fonts directory
arial_path = Path("fonts/arial.ttf")
if not arial_path.exists():
print(f"❌ Arial font not found at {arial_path}")
return False
print(f"✅ Arial font found at {arial_path}")
# Test font setup function
result = setup_local_arial_font()
if result:
print("✅ Local Arial font setup successful")
else:
print("❌ Local Arial font setup failed")
return result
def test_template_analysis():
"""Test template.docx analysis for font sizes"""
print("\n📏 Testing template.docx font size analysis...")
template_path = Path("template.docx")
if not template_path.exists():
print(f"❌ Template file not found at {template_path}")
return False
print(f"✅ Template file found at {template_path}")
# Test font size analysis
font_mapping = analyze_template_font_sizes(str(template_path))
if font_mapping:
print(f"✅ Font size analysis successful - {len(font_mapping)} patterns found:")
for text, size in list(font_mapping.items())[:10]: # Show first 10
print(f" • '{text[:30]}...' → {size}pt")
if len(font_mapping) > 10:
print(f" • ... and {len(font_mapping) - 10} more patterns")
else:
print("❌ Font size analysis failed")
return bool(font_mapping)
def test_docx_validation():
"""Test DOCX structure validation with template"""
print("\n🔍 Testing DOCX structure validation...")
template_path = Path("template.docx")
if not template_path.exists():
print(f"❌ Template file not found at {template_path}")
return False
# Test validation function
validation_info = validate_docx_structure(str(template_path))
print("✅ DOCX validation completed:")
print(f" • Has tables: {validation_info.get('has_tables', False)}")
print(f" • Has images: {validation_info.get('has_images', False)}")
print(f" • Text length: {validation_info.get('text_content_length', 0)} chars")
print(f" • Font families: {len(validation_info.get('font_families', set()))}")
print(f" • RTL content: {validation_info.get('rtl_content_detected', False)}")
print(f" • Placeholders: {validation_info.get('placeholder_count', 0)}")
print(f" • Font mapping: {len(validation_info.get('font_size_mapping', {}))}")
return True
def test_preprocessing():
"""Test DOCX preprocessing with font settings"""
print("\n🔧 Testing DOCX preprocessing...")
template_path = Path("template.docx")
if not template_path.exists():
print(f"❌ Template file not found at {template_path}")
return False
# First validate the structure
validation_info = validate_docx_structure(str(template_path))
# Test preprocessing
try:
processed_path = preprocess_docx_for_perfect_conversion(str(template_path), validation_info)
if processed_path != str(template_path):
print(f"✅ Preprocessing applied - new file: {processed_path}")
# Check if processed file exists
if Path(processed_path).exists():
print(f"✅ Processed file exists and is accessible")
# Clean up temporary file
try:
os.unlink(processed_path)
print("✅ Temporary file cleaned up")
except:
pass
else:
print(f"❌ Processed file not found")
return False
else:
print("ℹ️ No preprocessing needed - file structure is optimal")
return True
except Exception as e:
print(f"❌ Preprocessing failed: {e}")
return False
def test_libreoffice_setup():
"""Test LibreOffice setup"""
print("\n⚙️ Testing LibreOffice setup...")
result = setup_libreoffice()
if result:
print("✅ LibreOffice setup successful")
else:
print("❌ LibreOffice setup failed")
return result
def main():
"""Run all tests"""
print("🧪 Starting Template Conversion Tests")
print("=" * 50)
tests = [
("Arial Font Setup", test_arial_font_setup),
("Template Analysis", test_template_analysis),
("DOCX Validation", test_docx_validation),
("DOCX Preprocessing", test_preprocessing),
("LibreOffice Setup", test_libreoffice_setup),
]
results = {}
for test_name, test_func in tests:
try:
results[test_name] = test_func()
except Exception as e:
print(f"❌ {test_name} failed with exception: {e}")
results[test_name] = False
# Summary
print("\n" + "=" * 50)
print("📊 Test Results Summary:")
passed = 0
total = len(tests)
for test_name, result in results.items():
status = "✅ PASS" if result else "❌ FAIL"
print(f" {status} - {test_name}")
if result:
passed += 1
print(f"\n🎯 Overall: {passed}/{total} tests passed ({passed/total*100:.1f}%)")
if passed == total:
print("🌟 All tests passed! Template conversion system is ready.")
elif passed >= total * 0.8:
print("👍 Most tests passed. System should work with minor issues.")
else:
print("⚠️ Several tests failed. Please check the setup.")
return passed == total
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)