"""PDF generation utilities using WeasyPrint."""
import os
from flask import current_app
from weasyprint import HTML
from jinja2 import Template
def generate_pdf(inspection):
"""
Generate a PDF report for the given inspection.
Returns the PDF bytes.
"""
# Load HTML template
template_dir = os.path.join(current_app.root_path, 'templates')
html_template = os.path.join(template_dir, 'inspection_report.html')
# Simple HTML content
html_content = f"""
Reference: {{ reference_number }}
Version: {{ version }}
Installation: {{ installation_name }}
Location: {{ location }}
Date: {{ inspection_date.strftime('%Y-%m-%d') }}
Conclusion: {{ conclusion_text }}
Status: {{ conclusion_status }}
Observations: {{ observations }}
Inspectors:
{% for inspector in inspectors %}
{{ inspector.full_name or inspector.free_text_name }},
{% endfor %}
{% for photo in photos %}

{{ caption }}
{% endfor %}
"""
# Generate PDF
html = HTML(string=html_content)
pdf_bytes = html.write_pdf()
return pdf_bytes