diff --git a/app/routes/inspections.py b/app/routes/inspections.py index 721ee6c..18fbf5e 100644 --- a/app/routes/inspections.py +++ b/app/routes/inspections.py @@ -913,18 +913,27 @@ def export_pdf(inspection_id): flash('Access denied.', 'danger') return redirect(url_for('inspections.index')) - template = inspection.template - form_fields = sorted(template.get_form_schema(), - key=lambda f: (f.get('row', 0), f.get('col', 0))) + template = inspection.template - form_data = {} + # Use the snapshot saved at submission time (same source as view()) so the + # PDF reflects the exact template the inspector saw, not a later revision. + form_fields = None + form_data = {} if inspection.notes: try: - parsed = json.loads(inspection.notes) - if isinstance(parsed, dict): - form_data = parsed.get('_form_data', {}) + _snap = json.loads(inspection.notes) + if isinstance(_snap, dict): + if '_template_schema' in _snap: + form_fields = sorted( + _snap['_template_schema'], + key=lambda f: (f.get('row', 0), f.get('col', 0)), + ) + form_data = _snap.get('_form_data') or {} except (json.JSONDecodeError, TypeError): pass + if form_fields is None: + form_fields = sorted(template.get_form_schema(), + key=lambda f: (f.get('row', 0), f.get('col', 0))) issues = inspection.issues.order_by(Issue.reported_at.desc()).all() static_folder = os.path.join(current_app.root_path, 'static')