"""Branded business-letter PDF renderer."""
from html import escape
from pathlib import Path
from reportlab.lib.units import mm
from reportlab.platypus import Paragraph,Spacer,Table,TableStyle
from roborto_accounting.persistence.models import BusinessLetter,BusinessSettings
from roborto_accounting.reporting.documents.theme import DocumentTheme
def render_letter_pdf(path:Path,*,letter:BusinessLetter,settings:BusinessSettings,theme:DocumentTheme)->Path:
contact=[value for value in (settings.postal_address,settings.general_email,settings.website,settings.telephone) if value]
recipient="
".join(escape(value) for value in (letter.recipient_name,*letter.recipient_address.splitlines(),letter.recipient_email) if value)
story=theme.header(contact)
story.extend([Paragraph(letter.letter_date.strftime("%d %B %Y"),theme.body),Spacer(1,10*mm),Paragraph("TO",theme.section),Paragraph(recipient,theme.body),Spacer(1,10*mm)])
if letter.reference:story.extend([Paragraph("REFERENCE",theme.section),Paragraph(escape(letter.reference),theme.body),Spacer(1,4*mm)])
story.extend([Paragraph("SUBJECT",theme.section),Paragraph(f"{escape(letter.subject)}",theme.body),Spacer(1,9*mm)])
for block in letter.body_text.split("\n\n"):
story.extend([Paragraph(escape(block).replace("\n","
"),theme.body),Spacer(1,5*mm)])
sender=settings.trading_name or "roborto"
story.extend([Spacer(1,4*mm),Paragraph("Kind regards,",theme.body),Spacer(1,5*mm),Paragraph(f"{escape(sender)}",theme.body)])
if settings.business_descriptor:story.append(Paragraph(escape(settings.business_descriptor),theme.body))
return theme.build(path,story)