Files
capstone_msagi/utilities/pdf_tools.py
T

61 lines
1.8 KiB
Python

html_template = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
/* Existing styles... */
/* Styles for Links */
a {{
color: #0000ff; /* Sets the link color to blue */
text-decoration: underline; /* Ensures the underline is visible */
}}
a:visited {{
color: #0000ff; /* Keeps the link blue even after it is clicked */
}}
/* Styles for clean Table Lines */
table {{
width: 100%;
border-collapse: collapse; /* Merges adjacent cell borders into a single line */
margin: 20px 0;
font-size: 11pt;
}}
th, td {{
border: 1px solid #bdc3c7; /* Draws the actual grid lines */
padding: 10px;
text-align: left;
}}
th {{
background-color: #f2f4f4; /* Optional: adds a neat background header color */
font-weight: bold;
color: #2c3e50;
}}
tr:nth-child(even) {{
background-color: #f9f9f9; /* Optional: adds zebra striping to rows */
}}
</style>
</head>
<body>
{content}
</body>
</html>
"""
import markdown
from weasyprint import HTML
def export_to_pdf(inputText, filename):
# Step 1: Convert Markdown components to HTML chunks.
# The 'tables' and 'fenced_code' extensions keep standard LLM formatting neat.
html_content = markdown.markdown(inputText, extensions=['tables', 'fenced_code'])
# Step 2: Inject the content into our CSS-styled HTML boilerplate template
final_html = html_template.format(content=html_content)
# Step 3: Render directly to a high-fidelity PDF file
HTML(string=final_html).write_pdf(filename)
#print(f"Successfully generated styled PDF at: {output_pdf_path}")