from datetime import datetime from io import BytesIO from pathlib import Path from typing import Dict, Any, List from markdown import markdown import site from fpdf import FPDF from open_webui.env import STATIC_DIR, FONTS_DIR from open_webui.apps.webui.models.chats import ChatTitleMessagesForm class PDFGenerator: """ Description: The `PDFGenerator` class is designed to create PDF documents from chat messages. The process involves transforming markdown content into HTML and then into a PDF format Attributes: - `form_data`: An instance of `ChatTitleMessagesForm` containing title and messages. """ def __init__(self, form_data: ChatTitleMessagesForm): self.html_body = None self.messages_html = None self.form_data = form_data self.css = Path(STATIC_DIR / "assets" / "pdf-style.css").read_text() def format_timestamp(self, timestamp: float) -> str: """Convert a UNIX timestamp to a formatted date string.""" try: date_time = datetime.fromtimestamp(timestamp) return date_time.strftime("%Y-%m-%d, %H:%M:%S") except (ValueError, TypeError) as e: # Log the error if necessary return "" def _build_html_message(self, message: Dict[str, Any]) -> str: """Build HTML for a single message.""" role = message.get("role", "user") content = message.get("content", "") timestamp = message.get("timestamp") model = message.get("model") if role == "assistant" else "" date_str = self.format_timestamp(timestamp) if timestamp else "" # extends pymdownx extension to convert markdown to html. # - https://facelessuser.github.io/pymdown-extensions/usage_notes/ html_content = markdown(content, extensions=["pymdownx.extra"]) html_message = f"""
""" return html_message def _generate_html_body(self) -> str: """Generate the full HTML body for the PDF.""" return f"""