add contract pdf generation

This commit is contained in:
2026-02-14 23:59:44 +01:00
parent 7e42fbe106
commit f440cef59e
42 changed files with 1299 additions and 123 deletions

View File

@@ -0,0 +1,58 @@
import jinja2
import src.models as models
import html
from weasyprint import HTML
def generate_html_contract(
form: models.Form,
contract_informations: dict,
planned: list[dict],
recurrent: list[dict],
recurrent_price: float,
total_price: float,
):
template_dir = "./src/contracts/templates"
template_loader = jinja2.FileSystemLoader(searchpath=template_dir)
template_env = jinja2.Environment(loader=template_loader, autoescape=jinja2.select_autoescape(["html", "xml"]))
template_file = "layout.html"
template = template_env.get_template(template_file)
output_text = template.render(
contract_name=form.name,
contract_type=form.productor.type,
contract_season=form.season,
referer_name=form.referer.name,
referer_email=form.referer.email,
productor_name=form.productor.name,
productor_address=form.productor.address,
payment_methods_map={"cheque": "Ordre du chèque", "transfer": "IBAN (paiement par virements)"},
productor_payment_methods=form.productor.payment_methods,
member_name=f'{html.escape(contract_informations["firstname"])} {html.escape(contract_informations["lastname"])}',
member_email=html.escape(contract_informations["email"]),
member_phone=html.escape(contract_informations["phone"]),
contract_start_date=form.start,
contract_end_date=form.end,
planned=planned,
recurrent=recurrent,
recurrent_price=recurrent_price,
total_price=total_price,
)
options = {
'page-size': 'Letter',
'margin-top': '0.5in',
'margin-right': '0.5in',
'margin-bottom': '0.5in',
'margin-left': '0.5in',
'encoding': "UTF-8",
'print-media-type': True,
"disable-javascript": True,
"disable-external-links": True,
'enable-local-file-access': False,
"disable-local-file-access": True,
"no-images": True,
}
return HTML(
string=output_text,
base_url=template_dir
).write_pdf()