Skip to content

Commit 7bc3b48

Browse files
committed
Change feedback transcript format from HTML to PDF; Include simple styling for feedback modal; Update formatting for PDF file in both export and feedback features; Include new library xhtml2pdf to do HTML to PDF conversion
1 parent e355115 commit 7bc3b48

File tree

6 files changed

+530
-22
lines changed

6 files changed

+530
-22
lines changed

backend/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ requires-python = ">=3.12"
55
dependencies = [
66
"flask",
77
"Flask-Mail",
8+
"xhtml2pdf",
89
"valkey",
910
"gunicorn",
1011
"openai==1.89",

backend/tenantfirstaid/feedback.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
1+
from xhtml2pdf import pisa
2+
from io import BytesIO
13
from flask import request
24
from flask_mail import Message
3-
from werkzeug.utils import secure_filename
45
import os
56

67

8+
def convert_html_to_pdf(html_content):
9+
pdf_buffer = BytesIO()
10+
pisa_status = pisa.CreatePDF(html_content, dest=pdf_buffer)
11+
if pisa_status.err:
12+
return None
13+
return pdf_buffer.getvalue()
14+
15+
716
def send_feedback(mail):
817
feedback = request.form.get("feedback")
918
file = request.files.get("transcript")
1019

1120
if not file:
1221
return "No file provided", 400
1322

14-
filename = secure_filename(file.filename)
15-
filepath = os.path.join("/tmp", filename)
16-
file.save(filepath)
17-
18-
with open(filepath, "r", encoding="utf-8") as f:
19-
html_content = f.read()
23+
html_content = file.read().decode("utf-8")
24+
pdf_data = convert_html_to_pdf(html_content)
25+
if pdf_data is None:
26+
return "PDF conversion failed", 500
2027

2128
msg = Message(
2229
subject="Feedback with Transcript",
@@ -25,12 +32,11 @@ def send_feedback(mail):
2532
body=f"User feedback:\n\n{feedback}",
2633
)
2734
msg.attach(
28-
filename="transcript.html",
29-
content_type="text/html",
30-
data=html_content.encode("utf-8"),
35+
filename="transcript.pdf",
36+
content_type="application/pdf",
37+
data=pdf_data,
3138
)
3239

3340
mail.send(msg)
34-
os.remove(filepath)
3541

3642
return "Email sent", 200

0 commit comments

Comments
 (0)