| """ |
| Module pour la génération des contrats en format PDF. |
| Module optimisé pour une génération plus complète et détaillée. |
| """ |
| import io |
| import reportlab |
| from reportlab.pdfgen import canvas |
| from reportlab.lib.pagesizes import A4 |
| from reportlab.lib.units import mm |
| from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle |
| from reportlab.pdfbase import pdfform |
| from reportlab.lib.colors import black, white |
| from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle |
| from reportlab.lib.enums import TA_CENTER, TA_JUSTIFY, TA_LEFT |
| from reportlab.pdfbase import pdfmetrics |
| from reportlab.pdfbase.ttfonts import TTFont |
| from reportlab.lib.styles import getSampleStyleSheet |
| import time |
|
|
| from config import PDF_CONFIG |
| from contract_builder import ContractBuilder |
| from utils import create_temp_file, ensure_default_supports |
| from contract_templates import ContractTemplates |
|
|
| pdfmetrics.registerFont(TTFont('Vera', 'Vera.ttf')) |
| pdfmetrics.registerFont(TTFont('VeraBd', 'VeraBd.ttf')) |
|
|
|
|
|
|
| def generate_pdf(contract_type, is_free, author_type, author_info, |
| work_description, image_description, supports, |
| additional_rights, remuneration, is_exclusive): |
| """ |
| Génère un PDF du contrat avec des champs interactifs. |
| """ |
| |
| is_free_bool = (is_free == "Gratuite") |
| |
| is_exclusive_bool = bool(is_exclusive) and not is_free_bool |
| |
| |
| final_additional_rights = [] if is_free_bool else additional_rights |
| |
| |
| final_supports = ensure_default_supports(supports) |
| |
| |
| output_filename = create_temp_file(prefix="contrat_cession_", suffix=".pdf") |
| |
| |
| contract_elements = ContractBuilder.build_contract_elements( |
| contract_type, is_free_bool, author_type, author_info, |
| work_description, image_description, final_supports, |
| final_additional_rights, remuneration, is_exclusive_bool |
| ) |
| |
| |
| buffer = io.BytesIO() |
| |
| |
| doc = SimpleDocTemplate( |
| buffer, |
| pagesize=A4, |
| rightMargin=15*mm, |
| leftMargin=15*mm, |
| topMargin=15*mm, |
| bottomMargin=15*mm |
| ) |
| |
| |
| doc.build(contract_elements) |
| |
| |
| buffer.seek(0) |
| |
| |
| p = canvas.Canvas(output_filename, pagesize=A4) |
| form = p.acroForm |
| |
| |
| if "Auteur (droits d'auteur)" in contract_type and "Image (droit à l'image)" in contract_type: |
| cedant_label = "l'Auteur et Modèle" |
| elif "Auteur (droits d'auteur)" in contract_type: |
| cedant_label = "l'Auteur" |
| else: |
| cedant_label = "le Modèle" |
| |
| |
| form.textfield(name='lieu', tooltip='Lieu de signature', |
| x=80, y=120, width=100, height=15, |
| borderWidth=0, forceBorder=True) |
| |
| form.textfield(name='date', tooltip='Date de signature', |
| x=230, y=120, width=100, height=15, |
| borderWidth=0, forceBorder=True) |
| |
| form.textfield(name='mention_cedant', tooltip='Mention "Lu et approuvé"', |
| x=70, y=95, width=150, height=15, |
| borderWidth=0, forceBorder=True) |
| |
| form.textfield(name='mention_cessionnaire', tooltip='Mention "Lu et approuvé"', |
| x=350, y=95, width=150, height=15, |
| borderWidth=0, forceBorder=True) |
| |
| form.textfield(name='signature_cedant', tooltip=f'Signature de {cedant_label}', |
| x=70, y=60, width=150, height=30, |
| borderWidth=0, forceBorder=True) |
| |
| form.textfield(name='signature_cessionnaire', tooltip='Signature du Cessionnaire', |
| x=350, y=60, width=150, height=30, |
| borderWidth=0, forceBorder=True) |
| |
| |
| |
| |
| |
| p.save() |
| |
| |
| with open(output_filename, 'wb') as f: |
| f.write(buffer.getvalue()) |
| |
| return output_filename |
|
|
|
|
| def get_simplified_styles(): |
| """ |
| Retourne des styles simplifiés pour une génération plus rapide. |
| |
| Returns: |
| dict: Dictionnaire des styles simplifiés |
| """ |
| styles = getSampleStyleSheet() |
| |
| styles.add(ParagraphStyle(name='ContractTitle', |
| fontName='VeraBd', |
| fontSize=14, |
| alignment=TA_CENTER, |
| spaceAfter=10)) |
| styles.add(ParagraphStyle(name='ContractText', |
| fontName='Vera', |
| fontSize=10, |
| alignment=TA_JUSTIFY, |
| spaceAfter=5)) |
| styles.add(ParagraphStyle(name='ContractArticle', |
| fontName='VeraBd', |
| fontSize=11, |
| spaceAfter=5)) |
| return styles |