Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
from playwright.sync_api import sync_playwright
|
||||
|
||||
from context import context_list
|
||||
|
||||
env = Environment(loader=FileSystemLoader("report_gen"))
|
||||
|
||||
html_pages = []
|
||||
|
||||
header_context = {
|
||||
"patient_name": "Keirstyn Moran",
|
||||
"age": 34,
|
||||
"height": "5'4\"",
|
||||
"weight": "123lbs",
|
||||
"focus": "Endurance",
|
||||
}
|
||||
|
||||
footer_context = [
|
||||
{
|
||||
"contact_email": "info@ishplabs.com ",
|
||||
"website": "www.ishplabs.com",
|
||||
"social": "@ishplabs",
|
||||
"page_number": i + 1,
|
||||
}
|
||||
for i in range(len(context_list))
|
||||
]
|
||||
|
||||
|
||||
header_html = env.get_template("header.html").render(header_context)
|
||||
footer_html_list = [
|
||||
env.get_template("footer.html").render(context) for context in footer_context
|
||||
]
|
||||
|
||||
for i, context in enumerate(context_list):
|
||||
template = env.get_template(f"page_{i + 1}.html").render(context)
|
||||
|
||||
if (i + 1) > 2:
|
||||
full_html = f"""
|
||||
<div class="page flex flex-col justify-between">
|
||||
<div>
|
||||
{header_html}
|
||||
</div>
|
||||
<main class="flex-grow p-4">
|
||||
{template}
|
||||
</main>
|
||||
<div class="border-t text-center text-sm text-gray-600">
|
||||
{footer_html_list[i]}
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
html_pages.append(full_html)
|
||||
else:
|
||||
html_pages.append(template)
|
||||
|
||||
# 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>
|
||||
html, body {{
|
||||
height: 100%;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}}
|
||||
.page-break {{ page-break-after: always; }}
|
||||
.page {{
|
||||
height: 100vh;
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}}
|
||||
.page main {{
|
||||
flex: 1;
|
||||
overflow: hidden;
|
||||
}}
|
||||
/* Reset margins and padding everywhere */
|
||||
* {{
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}}
|
||||
/* Prevent images from being too large */
|
||||
img {{
|
||||
max-height: 300px;
|
||||
}}
|
||||
/* Larger images for specific charts */
|
||||
.chart-large {{
|
||||
max-height: 500px !important;
|
||||
}}
|
||||
</style>
|
||||
</head>
|
||||
<body class="m-0 p-0">
|
||||
{final_html}
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
|
||||
# Generate PDF
|
||||
|
||||
|
||||
def html_string_to_pdf(html_content, pdf_path):
|
||||
with sync_playwright() as p:
|
||||
browser = p.chromium.launch()
|
||||
page = browser.new_page()
|
||||
|
||||
# Set the HTML directly
|
||||
page.set_content(html_content)
|
||||
|
||||
# Export to PDF
|
||||
page.pdf(path=pdf_path, format="A4", print_background=True)
|
||||
|
||||
browser.close()
|
||||
|
||||
|
||||
html_string_to_pdf(html_doc, "multi_page_report.pdf")
|
||||
# pdfkit.from_string(html_doc, "truth_report.pdf", options=options)
|
||||
|
||||
print("✅ PDF generated: multi_page_report.pdf")
|
||||
Reference in New Issue
Block a user