from typing import Optional, Dict, Any, List from datetime import datetime import os import logging logger = logging.getLogger(__name__) try: from weasyprint import HTML HAS_WEASYPRINT = True except ImportError: HAS_WEASYPRINT = False logger.warning("weasyprint not installed, PDF generation disabled") QUOTATION_TEMPLATE = """
#{quotation_number}
{customer_name}
{customer_company}
{customer_country}
Date: {date}
Valid Until: {valid_until}
Currency: {currency}
| Item | Description | Qty | Unit | Unit Price | Total |
|---|
| Subtotal: | {subtotal} | ||||
| Discount: | -{discount} | ||||
| Shipping: | {shipping} | ||||
| TOTAL: | {total} | ||||
Payment Terms: {payment_terms}
Delivery Terms: {delivery_terms}
Lead Time: {lead_time}
{notes_html}Notes: {data['notes']}
" html = QUOTATION_TEMPLATE.format( quotation_number=data.get("quotation_number", "N/A"), customer_name=data.get("customer_name", ""), customer_company=data.get("customer_company", "") or "", customer_country=data.get("customer_country", "") or "", date=data.get("date", datetime.utcnow().strftime("%Y-%m-%d")), valid_until=data.get("valid_until", "N/A"), currency=cur, items_rows=items_rows, subtotal=subtotal, discount=discount, shipping=shipping, total=total, payment_terms=data.get("payment_terms", "N/A"), delivery_terms=data.get("delivery_terms", "N/A"), lead_time=data.get("lead_time", "N/A"), notes_html=notes_html, generated_at=datetime.utcnow().strftime("%Y-%m-%d %H:%M UTC"), ) pdf = HTML(string=html).write_pdf() return pdf pdf_generator = PDFGenerator()