Add initial HTML structure for report generation
- Created page_1.html with layout including ISHP branding, main content, and dotted pattern. - Developed page_2.html featuring a Table of Contents with sections for Lung Analysis, Cardio Metrics, Fuel Utilization, Local Muscle Activity, Training Recommendations, Next Steps, and Glossary. - Added placeholder files for pages 3 to 19 to facilitate future content development.
This commit is contained in:
@@ -1,38 +1,54 @@
|
||||
import pdfkit
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
from weasyprint import HTML
|
||||
import matplotlib.pyplot as plt
|
||||
import os
|
||||
|
||||
# 1. Generate a chart with matplotlib
|
||||
os.makedirs("static/charts", exist_ok=True)
|
||||
chart_path = "static/charts/resp_chart.png"
|
||||
env = Environment(loader=FileSystemLoader("report_gen"))
|
||||
|
||||
plt.plot([1, 2, 3, 4], [1, 4, 2, 5], label="Breath Volume")
|
||||
plt.legend()
|
||||
plt.title("Respiratory Chart")
|
||||
plt.savefig(chart_path)
|
||||
plt.close()
|
||||
# Define templates and their unique contexts
|
||||
pages = [
|
||||
("page_1.html", {"name": "John Doe", "surname": "Moran", "date": "July 29, 2025"}),
|
||||
("page_2.html", {"content": "This is page 2 content"}),
|
||||
]
|
||||
|
||||
# 2. Patient data (this would usually come from your DB)
|
||||
patient_data = {
|
||||
"name": "Keirstyn Moran",
|
||||
"age": 34,
|
||||
"height": 163,
|
||||
"weight": 56
|
||||
# Render each template with its own context
|
||||
html_pages = []
|
||||
for tpl, ctx in pages:
|
||||
template = env.get_template(tpl)
|
||||
html_pages.append(template.render(ctx))
|
||||
|
||||
# Combine with page breaks
|
||||
final_html = "<div class='page-break'></div>".join(html_pages)
|
||||
|
||||
# Wrap in full HTML document
|
||||
html_doc = f"""
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<link href="https://cdn.jsdelivr.net/npm/tailwindcss/dist/tailwind.min.css" rel="stylesheet">
|
||||
<style>
|
||||
.page-break {{ page-break-after: always; }}
|
||||
.page {{
|
||||
height: 386mm;
|
||||
}}
|
||||
</style>
|
||||
</head>
|
||||
<body class="m-0 p-0">
|
||||
{final_html}
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
print(html_doc)
|
||||
# Generate PDF
|
||||
options = {
|
||||
"page-size": "A4",
|
||||
"encoding": "UTF-8",
|
||||
"margin-top": "0mm",
|
||||
"margin-bottom": "0mm",
|
||||
"margin-left": "0mm",
|
||||
"margin-right": "0mm",
|
||||
"no-outline": None,
|
||||
}
|
||||
pdfkit.from_string(html_doc, "multi_page_report.pdf", options=options)
|
||||
|
||||
context = {
|
||||
"patient": patient_data,
|
||||
"indications": "No Respiratory Capacity Limitation",
|
||||
"chart_path": chart_path
|
||||
}
|
||||
|
||||
# 3. Render Jinja2 template
|
||||
env = Environment(loader=FileSystemLoader("templates"))
|
||||
template = env.get_template("report.html")
|
||||
html_out = template.render(context)
|
||||
|
||||
# 4. Generate PDF
|
||||
HTML(string=html_out, base_url=".").write_pdf("lung_report.pdf")
|
||||
|
||||
print("✅ Report generated: lung_report.pdf")
|
||||
print("✅ PDF generated: multi_page_report.pdf")
|
||||
|
||||
Reference in New Issue
Block a user