|
""" |
|
Module pour la génération des contrats en format PDF. |
|
""" |
|
import io |
|
from reportlab.pdfgen import canvas |
|
from reportlab.lib.pagesizes import A4 |
|
from reportlab.lib.units import mm |
|
from reportlab.platypus import SimpleDocTemplate |
|
from reportlab.pdfbase import pdfform |
|
from reportlab.lib.colors import black |
|
|
|
from config import PDF_CONFIG |
|
from contract_builder import ContractBuilder |
|
from utils import create_temp_file, ensure_default_supports |
|
|
|
|
|
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. |
|
|
|
Args: |
|
contract_type (list): Liste des types de contrats sélectionnés |
|
is_free (bool): True si la cession est gratuite, False sinon |
|
author_type (str): Type d'auteur ("Personne physique" ou "Personne morale") |
|
author_info (dict): Informations sur l'auteur |
|
work_description (str): Description de l'œuvre |
|
image_description (str): Description de l'image |
|
supports (list): Liste des supports sélectionnés |
|
additional_rights (list): Liste des droits supplémentaires sélectionnés |
|
remuneration (str): Modalités de rémunération |
|
is_exclusive (bool): True si la cession est exclusive, False sinon |
|
|
|
Returns: |
|
str: Chemin vers le fichier PDF généré |
|
""" |
|
|
|
is_free_bool = (is_free == "Gratuite") |
|
is_exclusive_bool = bool(is_exclusive) |
|
|
|
|
|
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, |
|
additional_rights, remuneration, is_exclusive_bool |
|
) |
|
|
|
|
|
buffer = io.BytesIO() |
|
doc = SimpleDocTemplate(buffer, pagesize=A4, |
|
rightMargin=PDF_CONFIG['margin_right']*mm, |
|
leftMargin=PDF_CONFIG['margin_left']*mm, |
|
topMargin=PDF_CONFIG['margin_top']*mm, |
|
bottomMargin=PDF_CONFIG['margin_bottom']*mm) |
|
|
|
|
|
doc.build(contract_elements) |
|
|
|
|
|
buffer.seek(0) |
|
buffer_champs = io.BytesIO() |
|
|
|
p = canvas.Canvas(buffer_champs, 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=140, width=100, height=15, |
|
borderWidth=0, forceBorder=True) |
|
|
|
|
|
form.textfield(name='date', tooltip='Date de signature', |
|
x=230, y=140, 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) |
|
|
|
|
|
|
|
page_count = doc.page_count |
|
for page in range(1, page_count): |
|
p.showPage() |
|
form.textfield(name=f'paraphe_cedant_{page}', tooltip=f'Paraphe {cedant_label} - page {page}', |
|
x=70, y=30, width=50, height=20, |
|
borderWidth=0, forceBorder=True) |
|
form.textfield(name=f'paraphe_cessionnaire_{page}', tooltip=f'Paraphe Cessionnaire - page {page}', |
|
x=350, y=30, width=50, height=20, |
|
borderWidth=0, forceBorder=True) |
|
|
|
|
|
p.save() |
|
|
|
|
|
with open(output_filename, 'wb') as f: |
|
f.write(buffer.getvalue()) |
|
f.write(buffer_champs.getvalue()) |
|
|
|
return output_filename |
|
|
|
|
|
def add_interactive_fields(pdf_path, contract_type): |
|
""" |
|
Ajoute des champs interactifs à un PDF existant. |
|
|
|
Args: |
|
pdf_path (str): Chemin vers le PDF |
|
contract_type (list): Liste des types de contrats sélectionnés |
|
|
|
Returns: |
|
str: Chemin vers le PDF avec champs interactifs |
|
""" |
|
|
|
|
|
output_filename = create_temp_file(prefix="contrat_interactif_", suffix=".pdf") |
|
|
|
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" |
|
|
|
|
|
|
|
|
|
|
|
p.save() |
|
|
|
return output_filename |
|
|