- Ajout de texte libre sur les pages (Annotations > Ajouter du texte) - Texte multi-ligne, police, taille, couleur au choix - Position X/Y en mm, appliquer a page courante/toutes/paires/impaires - Ajout d'image sur les pages (Annotations > Ajouter une image) - PNG/JPG/BMP/GIF/TIFF, position, taille, ratio auto - Memes options d'application - Barre d'outils verticale a droite avec toutes les fonctions : - IMPOSITION : Livret, N-Up, Repetition, Manuel, Joindre - PAGES : Recadrer, Pg blanche, Fusionner - ANNOTATIONS : Texte, Image, Numeros, Masquer - REPERES : Coupe +/-, Bleed +/- - Toolbar horizontale : Ouvrir, Enregistrer, Imprimer, Zoom Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
87 lines
2.6 KiB
Python
87 lines
2.6 KiB
Python
"""Moteur ajout de contenu (texte, image) sur les pages."""
|
|
|
|
from pypdf import PdfReader, PdfWriter
|
|
from reportlab.pdfgen import canvas
|
|
from reportlab.lib.utils import ImageReader
|
|
from .pdf_utils import get_page_size
|
|
import io
|
|
|
|
|
|
def add_text_to_pages(input_path, output_path, text, font_name="Helvetica",
|
|
font_size=12, color=(0, 0, 0), x=0, y=0,
|
|
apply_to="all", current_page=0):
|
|
"""Ajouter du texte sur les pages."""
|
|
reader = PdfReader(input_path)
|
|
writer = PdfWriter()
|
|
|
|
for i, page in enumerate(reader.pages):
|
|
if _should_apply(i, apply_to, current_page):
|
|
w, h = get_page_size(page)
|
|
packet = io.BytesIO()
|
|
c = canvas.Canvas(packet, pagesize=(w, h))
|
|
c.setFont(font_name, font_size)
|
|
c.setFillColorRGB(*color)
|
|
|
|
# Multi-ligne
|
|
lines = text.split("\n")
|
|
line_height = font_size * 1.3
|
|
for j, line in enumerate(lines):
|
|
c.drawString(x, y - j * line_height, line)
|
|
|
|
c.showPage()
|
|
c.save()
|
|
packet.seek(0)
|
|
overlay = PdfReader(packet)
|
|
page.merge_page(overlay.pages[0])
|
|
|
|
writer.add_page(page)
|
|
|
|
with open(output_path, "wb") as f:
|
|
writer.write(f)
|
|
return output_path
|
|
|
|
|
|
def add_image_to_pages(input_path, output_path, image_path, x=0, y=0,
|
|
width=100, height=None, apply_to="all", current_page=0):
|
|
"""Ajouter une image sur les pages."""
|
|
reader = PdfReader(input_path)
|
|
writer = PdfWriter()
|
|
|
|
img = ImageReader(image_path)
|
|
img_w, img_h = img.getSize()
|
|
|
|
if height is None or height <= 0:
|
|
height = width * (img_h / img_w)
|
|
|
|
for i, page in enumerate(reader.pages):
|
|
if _should_apply(i, apply_to, current_page):
|
|
w, h = get_page_size(page)
|
|
packet = io.BytesIO()
|
|
c = canvas.Canvas(packet, pagesize=(w, h))
|
|
c.drawImage(image_path, x, y, width=width, height=height,
|
|
preserveAspectRatio=True, mask='auto')
|
|
c.showPage()
|
|
c.save()
|
|
packet.seek(0)
|
|
overlay = PdfReader(packet)
|
|
page.merge_page(overlay.pages[0])
|
|
|
|
writer.add_page(page)
|
|
|
|
with open(output_path, "wb") as f:
|
|
writer.write(f)
|
|
return output_path
|
|
|
|
|
|
def _should_apply(page_index, apply_to, current_page):
|
|
page_num = page_index + 1
|
|
if apply_to == "all":
|
|
return True
|
|
elif apply_to == "current":
|
|
return page_index == current_page
|
|
elif apply_to == "even":
|
|
return page_num % 2 == 0
|
|
elif apply_to == "odd":
|
|
return page_num % 2 != 0
|
|
return False
|